-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_test.py
More file actions
50 lines (40 loc) · 1.23 KB
/
db_test.py
File metadata and controls
50 lines (40 loc) · 1.23 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
__author__ = 'jet'
#encoding=utf-8
# 甄码农代码 2012 03 06
# 打开sqlite3内存数据库,执行操作,将内存数据库保存到文件
import sqlite3
from io import StringIO
#使用:memory:标识打开的是内存数据库
con = sqlite3.connect(":memory:")
cur = con.cursor()
#使用executescript可以执行多个脚本
cur.executescript("""
create table quotes(
rid INTEGER PRIMARY KEY AUTOINCREMENT,
code char(10) NOT NULL,
high real,
open real,
low real,
close real,
amount real,
volume real)""")
#execute执行脚本,参数要放到元组中
cur.execute('insert into quotes(code,high,open,low,close,amount,volume) values(?,?,?,?,?,?,?)',
('600036',12.0,11.8,11.7,11.9,999999,8999))
#打印数据表数据
cur.execute("select * from quotes")
print(cur.fetchall())
#生成内存数据库脚本
str_buffer = StringIO()
#con.itrdump() dump all sqls
for line in con.iterdump():
str_buffer.write('%s\n' % line)
#关闭内存数据库
cur.close()
#打开文件数据库
con_file = sqlite3.connect('quotes.db3')
cur_file = con_file.cursor()
#执行内存数据库脚本
cur_file.executescript(str_buffer.getvalue())
#关闭文件数据库
cur_file.close()