我是靠谱客的博主 敏感小霸王,这篇文章主要介绍python实现sqlite的基本操作,现在分享给大家,希望可以做个参考。

Python sqlite3数据库是一款非常小巧的内置模块,它使用一个文件存储整个数据库,操作十分方便,相比其他大型数据库来说,确实有些差距。但是在性能表现上并不逊色,麻雀虽小,五脏俱全,sqlite3实现了多少sql-92标准,比如说transaction、trigger和复杂的查询等。

描述

  Python的数据库模块有统一的接口标准,所以数据库操作都有统一的模式(假设数据库模块名为db):

  1. 用db.connect创建数据库连接,假设连接对象为conn

  2. 如果该数据库操作不需要返回结果,就直接使用conn.execute查询,根据数据库事物隔离级别的不同,可能修改数据库需要conn.commit

  3. 如果需要返回查询结果则用conn.cursor创建游标对象cur,通过cur.execute查询数据库,cursor方法有fetchall、fetchone、fetchmany返回查询结果,根据数据库事物隔离级别不同,可能修改数据库需要coon.commit

  4. 关闭cur.close

sqlite3基本操作用例

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#coding=utf-8 import sqlite3 conn = sqlite3.connect("sqlite.db") #创建sqlite.db数据库 print ("open database success") conn.execute("drop table IF EXISTS student") query = """create table IF NOT EXISTS student( customer VARCHAR(20), produce VARCHAR(40), amount FLOAT, date DATE );""" conn.execute(query) print ("Table created successfully") #在表中插入数据 ''' 方法1 ''' #data = '''INSERT INTO student(customer,produce,amount,date) # VALUES("zhangsan","notepad",999,"2017-01-02")''' #conn.execute(data) #data = '''INSERT INTO student(customer,produce,amount,date) # VALUES("lishi","binder",3.45,"2017-04-05")''' #conn.execute(data) #conn.commit() ''' 方法2 ''' statement = "INSERT INTO student VALUES(?,?,?,?)" data = [("zhangsan","notepad",999,"2017-01-02"),("lishi","binder",3.45,"2017-04-05")] conn.executemany(statement, data) conn.commit() curson = conn.execute("select * from student") conn.commit() print (curson) rows = curson.fetchall() print (rows) conn.close()

sqlite3 csv->db->csv

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'''将csv数据导入数据库''' import sys import csv import sqlite3 #解析csv文件 def parsecsvFile(filepath): header = None data = [] with open(filepath, 'r') as csvfile: filereader = csv.reader(csvfile) header = next(filereader) #print (header) for row in filereader: data.append(row) #print (data) return header,data #使用sqlite3写数据库 def initdb(header, data): conn = sqlite3.connect("sqlite.db") print ("connect database success") conn.execute("drop table IF EXISTS student") conn.commit() query = '''create table IF NOT EXISTS student( Supplier Name VARCHAR(32), Invoice Number VARCHAR(16), Part Number VARCHAR(16), Cost VARCHAR(16), Purchase Date DATE);''' conn.execute(query) conn.commit() statement = "INSERT INTO student VALUES(?,?,?,?,?)" conn.executemany(statement, data) conn.commit() curson = conn.execute("select * from student") conn.commit() print (curson) rows = curson.fetchall() print (rows) conn.close() return rows #根据数据库内容写csv文件 def wirtecsvfile(writefilepath, header, data): with open(writefilepath, 'a+') as writefile: writer = csv.writer(writefile, delimiter=",") writer.writerow(header) for row in data: writer.writerow(row) if __name__ == "__main__": readfilepath = sys.argv[1] writefilepath = sys.argv[2] header,data = parsecsvFile(readfilepath) rows = initdb(header, data) wirtecsvfile(writefilepath, header, rows)

最后

以上就是敏感小霸王最近收集整理的关于python实现sqlite的基本操作的全部内容,更多相关python实现sqlite内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部