forked from techstay/python-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite.py
More file actions
48 lines (37 loc) · 1 KB
/
sqlite.py
File metadata and controls
48 lines (37 loc) · 1 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
import sqlite3
db_file = 'test.db'
memory_db_file = ':memory:'
create_table_sql = '''\
CREATE TABLE test(
name VARCHAR(255) PRIMARY KEY ,
value VARCHAR(255) NOT NULL
)
'''
insert_table_sql = """\
INSERT INTO test VALUES(?,?)
"""
query_table_sql = """\
SELECT *
FROM test WHERE `name`=?
"""
delete_table_sql = """\
DROP TABLE test
"""
print('--------------sqlite3--------------')
print(f'version:{sqlite3.version}')
print(f'sqlite_version:{sqlite3.sqlite_version}')
with sqlite3.connect(memory_db_file) as connection:
try:
cursor = connection.cursor()
cursor.execute(create_table_sql)
cursor.execute(insert_table_sql, ('name', 'yitian'))
cursor.execute(insert_table_sql, ('count', '100'))
cursor.execute(query_table_sql, ('name',))
name = cursor.fetchone()
print(name)
cursor.execute(query_table_sql, ('count',))
count = cursor.fetchone()
print(count)
cursor.execute(delete_table_sql)
finally:
cursor.close()