我想使用序列在数据库中创建多个表
我的代码是:
import sqlite3
con = sqlite3.connect('d:/db.sqlite')
c = con.cursor()
stations = [("station1"), ("station2"), ("station3"]
c.executemany("CREATE TABLE ? ('id_station' integer NOT NULL, 'name' varchar NOT NULL, 'date' varchar NOT NULL", stations)
我有:IndentationError:意外缩进
我做错了什么?
这就是我要做的,假设我想创建一个 test 表:
qry = """
CREATE TABLE IF NOT EXISTS test (
id_station INTEGER PRIMARY KEY,
name VARCHAR NOT NULL,
DATE DATE NOT NULL DEFAULT CURRENT_DATE
)
"""
c.execute(qry)
这里我定义了主键以及日期字段的默认值,
stations = [("station1",), ("station2",), ("station3",)] # Add comma on each tuple
c.executemany("INSERT INTO test (name) values (?)", stations)
注意这里的元组格式,确保末尾有逗号:("station1",)
c.execute("SELECT * FROM test")
for e in c.fetchall():
print(e)
输出:
(1, 'station1', '2021-06-03')
(2, 'station2', '2021-06-03')
(3, 'station3', '2021-06-03')