Skip to content

Commit ae76ff9

Browse files
committed
添加数据库编程.py
1 parent 0b97157 commit ae76ff9

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

.settings/org.eclipse.core.resources.prefs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ encoding/\u5206\u5E03\u5F0F\u8FDB\u7A0B.py=utf-8
77
encoding/\u591A\u7EBF\u7A0B.py=utf-8
88
encoding/\u591A\u8FDB\u7A0B.py=utf-8
99
encoding/\u5E8F\u5217\u5316.py=utf-8
10+
encoding/\u6570\u636E\u5E93\u7F16\u7A0B.py=utf-8
1011
encoding/\u6B63\u5219\u8868\u8FBE\u5F0F.py=utf-8
1112
encoding/\u9519\u8BEF_\u8C03\u8BD5\u548C\u6D4B\u8BD5.py=utf-8
1213
encoding/\u9762\u5411\u5BF9\u8C61.py=utf-8

数据库编程.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
type = 'sqlite3'
5+
6+
if type == 'sqlite3':
7+
# 导入驱动
8+
import sqlite3
9+
# 连接到数据库 test.db 如果文件不存在,则会自动创建
10+
conn = sqlite3.connect('test.db')
11+
# 创建一个cursor
12+
cursor = conn.cursor()
13+
# 执行创建表
14+
cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
15+
# 执行插入数据
16+
cursor.execute("insert into user (id, name) values ('1', 'wangh')")
17+
# 通过rowcount获取影响的行数
18+
print cursor.rowcount
19+
# 关闭cursor
20+
cursor.close()
21+
# 提交事物
22+
conn.commit()
23+
# 关闭connection
24+
conn.close()
25+
26+
conn1 = sqlite3.connect('test.db')
27+
cursor1 = conn1.cursor()
28+
# 执行查询语句
29+
cursor1.execute("select * from user where id=?", '1')
30+
# 获取结果集
31+
value = cursor1.fetchall()
32+
print value
33+
cursor1.close()
34+
conn1.close()
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+

0 commit comments

Comments
 (0)