使用时:
import datetime
import sqlite3
db = sqlite3.connect('mydb.sqlite', detect_types=sqlite3.PARSE_DECLTYPES)
c = db.cursor()
db.text_factory = str
c.execute('create table if not exists mytable (date timestamp, title str, \
custom str, x float, y float, z char default null, \
postdate timestamp default null, id integer primary key autoincrement, \
url text default null)')
c.execute('insert into mytable values(?, ?, ?, ?, ?)', \
(datetime.datetime(2018,4,23,23,00), 'Test', 'Test2', 2.1, 11.1))
我有:
sqlite3.OperationalError:table mytable有9列,但提供了5个值
Why doesn't SQlite take default values (specified during table creation) in consideration to populate a new row?
(另外,当我在sqlite3 doc中找到数据类型 str
, char
时,它仍然相关吗?)
2 回答
因为您说要通过不指定特定列来插入所有列 .
改变
'insert into mytable values(?, ?, ?, ?, ?)'
到
'insert into mytable (date, title, custom, x, y) values(?, ?, ?, ?, ?)'
实际上,可以指定列类型的任何值,该值将遵循一组规则并转换为TEXT,INTEGER,REAL,NUMERIC或BLOB . 但是,您可以在任何列中存储任何类型的值 .
STR将解析为NUMERIC,
TIMESTAMP将解析为NUMERIC,
FLOAT将解析为REAL,
CHAR到TEXT .
阅读Datatypes In SQLite或者看看How flexible/restricive are SQLite column types?
如果您只想为某些列提供值,则需要指定哪些列 . 否则引擎将不知道放在哪里 . 这条线需要改变:
对此: