Python模块Sqlite数据库模块简介
从Python2.5以后的版本开始引入SQLite,属于Python内置模块,不需要额外安装。Sqlite以小巧快捷著称。SQLite第一个Alpha版本诞生于2000年5月. 至今已经有很多年了,SQLite也迎来了一个版本 SQLite 3已经发布。
Python模块Sqlite数据库模块常用方法简介
1. 创建/打开数据库
在调用connect函数的时候,指定库名称,如果指定的数据库存在就直接打开这个数据库,如果不存在就创建数据库再打开。
Python
#在文件中创建数据库
cx = sqlite3.connect("E:/test.db")
1
2
#在文件中创建数据库
cx=sqlite3.connect("E:/test.db")
2.数据库连接对象
开数据库时返回的对象cx就是一个数据库连接对象,它可以有以下操作:
Python
commit()--事务提交
rollback()--事务回滚
close()--关闭一个数据库连接
cursor()--创建一个游标
1
2
3
4
commit()--事务提交
rollback()--事务回滚
close()--关闭一个数据库连接
cursor()--创建一个游标
关于commit(),如果isolation_level隔离级别默认,那么每次对数据库的操作,都需要使用该命令,你也可以设置isolation_level=None,这样就变为自动提交模式。
3.使用游标查询数据库
我们需要使用游标对象SQL语句查询数据库,获得查询对象。 通过以下方法来定义一个游标。
Python
cu=cx.cursor()
1
cu=cx.cursor()
游标对象有以下的操作:
Python
execute()--执行sql语句
executemany--执行多条sql语句
close()--关闭游标
fetchone()--从结果中取一条记录,并将游标指向下一条记录
fetchmany()--从结果中取多条记录
fetchall()--从结果中取出所有记录
scroll()--游标滚动
1
2
3
4
5
6
7
execute()--执行sql语句
executemany--执行多条sql语句
close()--关闭游标
fetchone()--从结果中取一条记录,并将游标指向下一条记录
fetchmany()--从结果中取多条记录
fetchall()--从结果中取出所有记录
scroll()--游标滚动
4. 建表
cu.execute(“create table table_py40 (id integer primary key,pid integer,name varchar(10) UNIQUE,nickname text NULL)”)
上面语句创建了一个叫table_py40的表,它有一个主键id,一个pid,和一个name,name是不可以重复的,以及一个nickname默认为NULL。
5. 插入数据
Python
#请注意避免以下写法:
# Never do this -- insecure 会导致注入攻击
pid=200
c.execute("... where pid = '%s'" % pid)
#正确的做法如下,如果t只是单个数值,也要采用t=(n,)的形式,因为元组是不可变的。
for t in[(0,10,'abc','Yu'),(1,20,'cba','Xu')]:
cx.execute("insert into table_py40 values (?,?,?,?)", t)
#简单的插入两行数据,不过需要提醒的是,只有提交了之后,才能生效.我们使用数据库连接对象cx来进行提交commit和回滚rollback操作.
cx.commit()
1
2
3
4
5
6
7
8
9
10
11
#请注意避免以下写法:
# Never do this -- insecure 会导致注入攻击
pid=200
c.execute("... where pid = '%s'"%pid)
#正确的做法如下,如果t只是单个数值,也要采用t=(n,)的形式,因为元组是不可变的。
fortin[(0,10,'abc','Yu'),(1,20,'cba','Xu')]:
cx.execute("insert into table_py40 values (?,?,?,?)",t)
#简单的插入两行数据,不过需要提醒的是,只有提交了之后,才能生效.我们使用数据库连接对象cx来进行提交commit和回滚rollback操作.
cx.commit()
6.查询
Python
cu.execute("select * from table_py40")
1
cu.execute("select * from table_py40")
要获取查询到的数据,使用游标的fetch函数,如:
Python
In [10]: cu.fetchall()
Out[10]: [(0, 10, u'abc', u'Yu'), (1, 20, u'cba', u'Xu')]
1
2
In[10]:cu.fetchall()
Out[10]:[(0,10,u'abc',u'Yu'),(1,20,u'cba',u'Xu')]
如果我们使用cu.fetchone(),则首先返回列表中的第一项,再次使用,则返回第二项,依次下去.
7.修改
Python
In [12]: cu.execute("update table_py40 set name='Boy' where id = 0")
In [13]: cx.commit()
1
2
In[12]:cu.execute("update table_py40 set name='Boy' where id = 0")
In[13]:cx.commit()
注意,修改数据以后commit提交
8.删除
Python
cu.execute("delete from table_py40 where id = 1")
cx.commit()
1
2
cu.execute("delete from table_py40 where id = 1")
cx.commit()
9.使用中文
请先确定你的IDE或者系统默认编码是utf-8,并且在中文前加上u
Python
x=u'鱼'
cu.execute("update table_py40 set name=? where id = 0",x)
cu.execute("select * from table_py40")
cu.fetchall()
[(0, 10, u'\u9c7c', u'Yu'), (1, 20, u'cba', u'Xu')]
1
2
3
4
5
x=u'鱼'
cu.execute("update table_py40 set name=? where id = 0",x)
cu.execute("select * from table_py40")
cu.fetchall()
[(0,10,u'\u9c7c',u'Yu'),(1,20,u'cba',u'Xu')]
如果要显示出中文字体,那需要依次打印出每个字符串
Python
In [26]: for item in cu.fetchall():
....: for element in item:
....: print element,
....:
0 10 鱼 Yu
1 20 cba Xu
1
2
3
4
5
6
7
In[26]:foritemincu.fetchall():
....:forelementinitem:
....:printelement,
....:
010鱼Yu
120cbaXu