標籤:
正如前面一文說的,SQLite多用於嵌入式開發中,但有時為了更方便的編輯資料庫檔案,我們也常常需要開發在電腦上啟動並執行SQLite程式。這種情況是經常發生的,比如在我們需要把一大批的txt檔案中的資料插入到一個資料庫中的時候。
還好這是很簡單的,所以本文我們來學習如何用Java開發SQLite程式。
(1)準備工作
下載sqlite-jdbc-版本號碼.jar檔案,放到jre\lib\ext檔案夾。如我的路徑是C:\Program Files\Java\jre1.8.0_77\lib\ext。
(2)一個測試代碼,可以先運行感受下,具體內容下面再詳細講。
import java.sql.*; public class SQLiteTest { public static void main(String[] args) { try { //串連SQLite的JDBC Class.forName("org.sqlite.JDBC"); //建立一個資料庫名test.db的串連,如果不存在就在目前的目錄下建立之 Connection mConnection = DriverManager.getConnection("jdbc:sqlite:test.db3"); Statement state = mConnection.createStatement(); //建立表 state.executeUpdate( "create table tb1(name varchar(20), grade int);" ); //插入資料 state.executeUpdate( "insert into tb1 values('名字1',90);" ); state.executeUpdate( "insert into tb1 values('名字2',58);" ); state.executeUpdate( "insert into tb1 values('名字3',69);" ); state.executeUpdate( "insert into tb1 values('名字4',85);" ); //查詢資料 ResultSet result = state.executeQuery("select * from tb1;"); //列印查詢結果 while (result.next()) { System.out.print(result.getString("name") + " "); System.out.println(result.getString("grade")); } result.close(); mConnection.close(); //結束資料庫的串連 } catch( Exception e ) { e.printStackTrace ( ); } }}
輸出結果:
名字1 90
名字2 58
名字3 69
名字4 85
(3)串連資料庫
我們需要串連到可用的JDBC驅動程式。之後我們需要建立到某個資料庫的串連,如果這個資料庫不存在,就會在指定位置建立資料庫檔案。並通過串連建立一個Statement執行個體。
//串連SQLite的JDBC
Class.forName("org.sqlite.JDBC");
//建立一個資料庫名test.db的串連,如果不存在就在目前的目錄下建立之
Connection mConnection = DriverManager.getConnection("jdbc:sqlite:test.db3");
Statement state =mConnection.createStatement();
(4)資料庫的操作:
對於不需要取得返回資料的SQL語句,我們都調用Statement的executeUpdate(str)方法執行對應SQL語句。包括建表、插入、刪除等等。對於需要得到返回資料的SQL查詢語句,我們需要調用executeQuery(str)方法,返回結果為一個ResultSet 對象,就像它的名字一樣,這是一個查詢結果的集合,我們可以用它的next()方法等來操控集合的行指標,用getXxx(“列名”)方法擷取某一行的某一列的值。
需要注意每個SQLite語句要以“;”結尾。
一種較特殊的情況就是插入語句中需要以傳入參數的形式來寫,這樣會省下拼接字串的諸多麻煩。這時候我們需要用到PreparedStatement類,使用的步驟如下所示:
<1> 定義插入資料的規範:
PreparedStatement prs =mConnection.prepareStatement("insert into tb1 values (?,?);");
<2>傳入參數:
第一個參數為列數,第二個參數為該列的值。
prs.setString(1,"名字5");
prs.setString(2,"83");
<3>執行語句:
調用下面的方法真正執行插入語句。
prs.addBatch();
mConnection.setAutoCommit(false);
prs.executeBatch();
mConnection.setAutoCommit(true);
我們修改上面的測試檔案用傳參的形式插入資料:
import java.sql.*; public class SQLiteTest { public static void main(String[] args) { try { //串連SQLite的JDBC Class.forName("org.sqlite.JDBC"); //建立一個資料庫名test.db的串連,如果不存在就在目前的目錄下建立之 Connection mConnection = DriverManager.getConnection("jdbc:sqlite:test.db3"); Statement state = mConnection.createStatement(); //建立表 state.executeUpdate( "create table tb1(name varchar(20), grade int);" ); //插入資料 state.executeUpdate( "insert into tb1 values('名字1',90);" ); state.executeUpdate( "insert into tb1 values('名字2',58);" ); state.executeUpdate( "insert into tb1 values('名字3',69);" ); state.executeUpdate( "insert into tb1 values('名字4',85);" ); //以傳參的形式插入資料 PreparedStatement prs = mConnection.prepareStatement("insert into tb1 values (?,?);"); prs.setString(1,"名字5"); prs.setString(2,"83"); prs.addBatch(); mConnection.setAutoCommit(false); prs.executeBatch(); mConnection.setAutoCommit(true); //查詢資料 ResultSet result = state.executeQuery("select * from tb1;"); //列印查詢結果 while (result.next()) { System.out.print(result.getString("name") + " "); System.out.println(result.getString("grade")); } result.close(); mConnection.close(); //結束資料庫的串連 } catch( Exception e ) { e.printStackTrace ( ); } } }
(5)關閉結果集、斷開與資料庫的串連
result.close();
mConnection.close();
Android學習筆記(43):Java開發SQLite程式