下载地址(直接导入即可,不需要再下载sqlite):
http://mvnrepository.com/artifact/org.xerial/sqlite-jdbc
常用命令学习,请移步:
http://www.runoob.com/sqlite/sqlite-tutorial.html
一.先来说说INSERT 批量操作性能优化
(亲自测试,1万条数据在120毫秒左右,10万条在800毫秒左右)
SQLite> insert into user(id,name,gender,token) values (10001,'name1','male','s878988tthsxs'), (10002,'name2','female','ztsbqzfty11211'), ..... (90701,'name1','male','s878988tthsxs');
二.使用更加严格DDL建表语句,尽量避免不必要的操作和数据输入
UNIQUE,NOT NULL,CHECK,DEFAULT,PRIMAERY KEY,AUTOINCREMENT
三,尽量使用int,float,varchar存储数据,特别是将时间,ip地址保存成int
五,SQLite-JDBC的使用方式
关于SQLite-JDBC
连接器
jdbc:sqlite://dirA/dirB/dbfile jdbc:sqlite://DRIVE:/dirA/dirB/dbfile jdbc:sqlite://COMPUTERNAME/shareA/dirB/dbfile
使用方式
import java.sql.*; import org.sqlite.JDBC; public class SQLiteTest { public static void main(String[] args) { try { String fileName = "c:/pmp.db"; l Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:"+fileName); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM pmp_countries ORDER BY country_name ASC"); while (rs.next()) { String id = rs.getString("country_id"); // Column 1 String code = rs.getString("country_code"); // Column 2 String name = rs.getString("country_name"); // Column 3 System.out.println("ID: "+id+" Code: "+code+" Name: "+name); } conn.close(); } catch (Exception e) { // Print some generic debug info System.out.println(e.getMessage()); System.out.println(e.toString()); } } }
插入数据
import java.sql.*; import org.sqlite.JDBC; /** * 这是个非常简单的SQLite的Java程序, * 程序中创建数据库、创建表、然后插入数据, * 最后读出数据显示出来 * @author zieckey (http://zieckey.cublog.cn) */ public class TestSQLite{ public static void main(String[] args){ try{ //连接SQLite的JDBC Class.forName("org.sqlite.JDBC"); //建立一个数据库名zieckey.db的连接,如果不存在就在当前目录下创建之 Connection conn = DriverManager.getConnection("jdbc:sqlite:zieckey.db"); Statement stat = conn.createStatement(); stat.executeUpdate( "create table tbl1(name varchar(20), salary int);" );//创建一个表,两列 stat.executeUpdate( "insert into tbl1 values('ZhangSan',8000);" ); //插入数据 stat.executeUpdate( "insert into tbl1 values('LiSi',7800);" ); stat.executeUpdate( "insert into tbl1 values('WangWu',5800);" ); stat.executeUpdate( "insert into tbl1 values('ZhaoLiu',9100);" ); ResultSet rs = stat.executeQuery("select * from tbl1;"); //查询数据 while (rs.next()) { //将查询到的数据打印出来 System.out.print("name = " + rs.getString("name") + " "); //列属性一 System.out.println("salary = " + rs.getString("salary")); //列属性二 } rs.close(); conn.close(); //结束数据库的连接 } catch( Exception e ) { e.printStackTrace ( ); } } }
批量插入
package com.yytwt.uap.download; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; public class SQLite3Test { public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("org.sqlite.JDBC"); Connection connection = null; try { connection = DriverManager.getConnection("jdbc:sqlite:sample.db"); connection.setAutoCommit(false); Statement statement = connection.createStatement(); statement.setQueryTimeout(30); // set timeout to 30 sec. statement.executeUpdate("drop table if exists person"); statement.executeUpdate("create table person (id integer, name varchar,token varchar)"); long start = System.currentTimeMillis(); StringBuilder sb = new StringBuilder("insert into person values"); int len = 500; for(int i=0;i<len;i++) { if(i<len-1) { sb.append(" ("+(i+1)+",'leo','"+Math.random()+"'),"); }else{ sb.append(" ("+(i+1)+",'leo','"+Math.random()+"');"); } } statement.executeUpdate(sb.toString()); long end = System.currentTimeMillis(); System.out.println("start="+start+",end="+end+",耗时"+(end-start)+"ms"); //获取受影响的行数 ResultSet rs = statement.executeQuery("select changes();"); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); while(rs.next()) { for (int i = 0; i < columnCount; i++) { String columnName = metaData.getColumnName(i+1); String value = rs.getString(i+1); System.out.println(columnName +"="+ value); } } statement.close(); rs.close(); } catch(SQLException e) { connection.rollback(); e.printStackTrace(); } finally { try { if(connection != null) connection.close(); } catch(SQLException e) { System.err.println(e); } } } }
二.一些常用的SQLite技巧
获取受影响的函数
select changes();
这里是一些常用SQLITE函数(Lang 和 JSON):
http://www.sqlite.org/lang_corefunc.html
http://www.sqlite.org/lang_aggfunc.html
http://www.sqlite.org/json1.html#jmini
打印表字段信息
PRAGMA TABLE_INFO('you_table_name')
这里是一下常用的PRAGMA 命令:
http://www.sqlite.org/pragma.html
使用Sqlite_master查询表信息,sqlite_master是用来记录所有表信息的数据表
CREATE TABLE sqlite_master ( type TEXT, name TEXT, tbl_name TEXT, rootpage INTEGER, sql TEXT );
查询数据库中指定名称的表是否存在
SELECT count(*) FROM sqlite_master WHERE type='table' and name='mytablename'
事务
BEGIN,COMMIT,ROLLBACK