`
这些年
  • 浏览: 389580 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

sqllite 简单使用

 
阅读更多

1:sqllite中文网站

http://www.sqlite.com.cn/

 2:sqllite 在java中的使用:

       1):jdbc包见附件,(管理软件(SQLiteExpert):http://www.xiazaiba.com/html/2771.html

        2):建库

sqlite.exe gbz.db #文件名 即库名 .databases 可以查看当前加载到内存中的库

       3):建表(表名不能以sqlite_开头)

CREATE TABLE testtable (first_col integer);    #.table  可以查看所有表

         可以创建表时指定默认值

CREATE TABLE testtable (first_col integer DEFAULT 0, second_col varchar DEFAULT 'hello');

         为其它数据库创建表

ATTACH DATABASE 'd:/mydb.db' AS mydb;           #库为mydb
CREATE TABLE mydb.testtable (first_col integer); 

        创建临时表(关闭链接时temp表消失)

CREATE TEMP TABLE temptable(first_col integer);

        保存退出

.backup d:/mydb.db #到指定库

 问题:覆盖创建(除非当前的表名和某一索引名冲突)

sqlite> create table gbz(aaa integer);    #gbz已经存在
Error: table gbz already exists
sqlite> create table if not exists gbz(aaa integer);

      使用select结果集创建表(只有数据)

CREATE TABLE testtable2 AS SELECT * FROM testtable;    #.schema gbz 查看创表语句

       4):约束

6. 主键约束:
--直接在字段的定义上指定主键。
sqlite> CREATE TABLE testtable (first_col integer PRIMARY KEY ASC);
--在所有字段已经定义完毕后,再定义表的数约束,这里定义的是基于first_col和second_col的联合主键。
sqlite> CREATE TABLE testtable2 (...>     first_col integer,...>     second_col integer,
...>     PRIMARY KEY (first_col,second_col)...> );
和其他关系型数据库一样,主键必须是唯一的。 
7. 唯一性约束:
--直接在字段的定义上指定唯一性约束。
sqlite> CREATE TABLE testtable (first_col integer UNIQUE);
--在所有字段已经定义完毕后,在定义表的唯一性约束,这里定义的是基于两个列的唯一性约束。
sqlite> CREATE TABLE testtable2 (...>     first_col integer,...>     second_col integer,
...>     UNIQUE (first_col,second_col)...> ); 
在SQLite中,NULL值被视为和其他任何值都是不同的,这样包括和其他的NULL值,如下例:
sqlite> DELETE FROM testtable;
sqlite> SELECT count(*) FROM testtable;count(*)----------0
sqlite> INSERT INTO testtable VALUES(NULL);sqlite> INSERT INTO testtable VALUES(NULL);sqlite> SELECT count(*) FROM testtable;count(*)----------2
由此可见,两次插入的NULL值均插入成功。 
8. 为空(NOT NULL)约束:
sqlite> CREATE TABLE testtable(first_col integer NOT NULL);sqlite> INSERT INTO testtable VALUES(NULL);Error: testtable.first_col may not be NULL
从输出结果可以看出,first_col已经被定义了非空约束,因此不能在插入NULL值了。 
9. 检查性约束:
sqlite> CREATE TABLE testtable (first_col integer CHECK (first_col < 5));sqlite> INSERT INTO testtable VALUES(4);
sqlite> INSERT INTO testtable VALUES(20); -- 20违反了字段first_col的检查性约束(first_col < 5)
Error: constraint failed
--和之前的其它约束一样,检查性约束也是可以基于表中的多个列来定义的。
sqlite> CREATE TABLE testtable2 (...>     first_col integer,...>     second_col integer,
...>     CHECK (first_col > 0 AND second_col < 0)...> );

           5):其它

二、表的修改:
SQLite对ALTER TABLE命令支持的非常有限,仅仅是修改表名和添加新字段。其它的功能,如重命名字段、删除字段和添加删除约束等均为提供支持。 
1. 修改表名:
需要先说明的是,SQLite中表名的修改只能在同一个数据库中,不能将其移动到Attached数据库中。再有就是一旦表名被修改后,该表已存在的索引将不会受到影响,然而依赖该表的视图和触发器将不得不重新修改其定义。
sqlite> CREATE TABLE testtable (first_col integer);
sqlite> ALTER TABLE testtable RENAME TO testtable2;sqlite> .tablestesttable2 
通过.tables命令的输出可以看出,表testtable已经被修改为testtable2。 
2. 新增字段:
sqlite> CREATE TABLE testtable (first_col integer);
sqlite> ALTER TABLE testtable ADD COLUMN second_col integer;sqlite> .schema testtable
CREATE TABLE "testtable" (first_col integer, second_col integer); 
通过.schema命令的输出可以看出,表testtable的定义中已经包含了新增字段。
关于ALTER TABLE最后需要说明的是,在SQLite中该命令的执行时间是不会受到当前表行数的影响,也就是说,修改有一千万行数据的表和修改只有一条数据的表所需的时间几乎是相等的。 
三、表的删除:
在SQLite中如果某个表被删除了,那么与之相关的索引和触发器也会被随之删除。在很多其他的关系型数据库中是不可以这样的,如果必须要删除相关对象,只能在删除表语句中加入WITH CASCADE从句。见如下示例:
sqlite> CREATE TABLE testtable (first_col integer);sqlite> DROP TABLE testtable;sqlite> DROP TABLE testtable;Error: no such table: testtable
sqlite> DROP TABLE IF EXISTS testtable; 
从上面的示例中可以看出,如果删除的表不存在,SQLite将会报错并输出错误信息。如果希望在执行时不抛出异常,我们可以添加IF EXISTS从句,该从句的语义和CREATE TABLE中的完全相同。 
四、创建视图:
我们这里只是给出简单的SQL命令示例,具体的含义和技术细节可以参照上面的创建数据表部分,如临时视图、"IF NOT EXISTS"从句等。

1. 最简单的视图:
sqlite> CREATE VIEW testview AS SELECT * FROM testtable WHERE first_col > 100; 
2. 创建临时视图:
sqlite> CREATE TEMP VIEW tempview AS SELECT * FROM testtable WHERE first_col > 100; 
3. "IF NOT EXISTS"从句:
sqlite> CREATE VIEW testview AS SELECT * FROM testtable WHERE first_col > 100;Error: table testview already exists
sqlite> CREATE VIEW IF NOT EXISTS testview AS SELECT * FROM testtable WHERE first_col > 100; 
五、删除视图:
该操作的语法和删除表基本相同,因此这里只是给出示例:sqlite> DROP VIEW testview;sqlite> DROP VIEW testview;Error: no such view: testview
sqlite> DROP VIEW IF EXISTS testview;

 我的java代码:

private Connection getConn() {
		Connection conn = null;
		String resource = System.getProperty("user.dir");//"C:\\Users\\baozong.gao\\Desktop\\财务程序";
		try {
			Class.forName("org.sqlite.JDBC");
			conn = DriverManager.getConnection("jdbc:sqlite:/" + resource + "\\Configuration\\billDataBase.db");
		} catch (SQLException e) {
			logger.error("连接本地数据库错误");
		} catch (ClassNotFoundException e) {
			logger.error("连接本地数据库错误");
		}
		return conn;
	}

 因为它会把内存中的数据更新到本地文件,所以每次提交都会open文件,耗时较长

protected void save(List<List<String>> sqlParameterList) {
		Connection conn = getConn();
		try {
			PreparedStatement ps = conn
					.prepareStatement("INSERT INTO EXCEL_MEM VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			for (List<String> sqlParameter : sqlParameterList) {
				for (int i = 0; i < sqlParameter.size(); i++) {
					ps.setString(i + 1, sqlParameter.get(i));
				}
				ps.addBatch();
			}
			conn.setAutoCommit(false);// 提高性能
			ps.executeBatch();
			conn.setAutoCommit(true);

			ps.close();
			conn.close();
		} catch (SQLException e) {
			logger.error("保存数据错误");
					}
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics