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. Smack

Hello everybody.

I'm trying to extend several messages using smack library and I have
got a problem.

I can perfectly add an extension for a packet:




Message my_message = new Message();

my_message.setTo("[EMAIL PROTECTED]");

PacketExtension my_extension = new PacketExtension("my_element", "my
namespace");

my_message.addextension(my_extension);

SparkManager.getConnection().sendPacket();



JID receive this message and is able to show the XML text of this
message:




String xmlText = packet.toXML();


"<message ................>

<my_element xmlns="my namespace"/>

</message>"

But, how can I add attributes inside extension?. I mean, I want to
send this xml text after adding the extension:

"<message ................>

<my_element xmlns="my namespace">

     <header my_header=................/>
     <body>......................<body>

</my_element>

</message>"

Furthermore, how can I extend IQ messages and send them?.

Thank you, very much.
--~--~---------~--~----~------------~-------~--~----~

2. Moving files from one machine to another and getting eclipse to read it

Hi,

I copy the entire file structure for a project over to different
computer and eclipse will  not show the project or let me create from
existing source because the project already exists, but is not
displayed. I have tried refreshing every way I know, but it still will
not show up. Any help is appreciated.

--~--~---------~--~----~------------~-------~--~----~

3. android LBS emulation

4. Any answers for this...

5. When will the emulator support capturing input voice?

6. Receive an SMS (sms send +9998887777 hello)

7. SimpleCursorTreeAdapter