2

I need to use a database on an Android application, since when in use the user won't have internet access. For that, I want to do a connection to the database via JDBC with SQL Lite. After some research I found out that it is not supported by the Android API, but there is project which does just that: SQLDroid

I downloaded the jars and followed the main tutorial, but I keep getting an sql exception java.sql.SQLException: No suitable driver when i want to create the connection with the DriverManager.

String url = "jdbc:sqldroid:" + "/data/data/com.mypackage.droid" + "/main.sqlite";
Connection con = DriverManager.getConnection(url);

What am I doing wrong? By the way, my activity has the name "AndroidActivity" and the package is called com.mypackage.droid.

Edit: Complete code:

public class AndroidActivity extends Activity {


    String url = "jdbc:sqldroid:" + "/data/data/com.mypackage.droid" + "/main.sqlite";
    static Connection con;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            Class.forName("SQLite.JDBCDriver");
            con = DriverManager.getConnection(url);
        } catch (java.sql.SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
Paulo Barros
  • 2,740
  • 8
  • 28
  • 36
  • Why can't you use the build in SQLite-support? – Lukas Knuth Oct 27 '11 at 14:10
  • Because google tells you to don't do so, because it is available but it doesn't work. In the link provided you can read about it. – Paulo Barros Oct 27 '11 at 14:14
  • I meant the build in SQLite-classes which enable you to work with a SQLite Database. See [this](http://developer.android.com/guide/topics/data/data-storage.html#db). – Lukas Knuth Oct 27 '11 at 14:15
  • I have a already working database, and it is used on desktop devices. I want to use this same database on the Droid. But thanks for the link – Paulo Barros Oct 27 '11 at 14:17

2 Answers2

2

With sqldroid-1.0.0RC1.jar I am using

Class.forName("org.sqldroid.SqldroidDriver");
con = DriverManager.getConnection(url);

Sometimes the driver can load via the drivermanager and sometimes it fails.

As a workaourd i am directly using SQLDroidDriver and this works all of the time.

con = new org.sqldroid.SQLDroidDriver().connect(url , new Properties());
k3b
  • 14,517
  • 7
  • 53
  • 85
2

Did you declare:

Class.forName("SQLite.JDBCDriver");

before declaring the connection?

EDIT:

Sorry I thought, that you want to use the native Android JDBC driver. In this case it would have been my approach.

But since you are using the SQLDroid library which brings an own driver you have to register the driver first by smth. like:

Class.forName("org.sqldroid.SqldroidDriver").newInstance();
Dyonisos
  • 3,541
  • 2
  • 22
  • 25