SQLite JDBC driver on Android

谁都会走 提交于 2021-02-08 20:51:20

问题


I'm trying to use xerial sqlite-jdbc to manage my database in Android with no success.I'm getting an java.lang.NoClassDefFoundError: org.sqlite.SQLiteConnection exception.I've imported this dependency 'org.xerial:sqlite-jdbc:3.18.0' in my gradle. My code is as follows,

 try {

        Class.forName("org.sqlite.JDBC");
        Connection connection = DriverManager.getConnection("jdbc:sqlite:hs.db");

    } catch (ClassNotFoundException eString) {System.err.println("Could not init JDBC driver - driver not found");
    } catch (java.sql.SQLException e) {e.printStackTrace();}

Using android.database.sqlite in my project is not option so please don't suggest that as an answer.


回答1:


Gradle

 compile 'org.sqldroid:sqldroid:1.0.3'

JAVA

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;

public class MainActivity extends AppCompatActivity {

    private Connection connection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            DriverManager.registerDriver((Driver) Class.forName("org.sqldroid.SQLDroidDriver").newInstance());
        } catch (Exception e) {
            throw new RuntimeException("Failed to register SQLDroidDriver");
        }
        String jdbcUrl = "jdbc:sqldroid:" + "/data/data/" + getPackageName() + "/my-database.db";
        try {
            this.connection = DriverManager.getConnection(jdbcUrl);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onDestroy() {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        super.onDestroy();
    }
}

https://github.com/SQLDroid/SQLDroid




回答2:


I needed to share some database code between my server DB and my phone DB, so I spent a lot of time writing the common code to use JDBC.

However, what I found out, after already being neck deep in this endeavor, is that there essentially isn't a working JDBC implementation on Android. I would strongly urge you not to go down this path - both Xerial and SQLDroid JDBC drivers have significant problems that I uncovered only during testing.

Problems with Xerial

It just doesn't work on Android :) It doesn't even load - I ran into this issue and couldn't work around it even after trying for hours.

Problems with SQLDroid

  1. See the Open Issues page - some of them were pretty scary to me. The deal breaker for me was the lack of batch support.
  2. See the Known Issues page before you start.

Finally, I ended up creating an implementation of the small subset of the java.sql interfaces and methods that I needed, using the android.database.sqlite classes. This was far less painful than I imagined (it took me just a couple of hours, for the subset of the functionality I needed) and it works great!



来源:https://stackoverflow.com/questions/44159823/sqlite-jdbc-driver-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!