Beginner in SQLite

2019-07-11 19:26发布

问题:

I haven't used SQLite before and I am looking if I could use it.

I am puzzled a bit. For example there does not seem to be a jdbc driver associated with SQLite that comes from the official SQLite.
I have found some posts/sites (some appear old links) that offer the jdbc driver but I am reluctant on how stable these drivers are or if they are missing features.

Additionally I am not clear on the usage of SQLite.

For example I tried the following code:

public static void main(String[] args) throws Exception {
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");
        Statement stat = conn.createStatement();
        stat.executeUpdate("drop table if exists people;");
        stat.executeUpdate("create table people (name, occupation);");
        PreparedStatement prep = conn.prepareStatement(
            "insert into people values (?, ?);");

        prep.setString(1, "Gandhi");
        prep.setString(2, "politics");
        prep.addBatch();
        prep.setString(1, "Turing");
        prep.setString(2, "computers");
        prep.addBatch();
        prep.setString(1, "Wittgenstein");
        prep.setString(2, "smartypants");
        prep.addBatch();

        conn.setAutoCommit(false);
        prep.executeBatch();
        conn.setAutoCommit(true);

        ResultSet rs = stat.executeQuery("select * from people;");
        while (rs.next()) {
            System.out.println("name = " + rs.getString("name"));
            System.out.println("job = " + rs.getString("occupation"));
        }
        rs.close();
        conn.close();

    }

This code creates a database file in my application directory.
So I guess the jdbc.jar creates this???
So I don't need to run the sqlite3.exe that I downloaded? Where should I get the jdbc driver from?

UPDATE:
This is my point. The link provided in one of the answers of PKeidel (http://www.zentus.com/sqlitejdbc/) is to a site that seems not to have been updated from 2009

回答1:

You can download the JDBC Driver from here: http://www.zentus.com/sqlitejdbc/

The sqlite3.exe is only needed when you work with your command line on your windows pc. You don't need it when you want to access a database with Java.

From Java you have to: - download the JDBC Driver - add it to your project/classpath - call Class.forName("org.sqlite.JDBC"); - now you can use it like in your first post