utoincrement, name varchar(15), age smallint);
INSERT INTO "student" VALUES(1,'zhangsan',24);
DELETE FROM sqlite_sequence;
INSERT INTO "sqlite_sequence" VALUES('student',1);
COMMIT;
7)从sql文件中导入表数据——导入
为了演示效果,我们先把student表删除再导入:
sqlite> drop table if exists student;
sqlite> .tables
sqlite>
sqlite> .read stu_bk.sql
sqlite> .tables
student
sqlite> .schema student
CREATE TABLE student(id integer primary key autoincrement, name varchar(15), age smallint);
sqlite> select * from student;
1|zhangsan|24
sqlite>
可以看到,数据导入成功。我们可以通过一个SQLite的客户端管理工具SQLite Expert来查看一下:

最后我们可以使用“.exit”或“.quit”退出SQLite命令行操作。
在Java中使用SQLite
要在Java中使用SQLite,需要下载SQLite相关驱动包,下载地址:https://bitbucket.org/xerial/sqlite-jdbc/downloads,这里我下载的还是以前的sqlite-jdbc-3.7.2.jar,当然你也可以下载最新的jar包。下面开始实例演示:
①新建一个名为sqlite的Java项目

我们引入了刚下载好的sqlite驱动包到类路径下,然后新建一个db的文件夹用于放置数据库文件。
②新建SQLiteTest.java文件,代码如下:
?
package com.leo.sqlite;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* SQLite在Java中的使用实例
*
* @author [*昨日重现*] lhy_ycu@163.com
* @since version 1.0
* @datetime 2015年5月18日 上午2:06:48
*/
public class SQLiteTest {
public static void main(String[] args) throws Exception {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:db/test.db");
Statement st = conn.createStatement();
st.executeUpdate("drop table if exists person");
st.executeUpdate("create table person(id integer,name varchar(15))");
st.executeUpdate("insert into person values(1,'zhangsan')");
st.executeUpdate("insert into person values(2,'lisi')");
st.executeUpdate("insert into person values(3,'wangwu')");
ResultSet rs = st.executeQuery("select * from person");
while (rs.next()) {
System.out.println("id = " + rs.getInt("id") + " ,name = "
+ rs.getString("name"));
}
st.close();
conn.close();
}
} 执行SQLiteTest.java文件,运行结果如下:
?

这时候,我们刷新一下sqlite项目,可以看到db文件夹下生成了一个test.db文件:

【注意】SQLite只提供数据库级的锁定,并且SQLite没有用户账户的概念,而是根据文件系统来确定所有数据库的权限。
?
现在主流移动设备像Android、iPhone等都使用SQLite作为复杂数据的存储引擎,在我们为移动设备开发应用程序时,也许就要使用到SQLite来存储大量的数据,所以我们有要掌握移动设备上SQLite开发技巧。对于Android平台来说,系统内置了大量丰富的API来供我们操作SQLite。下面开始演示一下SQLite的常用操作,为了方便,这里就将代码写在了Activity的onCreate方法中:
?
/**
* 在Android中使用SQLite:建库、建表、CRUD等简单操作
*
* @author [*昨日重现*] lhy_ycu@163.com
* @since version 1.0
* @datetime 2015-5-18 下午3:55:53
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 打开或创建test.db数据库
SQLiteDatabase db = openOrCreateDatabase("test.db",
Context.MODE_PRIVATE, null);
db.execSQL("drop table if exists person");
// 创建person表
db.execSQL("create table person(_id integer primary key autoincrement,name varchar,age smallint)");
Person person = new Person();
person.setName("zhangsan");
person.setAge(34);
// 插入数据
db.execSQL("insert into person values(NULL,?,?)",
new Object[] { person.getName(), person.getAge() });
person.setName("lisi");
person.s