JDBC訪問及操作SQLite數據庫


  SQLite 是一個開源的嵌入式關系數據庫,其特點是高度便攜、使用方便、結構緊湊、高效、可靠。 與其他數據庫管理系統不同,SQLite 的安裝和運行非常簡單,在大多數情況下,只要確保SQLite的二進制文件存在即可開始創建、連接和使用數據庫。

   SQLite的下載頁面:http://www.sqlite.org/download.html

  window操作系統下載:sqlite-dll-win32-x86-3081002.zip及sqlite-shell-win32-x86-3081002.zip。解壓2個壓縮包,並將解壓后的路徑添加到系統變量的path中。

 

  在命令行中操作SQLite數據庫的做基本的命令如下。

  創建數據庫:sqlite3 test.db

  創建表:sqlite> create table mytable(id integer primary key, value text);

  插入數據:sqlite> insert into mytable(id, value) values(1, 'Micheal');

  查詢數據:sqlite> select * from mytable;

 

  JDBC connector for SQLite的地址為:https://bitbucket.org/xerial/sqlite-jdbc/downloads

  下載連接驅動,如sqlite-jdbc-3.8.7.jar,並將jar包加入到Java工程的引用中。

  使用下面的類測試是否能夠正常的訪問及操作SQLite數據庫。

package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class Test {
    public static void main(String[] args) throws Exception {
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement prep = null;
        try {
            Class.forName("org.sqlite.JDBC");
            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);");
            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);
            rs = stat.executeQuery("select * from people;");
            while (rs.next()) {
                System.out.println("name = " + rs.getString("name"));
                System.out.println("job = " + rs.getString("occupation"));
            }
            //分頁時,可以使用ResultSet來完成分頁rs.absolute(100),也可以sql語句中完成分頁select ... limit 100,10;
            //下面是釋放數據庫連接的過程,使用數據庫連接池時不應該釋放連接,而是將連接重新放到連接池中。
            //以代理的方式生成Connection的對象,調用Connection的close方法時將Connection加入到線程池中。線程池中加入的是Connection的代理對象即可。
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } finally {
                    if (prep != null) {
                        try {
                            prep.close();
                        } finally {
                            if (conn != null) {
                                conn.close();
                            }
                        }
                    }
                }
            }
        }
    }
}

 我們可以在工程的根目錄下發現一個名為test.db的文件,這就是剛創建的數據庫文件。我們也可以指定test.db為其他的路徑下的SQLite數據庫文件。

 

  下面是一些SQLite的其他常用的命令:

  設置格式化查詢結果: sqlite> .mode column  

             sqlite> .header on 

  修改表結構,增加列:sqlite> alter table mytable add column email text not null '' collate nocase;

  創建視圖: sqlite> create view nameview as select * from mytable;

  創建索引:sqlite> create index test_idx on mytable(value);

  格式化輸出數據到 CSV 格式:sqlite >.output [filename.csv ]  

                sqlite >.separator ,  

                sqlite > select * from test;  

                sqlite >.output stdout

  從 CSV 文件導入數據到表中:sqlite >create table newtable ( id integer primary key, value text );  

                sqlite >.import [filename.csv ] newtable

 

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM