Java操作Sqlite數據庫步驟:
1. 導入Sqlite jdbc
本文使用sqlite-jdbc-3.7.2.jar,下載地址 http://pan.baidu.com/s/1kVHAGdD
2. 編寫jdbc連接代碼
public class OpSqliteDB { private static final String Class_Name = "org.sqlite.JDBC"; private static final String DB_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(DB_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