views:

2746

answers:

4

Hi all

I know that on android there is android.database.sqlite package that provides helpfull 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".

+8  A: 

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.

CommonsWare
+4  A: 

Hi

There is an (undocumented?) JDBC driver for Android's SQLite database. Try this: (from http://groups.google.com/group/android-developers/browse%5Fthread/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);
    }
kristianlm
http://code.google.com/p/sqldroid/
slf
+1  A: 

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

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

Elliott Hughes
A: 

SQLite is the bomb ... use that!

Brian T Hannan