sqlite3
sqlite3
说来惭愧,很早的时候就自学了python,由于工作的原因,很久都没有再用python,甚至是用来写脚本。前段时间,突然又对django框架,心血来潮,所以,又想再拾起来Python。近几天,我在看B站有几个up主,用python来做一些工具,有的甚至是用全中文的变量。中文编程,一直是我比较向往的。翻看自己的笔记,却发现没有一篇Python的文章。特从本文开始记录。
参考资料
https://www.cnblogs.com/nori/p/9967716.html
SQLite是一种嵌入式数据库,它的数据库就是一个文件。由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成。
Python就内置了SQLite3,所以,在Python中使用SQLite,不需要安装任何东西,直接使用。
在使用SQLite前,我们先要搞清楚几个概念:
表是数据库中存放关系数据的集合,一个数据库里面通常都包含多个表,比如学生的表,班级的表,学校的表,等等。表和表之间通过外键关联。
要操作关系数据库,首先需要连接到数据库,一个数据库连接称为Connection;
连接到数据库后,需要打开游标,称之为Cursor,通过Cursor执行SQL语句,然后,获得执行结果。
Python定义了一套操作数据库的API接口,任何数据库要连接到Python,只需要提供符合Python标准的数据库驱动即可。
由于SQLite的驱动内置在Python标准库中,所以我们可以直接来操作SQLite数据库。
代码示例
1 #导入数据库驱动
2 import sqlite3
3
4 #连接到数据库
5 #数据库文件是“test.db”
6 #如果数据库不存在的话,将会自动创建一个 数据库
7 conn = sqlite3.connect("test.db")
8
9 #创建一个游标 curson
10 cursor = conn.cursor()
11
12 #执行一条语句,创建 user表
13 # sql = "create table login (id varchar(20) primary key, name varchar(30), password varchar(30))"
14 # cursor.execute(sql)
15
16
17 #插入一条记录
18 #sql = "insert into login (name, password) values (\'love\', \'520520')"
19 #cursor.execute(sql)
20
21
22 #查询一条记录:
23 # sql = "select * from login"
24 # cursor.execute(sql)
25 # sql = "select * from login where id=?"
26 # cursor.execute(sql, ("2",))
27
28
29 #获取查询结果:
30 # values = cursor.fetchall()
31 #
32 # print(values)
33
34
35 # 通过rowcount获得插入的行数:
36 #cursor.rowcount()
37
38 #关闭游标:
39 cursor.close()
40
41 #提交事物
42 conn.commit()
43
44 #关闭连接
45 conn.close()
主要有两个对象,conn跟cursor,其他还有一大堆的错误对象。
Connection
backup(...)
备份数据库. Non-standard.
close(...)
关闭连接
commit(...)
Commit the current transaction.
create_aggregate(...)
Creates a new aggregate. Non-standard.
create_collation(...)
Creates a collation function. Non-standard.
create_function(...)
Creates a new function. Non-standard.
cursor(...)
Return a cursor for the connection.
enable_load_extension(...)
Enable dynamic loading of SQLite extension modules. Non-standard.
execute(...)
Executes a SQL statement. Non-standard.
executemany(...)
Repeatedly executes a SQL statement. Non-standard.
executescript(...)
Executes a multiple SQL statements at once. Non-standard.
interrupt(...)
Abort any pending database operation. Non-standard.
iterdump(...)
Returns iterator to the dump of the database in an SQL text format. Non-standard.
load_extension(...)
Load SQLite extension module. Non-standard.
rollback(...)
Roll back the current transaction.
set_authorizer(...)
Sets authorizer callback. Non-standard.
set_progress_handler(...)
Sets progress handler callback. Non-standard.
set_trace_callback(...)
Cursor
close(...)
Closes the cursor.
execute(...)
Executes a SQL statement.
executemany(...)
Repeatedly executes a SQL statement.
executescript(...)
Executes a multiple SQL statements at once. Non-standard.
fetchall(...)
Fetches all rows from the resultset.
fetchmany(...)
Fetches several rows from the resultset.
fetchone(...)
Fetches one row from the resultset.
setinputsizes(...)
Required by DB-API. Does nothing in pysqlite.
setoutputsize(...)
Required by DB-API. Does nothing in pysqlite.
报错处理
- 元组表达式使用错误
sql = 'select url from news where url=? ;'
cursor.execute(sql,(链接))
print(cursor.fetchone())
如下报错:
cursor.execute(sql,(链接))
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 13 supplied.
处理方式:
cursor.execute(sql,(链接,))
cursor.execute(sql,[链接]) # 或
因为只有一个,不能正确的识别为元组。 没有逗号,
(链接)只是一个分组表达式,而不是一个元组,因此该链接字符串被视为输入序列。如果该字符串的长度为15个字符,那么Python会将其视为15个单独的绑定值,每个绑定值长。