Java操作Sqlite資料庫

來源:互聯網
上載者:User

標籤:

Java操作Sqlite資料庫步驟:

1. 匯入Sqlite jdbc

本文使用sqlite-jdbc-3.7.2.jarhttp://pan.baidu.com/s/1kVHAGdD

2. 編寫jdbc串連代碼

public class OpSqliteDB {        private static final String Class_Name = "org.sqlite.JDBC";    private static final String CallMarkDB_URL = "jdbc:sqlite:F:\\xxxdatabase.db";    public static void main(String args[]) {        // load the sqlite-JDBC driver using the current class loader        Connection connection = null;        try {            connection = createConnection();            func1(connection);            System.out.println("Success!");        }  catch (SQLException e) {            System.err.println(e.getMessage());        } catch(Exception e) {            e.printStackTrace();        } finally{            try {                if (connection != null)                    connection.close();            } catch (SQLException e) {                // connection close failed.                System.err.println(e);            }        }    }        // 建立Sqlite資料庫連接    public static Connection createConnection() throws SQLException, ClassNotFoundException {        Class.forName(Class_Name);        return DriverManager.getConnection(CallMarkDB_URL);    }    public static void func1(Connection connection) throws SQLException {        Statement statement = connection.createStatement();        Statement statement1 = connection.createStatement();        statement.setQueryTimeout(30); // set timeout to 30 sec.        // 執行查詢語句        ResultSet rs = statement.executeQuery("select * from table_name1");        while (rs.next()) {            String col1 = rs.getString("col1_name");            String col2 = rs.getString("col2_name");            System.out.println("col1 = " + col1 + "  col2 = " + col2);                        System.out.println(location);            // 執行插入語句操作            statement1.executeUpdate("insert into table_name2(col2) values(‘" + col2_value + "‘)");            // 執行更新語句            statement1.executeUpdate("update table_name2 set 欄位名1=" +  欄位值1 + " where 欄位名2=‘" +  欄位值2 + "‘");        }    }

 

以下是其他的一些Sql語句,來自網址:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html (Android上面使用Sqlite)

查詢語句:

select * from 表名 where 條件子句 group by 分組字句 having ... order by 排序子句

如:

select * from person

select * from person order by id desc

select name from person group by name having count(*)>1


分頁SQL與mysql類似,下面SQL語句擷取5條記錄,跳過前面3條記錄

select * from Account limit 5 offset 3 或者 select * from Account limit 3,5


插入語句:

insert into 表名(欄位列表) values(值列表)。如: insert into person(name, age) values(‘傳智’,3)


更新語句:

update 表名 set 欄位名=值 where 條件子句。如:update person set name=‘傳智‘ where id=10


刪除語句:

delete from 表名 where 條件子句。如:delete from person where id=10

 

Java操作Sqlite資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.