Question

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();
        }


    }
Was it helpful?

Solution

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();

OTHER TIPS

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());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top