将记录添加到内存数据库只需要一次。
示例代码:
import sqlite3
# Connect to in-memory database
conn = sqlite3.connect(':memory:')
# Create table
conn.execute('''CREATE TABLE stocks
(date text, trans text, symbol text, qty real, price real)''')
# Adding records to the table
data = [('2019-01-05', 'BUY', 'RHAT', 100, 35.14),
('2019-02-03', 'SELL', 'IBM', 200, 157.79)]
conn.executemany("INSERT INTO stocks VALUES (?,?,?,?,?)", data)
# Commit the changes
conn.commit()
# Retrieve data from the table
cursor = conn.execute("SELECT * FROM stocks")
for row in cursor:
print(row)
# Output:
# ('2019-01-05', 'BUY', 'RHAT', 100.0, 35.14)
# ('2019-02-03', 'SELL', 'IBM', 200.0, 157.79)