python 中使用sqlite3数据库

5 篇文章 0 订阅
0 篇文章 0 订阅

SQLite 是一个开源的嵌入式关系数据库,实现自包容、零配置、支持事务的SQL数据库引擎。 其特点是高度便携、使用方便、结构紧凑、高效、可靠。 与其他数据库管理系统不同,SQLite 的安装和运行非常简单,在大多数情况下 - 只要确保SQLite的二进制文件存在即可开始创建、连接和使用数据库。如果您正在寻找一个嵌入式数据库项目或解决方案,SQLite是绝对值得考虑。

移植过程简单,具体参考相关文档。

以下是简单的教程:参考http://database.51cto.com/art/201205/335411.htm

1、 创建 SQLite 数据库

现在你已经安装了 SQLite 数据库,接下来我们创建首个数据库。在命令行窗口中输入如下命令来创建一个名为 test.db 的数据库

 
 
  1. sqlite3 test.db 

创建表:

 
 
  1. sqlite> create table  if not exists dataCatlog(id varchar primary key ,content varchar   

该表包含一个名为 id 的主键字段和一个名为 value 的文本字段。

接下来往表里中写入一些数据:

 
 
  1. sqlite> insert into mytable(id, value) values(1, 'Micheal');  
  2. sqlite> insert into mytable(id, value) values(2, 'Jenny');  
  3. sqlite> insert into mytable(value) values('Francis');  
  4. sqlite> insert into mytable(value) values('Kerk'); 

查询数据:

 
 
  1. sqlite> select * from test;  
  2. 1|Micheal  
  3. 2|Jenny  
  4. 3|Francis  
  5. 4|Kerk 

设置格式化查询结果:

 
 
  1. sqlite> .mode column;  
  2. sqlite> .header on;  
  3. sqlite> select * from test;  
  4. id          value  
  5. ----------- -------------  
  6. 1           Micheal  
  7. 2           Jenny  
  8. 3           Francis  
  9. 4           Kerk 

.mode column 将设置为列显示模式,.header 将显示列名。

修改表结构,增加列:

 
 
  1. sqlite> alter table mytable add column email text not null '' collate nocase;; 

创建视图:

 
 
  1. sqlite> create view nameview as select * from mytable; 

创建索引:

 
 
  1. sqlite> create index test_idx on mytable(value); 

4. 一些有用的 SQLite 命令

显示表结构:

 
 
  1. sqlite> .schema [table

获取所有表和视图:

 
 
  1. sqlite > .tables 

获取指定表的索引列表:

 
 
  1. sqlite > .indices [table ] 

导出数据库到 SQL 文件:

 
 
  1. sqlite > .output [filename ]  
  2. sqlite > .dump  
  3. sqlite > .output stdout 

从 SQL 文件导入数据库:

 
 
  1. sqlite > .read [filename ] 

格式化输出数据到 CSV 格式:

 
 
  1. sqlite >.output [filename.csv ]  
  2. sqlite >.separator ,  
  3. sqlite > select * from test;  
  4. sqlite >.output stdout 

从 CSV 文件导入数据到表中:

 
 
  1. sqlite >create table newtable ( id integer primary key, value text );  
  2. sqlite >.import [filename.csv ] newtable 

备份数据库:

 
 
  1. /* usage: sqlite3 [database] .dump > [filename] */  
  2. sqlite3 mytable.db .dump > backup.sql 

恢复数据库:

 
 
  1. /* usage: sqlite3 [database ] < [filename ] */  
  2. sqlite3 mytable.db < backup.sql 
结合自己的需求,写了一个简单的demo,

功能描述:建立数据库,insert函数每次插入一条数据。deldata每次从数据库中取出一条数据并删除之。


import sqlite3

class dbManage():

    def insert(self, key, value):
        conn = sqlite3.connect('test.db')
        cu = conn.cursor()
        cu.execute('create table  if not exists dataCatlog(id varchar primary key ,content varchar )')
        cu.execute('insert into dataCatlog values (?,?)', (key, value))
        cu.execute('select * from dataCatlog')
        print cu.fetchall()
        conn.commit()
        conn.close()
    def deldata(self):
        conn = sqlite3.connect('test.db')
        cu = conn.cursor()
        cu.execute('create table  if not exists dataCatlog(id varchar primary key ,content varchar )')
        cu.execute('select * from dataCatlog')
        allrecords = cu.fetchall()
        print "before del:", allrecords
        for record in allrecords:
            #record = cu.fetchone()[0]

            cu.execute("delete from dataCatlog where id = ?", (record[0],))
            cu.execute('select * from dataCatlog')
            print "after del:", cu.fetchall()
            conn.commit()
            #conn.close()
        conn.close()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值