forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite.py
More file actions
72 lines (61 loc) · 1.64 KB
/
sqlite.py
File metadata and controls
72 lines (61 loc) · 1.64 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# encoding: utf-8
__author__ = 'zhanghe'
import sqlite3
class SqLite(object):
def __init__(self, db_name):
self.conn = sqlite3.connect(db_name)
def close(self):
"""
关闭Connection
"""
self.conn.close()
def create(self):
"""
创建数据表
"""
# 创建一个Cursor:
cursor = self.conn.cursor()
# 执行一条SQL语句,创建user表:
cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
# 继续执行一条SQL语句,插入一条记录:
cursor.execute('insert into user (id, name) values (\'1\', \'Michael\')')
# 通过rowcount获得插入的行数:
print cursor.rowcount
# 关闭Cursor:
cursor.close()
# 提交事务:
self.conn.commit()
def show_tables(self):
"""
显示数据库表名
"""
cursor = self.conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
result = cursor.fetchall()
cursor.close()
print result
return result
def get_row(self):
"""
获取多行数据
:return:
"""
cursor = self.conn.cursor()
# 执行查询语句:
cursor.execute('select * from user where id=?', '1')
# 获得查询结果集:
values = cursor.fetchall()
cursor.close()
print values
return values
def test():
"""
测试
"""
db = SqLite('test.db')
# db.create()
db.show_tables()
db.get_row()
db.close()
if __name__ == '__main__':
test()