Does Android Support JDBC
Asked Answered
S

6

21

I know that on Android there is android.database.sqlite package that provides helpful classes to manage the internal android database.

The question is - can I use the standard java.sql package to manipulate Android's database without using anything from android.database.sqlite.* I try to open connection using SQLite JDBC driver but when i added the library as e reference to the project eclipse crashes with "java heap out of memory ... couldn't convert to dalvik VM".

Sanderson answered 13/11, 2009 at 10:46 Comment(0)
F
17

You cannot import a JAR implementing java.* classes easily. And, JDBC would need to be ported to Android, since it probably relies upon classes in JavaSE that Android lacks. And, you would need to write your own JDBC driver for SQLite anyway, wrapping the API Android already supplies, since I suspect the existing JDBC driver uses JNI. And, when all of that is done, you will have an application that adds a lot of bloat, making it less likely people will download and retain your application.

In short, I wouldn't go this route.

Flagstaff answered 13/11, 2009 at 16:0 Comment(1)
This is a bit misleading as it stands: The JDBC framework itself is present in Android and can be used (just connected to HSQLDB that way). A pure Java JDBC driver (as is the case with HSQLDB) would work with Android just like any other JAR (save for use of APIs not supported on Android, which is not specifically a JDBC issue). The issue is that most JDBC drivers, including SQLite, are built around a core of native code, which would have to be ported.Peloria
S
11

There is an (undocumented?) JDBC driver for Android's SQLite database. Try this: (from http://groups.google.com/group/android-developers/browse_thread/thread/cf3dea94d2f6243c)

    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);
    } 
Snaky answered 7/12, 2009 at 15:7 Comment(1)
Based on other information I have found, that seems to be true of Android versions somewhere in the 2.x range. On 4.4.4 and later, it is no longer present.Peloria
G
5

the JDBC driver is undocumented and unsupported. please do not use this code.

avoid java.sql and use android.database.sqlite instead.

Gargoyle answered 12/1, 2010 at 5:17 Comment(0)
I
2

There is such a driver now: SQLDroid.

Incurable answered 16/3, 2015 at 15:27 Comment(1)
It's unmaintained unfortunately and lacks a lot of features like getting generated keys after insert :(Pignus
P
0

Android has had full JDBC support from day one, but does not come with any drivers (at least officially). The general issue with JDBC drivers is that most rely on native code, which limits portability unless the developers specifically considered Android.

  • There seems to have been an unofficial SQLite JDBC driver, SQLite.JDBCDriver, with support for jdbc:sqlite:/path/to/db, in Android versions around 2.x. However, it appears to have long been removed (at least since 4.4.4).
  • SQLDroid and Xerial SQLite-JDBC are a third-party JDBC drivers which were written for Android. However, some users have reported various issues with these; I haven’t tried any of them myself.
  • Personally I have successfully used HSQLDB with a flat-file database (it can also run in client/server mode, which I have not tried on Android). Being pure Java (no native code), it is fully portable between Android and JRE.

Another issue you might run into when using JDBC on Android is that some Android mechanisms (such as content providers) expect a Cursor instance, as used by android.database.sqlite.*, whereas JDBC has ResultSet for that purpose. As these two are largely functional equivalents of each other, and Cursor is an interface, you might be able to write an adapter class which implements Cursor and translates its methods into their ResultSet equivalents.

Peloria answered 4/7, 2020 at 14:34 Comment(0)
T
0

There is another SQLite-jdbc driver out there. I got it working in my app as described here. However, there it is not supported for all Application Binary Interfaces, but the most devices are supported.

Trilly answered 29/4, 2021 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.