跳转至

sqlite3

连接数据库

# 若不存在则会创建
conn = sqlite3.connect('example.db')


创建游标

c = conn.cursor()


创建表单

c.execute('''CREATE TABLE IF NOT EXISTS table_name
             (date text, trans text, symbol text, qty real, price real)''')


插入数据

c.execute('''INSERT INTO table_name VALUES
             ('2006-01-05', 'BUY', 'RHAT', 100, 35.14)''')

sql = "INSERT INTO table_name VALUES (?,?,?,?,?)"
c.execute(sql, (data, trans, symbol, qty, price))


查询数据

for row in c.execute('SELECT * FROM table_name ORDER BY price'):
    print(row)


提交并保存

conn.commit()
conn.close()