JDBC Driver for SQLiteDatabase

by kristianlm » Tue, 08 Dec 2009 22:11:27 GMT


Sponsored Links
 Hi folks!


I've been looking around for a portable way to use database
functionality in Android without much success. I wanted to interface
the built-in SQLite database in Android through JDBC but there were no
drivers ...right? at least I could not find any!

So I started my own:  http://code.google.com/p/sqldroid/ 


It's only a beginning, but it works for my relatively simple demands
at the moment.
So my question is:

Why is no such JDBC driver already included in Android? Am I missing
the big picture here? I fear this is a bad idea but that I don't see
it. Why would you not want to interface the built-in SQLite engine
through JDBC?


Any advice welcome!
Thanks,
Kris

--



JDBC Driver for SQLiteDatabase

by Mark Murphy » Tue, 08 Dec 2009 23:00:16 GMT


 > Why is no such JDBC driver already included in Android?

Android does not support JDBC.


Android is designed to be used on devices with limited RAM, limited CPU,
limited storage space for application code, and limited electricity
(battery), to connect to local databases.

JDBC is designed to be used on devices with lots of RAM, lots of CPU
speed, comparatively unlimited storage space for application code,
full-time AC power, to connect to local databases and database servers.


You lose performance, battery life, and on-board storage space, all of
which are in short supply in an Android device.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
Android App Developer Books:  http://commonsware.com/books.html 


--


Sponsored Links


JDBC Driver for SQLiteDatabase

by Joerg Pleumann » Wed, 09 Dec 2009 02:38:06 GMT


 There is JDBC support in Android (see online reference for package
java.sql). There is also a built-in, though somewhat limited JDBC
driver for SQLite. Try this inside an Activity:

        try {
            String db = "jdbc:sqlite:" + getFilesDir() + "/test.db";

            Class.forName("SQLite.JDBCDriver");
            Connection conn = DriverManager.getConnection(db);
            Statement stat = conn.createStatement();
            stat.executeUpdate("create table primes (number int);");
            stat.executeUpdate("insert into primes values (2);");
            stat.executeUpdate("insert into primes values (3);");
            stat.executeUpdate("insert into primes values (5);");
            stat.executeUpdate("insert into primes values (7);");

            ResultSet rs = stat.executeQuery("select * from primes");
            boolean b = rs.first();
            while (b) {
                Log.d("JDBC", "Prime=" + rs.getInt(1));
                b = rs.next();
            }

            conn.close();
        } catch (Exception e) {
            Log.e("JDBC", "Error", e);
        }

Unless you really need to write or maintain portable code, I'd still
recommend using the SQLite interface in the android.database package,
because it's nicely integrated with the UI classes (ListActivity and
the like).

Cheers,
Joerg




--



JDBC Driver for SQLiteDatabase

by kristianlm » Wed, 09 Dec 2009 02:45:09 GMT


 Hi Mark,
and thanks for your feedback.

I cannot say I quite follow you though. There isn't really that much
overhead involved, and my JAR-file is only 17kB. The JDBC Driver is a
simple wrapper for the SQLiteDatabase calls, but only a small subset
of the JDBC interface is implemented.

I haven't used JDBC extensively, but I don't see why you wouldn't want
to do the database basics using JDBC. That way, everyone wouldn't have
to learn another database API and the code would be portable. In my
opinion, these benefits undermine the minor overhead introduced by a
JDBC driver.

In JDBC, you can specify things like directions of cursor (e.g.
CURSOR_FORWARD_ONLY) etc. which fits nicely into the existing
SQLiteDatabase architecture. So I guess my argument is while JDBC is
designed for heavy stuff, it is still suitable for embedded stuff.
Specially when Android already has the JDBC interface readily
available.

- Kris





--



JDBC Driver for SQLiteDatabase

by Mark Murphy » Wed, 09 Dec 2009 02:47:10 GMT


 > There is JDBC support in Android (see online reference for package

:: blink, blink ::

My apologies!

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
Android App Developer Books:  http://commonsware.com/books.html 


--



JDBC Driver for SQLiteDatabase

by jotobjects » Wed, 09 Dec 2009 02:50:42 GMT


 > JDBC is designed to be used on devices with lots of RAM, lots of CPU
JDBC drivers are often/usually used with remote databases (this is the
simplest meaning of 'n' tier).

By Java API standards the JDBC API is pretty compact and some of the
drivers are very small.  But then you need a different driver for each
database and the drivers would be duplicated in every application
unless they were built into the platform (which databases would you
vote to exclude?).  JDBC by itself has verbose syntax. It require
knowledge of SQL and is not object oriented which has lead to a
plethora of ORM's like Hibernate and more recently to standard JPA
(OpenJPA).  All those drivers and all those ORM's would be a blot of
cement on an Android platform.  So for the intended use it seems like
SQLite is a pretty good alternative.

--



JDBC Driver for SQLiteDatabase

by kristianlm » Wed, 09 Dec 2009 03:02:41 GMT


 i Joerg,

sorry your post just came in while I was writing the other.

I cant believe this though - there's already a driver available? I
can't find that in the docs anywhere! Which java.sql online manual are
you looking at? I'm at
http://developer.android.com/reference/java/sql/package-summary.html
but I don't see it anywhere.

Also, SQLite.JDBCDriver doesn't seem to be in android.jar ... any
ideas where it's coming from and why it's a secret?

And why is there no mention of JDBC in the database-section of:
http://developer.android.com/guide/topics/data/data-storage.html

Where can you turn to suggest such doc change?
I've seen several posts looking for JDBC access to their Android
SQLite db.

Anyways,
Thanks a lot for your help

On Dec 8, 7:37pm, Joerg Pleumann <joerg.pleum...@gmail.com> wrote:

--



JDBC Driver for SQLiteDatabase

by kristianlm » Wed, 09 Dec 2009 03:09:14 GMT


 no worries! I think we're all confused here (except Joerg)
i'm glad I'm not the only one who got this JDBC-android-support jungle
wrong




--



JDBC Driver for SQLiteDatabase

by jotobjects » Wed, 09 Dec 2009 03:33:53 GMT


 



 http://dev.android.com/reference/java/sql/package-summary.html 

That's JDBC - the Connection object uses a JDBC driver for SQLite.

Try Connection.getMetaData().getDriverName() and see what you get.
Probably it will be "SQLite.JDBCDriver"

--



JDBC Driver for SQLiteDatabase

by kristianlm » Wed, 09 Dec 2009 04:56:17 GMT


 I see. I still think the documents are lacking!

Am I the only one who'd want a comment on the JDBC
support in the dev-guide documents?

There seems to be a fair share of confused people out there ...






--



JDBC Driver for SQLiteDatabase

by Joerg Pleumann » Wed, 09 Dec 2009 14:41:16 GMT


 I agree the existence of the driver should be mentioned at least in
the JDBC package docs. Would you mind creating a ticket for this?

JDBC is somewhat of a second-class citizen in the Android world, since
Android has its own database API (that is actually not too far away
from JDBC, but has much better integration with the UI framework). I
guess this is why the docs don't encourage you to use JDBC. Still,
JDBC is usable, and - as you already noticed - it is possible to
install additional drivers.

Regarding android.jar, I never checked but I could imagine that it
contains only the public API classes and might even have the actual
bytecode erased. Nothing is ever run against it. It is just there to
make Javac or Eclipse happy (somebody please correct me if I'm wrong).
On the device, the driver should be part of /system/framework/
core.jar.

Cheers,
Joerg






--



JDBC Driver for SQLiteDatabase

by kristianlm » Fri, 11 Dec 2009 00:14:01 GMT


 Ok. Thanks a lot Joerg!

I wanna create a ticket, but I don't know where to turn

Is this where I'm supposed to post:
 http://code.google.com/p/android/issues/list 

It seems to be only for bugs though, is it?
Anyhow, I commited a bug there half a year
ago and it still hasn't been looked at, is
anyone dealing with those issues at all?

K







--



JDBC Driver for SQLiteDatabase

by Joerg Pleumann » Sat, 12 Dec 2009 21:19:58 GMT


 Hi Kris,

yes, this is the general bugtracker for the Android open-source
project. Please use it. I think it is adequate also for documentation
issues or feature requests. I don't know about your specific issue,
but the issues are being looked at by the Android team (mostly Google
guys) and worked on. For instance, I filed a small feature request for
DDMS, and the feature is now part of the SDK.

Cheers,
Joerg








--



JDBC Driver for SQLiteDatabase

by jotobjects » Sun, 13 Dec 2009 05:42:55 GMT


 

The SQLite driver is never used by the public API.  You pass the name
as a String URI "jdbc:sqlite:" to getConnection().  That is the way
JDBC always works. The drivers are not part of the public API so the
documentation is NOT missing anything regarding drivers.

As far as I can see the only missing documentation is the URI
"jdbc:sqlite:" as in Joerg's example -

            String db = "jdbc:sqlite:" + getFilesDir() + "/test.db";
            Class.forName("SQLite.JDBCDriver");
            Connection conn = DriverManager.getConnection(db);

The Class.forName() call is only necessary if Android does not
automatically register the driver with DriverManager (I don't know if
it does or not).

--



JDBC Driver for SQLiteDatabase

by kristianlm » Mon, 14 Dec 2009 21:29:53 GMT


 Hi jotobject,
I don't understant why this wouldn't be part of the public API. if
Android is shipped with a JDBC driver, why not let people use it? It's
certainly useful for many of us!


Kris






--



Other Threads

1. How to bypass "Waiting for sevice media.audio_policyb" ?

Hi All,

I am new developer for Android platform, currently we are using iMX51
for our development.
We changed codec chip form SGTL500C to Wolfson series, but the new
driver is not ready yet.
I found device will stop at animation logo if without audio driver
support. Can I bypass this check and keep going remain boot-up process
before audio driver ready?

Rick

Parts of messages that created by logcat.
----------------------------------------------------------------

I/        ( 2170): ServiceManager: 0xacd0
E/ALSALib ( 2170): external/alsa-lib/src/control/control.c:902:
(snd_ctl_open_noupdate) Invalid CTL AndroidOut
W/AudioHardwareALSA( 2170): Unable to attach mixer to device
AndroidOut: No such file or directory
E/AudioHardwareALSA( 2170): Unable to attach mixer to device default:
No such file or directory
E/ALSALib ( 2170): external/alsa-lib/src/control/control.c:902:
(snd_ctl_open_noupdate) Invalid CTL AndroidIn
W/AudioHardwareALSA( 2170): Unable to attach mixer to device
AndroidIn: No such file or directory
E/AudioHardwareALSA( 2170): Unable to attach mixer to device default:
No such file or directory
I/DEBUG   ( 2049): *** *** *** *** *** *** *** *** *** *** *** *** ***
*** *** ***
I/DEBUG   ( 2049): Build fingerprint: 'freescale/imx51_bbg/imx51_bbg/
imx51_bbg:2.2/FRF85B/eng.b18293.20101115.135326:eng/test-keys'
I/DEBUG   ( 2049): pid: 2170, tid: 2170  >>> /system/bin/mediaserver
<<<
I/DEBUG   ( 2049): signal 11 (SIGSEGV), fault addr 00000010
I/DEBUG   ( 2049):  r0 00000000  r1 08000000  r2 00000009  r3 00000000
I/DEBUG   ( 2049):  r4 a970e8a4  r5 0000b028  r6 beb07918  r7 0000b039
I/DEBUG   ( 2049):  r8 00000000  r9 0000aeb8  10 00000000  fp beb079b4
I/DEBUG   ( 2049):  ip a970e634  sp beb07918  lr a9709af0  pc
8013c4ec  cpsr 20000010
I/DEBUG   ( 2049):  d0  6472656767756265  d1  78696d2068636165
I/DEBUG   ( 2049):  d2  6564206f74207263  d3  6665642065636974
I/DEBUG   ( 2049):  d4  6c74635f646e7328  d5  6f6e5f6e65706f5f
I/DEBUG   ( 2049):  d6  2029657461647075  d7  2064696c61766e49
I/DEBUG   ( 2049):  d8  0000000000000000  d9  0000000000000000
I/DEBUG   ( 2049):  d10 0000000000000000  d11 0000000000000000
I/DEBUG   ( 2049):  d12 0000000000000000  d13 0000000000000000
I/DEBUG   ( 2049):  d14 0000000000000000  d15 0000000000000000
I/DEBUG   ( 2049):  d16 0000000000000000  d17 0000000000000000
I/DEBUG   ( 2049):  d18 0000000000000000  d19 0000000000000000
I/DEBUG   ( 2049):  d20 0000000000000000  d21 0000000000000000
I/DEBUG   ( 2049):  d22 0000000000000000  d23 0000000000000000
I/DEBUG   ( 2049):  d24 0000000000000000  d25 0000000000000000
I/DEBUG   ( 2049):  d26 0000000000000000  d27 0000000000000000
I/DEBUG   ( 2049):  d28 0000000000000000  d29 0000000000000000
I/DEBUG   ( 2049):  d30 0000000000000000  d31 0000000000000000
I/DEBUG   ( 2049):  scr 00000000
I/DEBUG   ( 2049):
I/DEBUG   ( 2049):          #00  pc 0003c4ec  /system/lib/libasound.so
I/DEBUG   ( 2049):          #01  pc 00009aec  /system/lib/libaudio.so
I/DEBUG   ( 2049):          #02  pc 0000794c  /system/lib/libaudio.so
I/DEBUG   ( 2049):          #03  pc 00007aa8  /system/lib/libaudio.so
I/DEBUG   ( 2049):          #04  pc 0000ad2c  /system/lib/libaudio.so
I/DEBUG   ( 2049):          #05  pc 00021bc0  /system/lib/
libaudioflinger.so
I/DEBUG   ( 2049):          #06  pc 00021c46  /system/lib/
libaudioflinger.so
I/DEBUG   ( 2049):          #07  pc 0000879e  /system/bin/mediaserver
I/DEBUG   ( 2049):          #08  pc 0000d3c0  /system/lib/libc.so
I/DEBUG   ( 2049):
I/DEBUG   ( 2049): code around pc:
I/DEBUG   ( 2049): 8013c4cc e2000029 e2811001 e18cc000 e1520001
I/DEBUG   ( 2049): 8013c4dc 8afffffb e3a00000 e1c3c0b0 e12fff1e
I/DEBUG   ( 2049): 8013c4ec e5b03010 e1530000 03a00000 12430004
I/DEBUG   ( 2049): 8013c4fc e12fff1e e5902010 e2803010 e1520003
I/DEBUG   ( 2049): 8013c50c 03a00000 15900014 12400004 e12fff1e
I/DEBUG   ( 2049):
I/DEBUG   ( 2049): code around lr:
I/DEBUG   ( 2049): a9709ad0 e5808004 e3a00064 e5c58010 e5850008
I/DEBUG   ( 2049): a9709ae0 e59c0004 ebfff574 e5990004 ebfff4b5
I/DEBUG   ( 2049): a9709af0 e2504000 1a000004 ea00002e e1a00004
I/DEBUG   ( 2049): a9709b00 ebfff630 e2504000 0a00002a e1a00004
I/DEBUG   ( 2049): a9709b10 ebfff5db e3500000 0afffff7 e1a01006
I/DEBUG   ( 2049):
I/DEBUG   ( 2049): stack:
I/DEBUG   ( 2049):     beb078d8  00000000
I/DEBUG   ( 2049):     beb078dc  40003024  /dev/ashmem/
system_properties (deleted)
I/DEBUG   ( 2049):     beb078e0  0000b039  [heap]
I/DEBUG   ( 2049):     beb078e4  40003020  /dev/ashmem/
system_properties (deleted)
I/DEBUG   ( 2049):     beb078e8  00000001
I/DEBUG   ( 2049):     beb078ec  0000b039  [heap]
I/DEBUG   ( 2049):     beb078f0  0000b039  [heap]
I/DEBUG   ( 2049):     beb078f4  a970d220  /system/lib/libaudio.so
I/DEBUG   ( 2049):     beb078f8  0000b039  [heap]
I/DEBUG   ( 2049):     beb078fc  afd258f7  /system/lib/libc.so
I/DEBUG   ( 2049):     beb07900  a970e8a4  /system/lib/libaudio.so
I/DEBUG   ( 2049):     beb07904  af90605b  /system/lib/libcutils.so
I/DEBUG   ( 2049):     beb07908  a970e8a4  /system/lib/libaudio.so
I/DEBUG   ( 2049):     beb0790c  0000b028  [heap]
I/DEBUG   ( 2049):     beb07910  df002777
I/DEBUG   ( 2049):     beb07914  e3a070ad
I/DEBUG   ( 2049): #01 beb07918  00000000
I/DEBUG   ( 2049):     beb0791c  00000000
I/DEBUG   ( 2049):     beb07920  00000000
I/DEBUG   ( 2049):     beb07924  00000000
I/DEBUG   ( 2049):     beb07928  00000000
I/DEBUG   ( 2049):     beb0792c  00000000
I/DEBUG   ( 2049):     beb07930  00000000
I/DEBUG   ( 2049):     beb07934  00000000
I/DEBUG   ( 2049):     beb07938  00000000
I/DEBUG   ( 2049):     beb0793c  00000000
I/DEBUG   ( 2049):     beb07940  00000000
I/DEBUG   ( 2049):     beb07944  00000000
I/DEBUG   ( 2049):     beb07948  00000000
I/DEBUG   ( 2049):     beb0794c  00000000
I/DEBUG   ( 2049):     beb07950  00000000
I/DEBUG   ( 2049):     beb07954  00000000
I/DEBUG   ( 2049):     beb07958  0000026c
I/DEBUG   ( 2049):     beb0795c  a9709a4c  /system/lib/libaudio.so
I/DEBUG   ( 2049):     beb07960  a970e200  /system/lib/libaudio.so
I/DEBUG   ( 2049):     beb07964  00000000
I/DEBUG   ( 2049):     beb07968  00000228
I/DEBUG   ( 2049):     beb0796c  0000027c
I/DEBUG   ( 2049):     beb07970  0000aeb8  [heap]
I/DEBUG   ( 2049):     beb07974  a970e1f8  /system/lib/libaudio.so
I/DEBUG   ( 2049):     beb07978  a970e964  /system/lib/libaudio.so
I/DEBUG   ( 2049):     beb0797c  00000000
I/DEBUG   ( 2049):     beb07980  a970e628  /system/lib/libaudio.so
I/DEBUG   ( 2049):     beb07984  00000000
I/DEBUG   ( 2049):     beb07988  00000000
I/DEBUG   ( 2049):     beb0798c  479bc56a
I/DEBUG   ( 2049):     beb07990  00000000
I/DEBUG   ( 2049):     beb07994  a970e628  /system/lib/libaudio.so
I/DEBUG   ( 2049):     beb07998  0000ae50  [heap]
I/DEBUG   ( 2049):     beb0799c  beb079bc  [stack]
I/DEBUG   ( 2049):     beb079a0  0000aeb8  [heap]
I/DEBUG   ( 2049):     beb079a4  00000000
I/DEBUG   ( 2049):     beb079a8  00000000
I/DEBUG   ( 2049):     beb079ac  00000000
I/DEBUG   ( 2049):     beb079b0  00000000
I/DEBUG   ( 2049):     beb079b4  a9707950  /system/lib/libaudio.so
I/ServiceManager( 2101): Waiting for sevice media.audio_policy...

-- 

2. Android Emulator: "Accounts & Sync"

I'm using the 2.2 Android emulator. How do you enter your google
account details?

I would like to test a new app called chrometophone which uses 2.2's
new C2DM api. This seems to require being able to add google account
details.

--

3. Whatsapp.... Ada yg tau?

4. Pejing DHD user

5. [wts] motorola xt800 android gsm cdma dual on

6. 3G data is paused when phone goes to idle/sleep

7. SOLD WTS : NEXIAN Journey 2nd like NEW (tanpa Bundle 3)