我在sqlite3中创建了一个表,结构如下:
cid name type notnull pk
0 wheelId INTEGER 1 1
1 wheelName VARCHAR 1 0
2 paraDiam INTEGER 0 0
3 paraWidth FLOAT 0 0
4 paraEt FLOAT 0 0
5 paraHole INTEGER 0 0
6 factParaHole INTEGER 0 0
7 paraCenterhole FLOAT 0 0
8 wheelColorName VARCHAR 0 0
9 wheelColorNameShow TEXT 0 0
10 paraWeight FLOAT 0 0
11 vpPrice INTEGER 0 0
12 ntStock INTEGER 0 0
13 wheelURL VARCHAR 0 0
14 wheelImg VARCHAR 0 0
15 wheelRank INTEGER 0 0
现在,我使用python在表中插入一条记录,我只想插入列值'wheelName','wheelImg'并保持其他列为null,(第一个是wheelId
,我希望它能自动增量。)
我几乎使用了我在网站上找到的所有方法:
cursor.execute("""INSERT INTO WHEELS (wheelName,wheelImg) VALUES (%s,%s)""" ,
(WHEELNAME,WHEELIMG))
Python抛出sqlite3.OperationalError: near "%": syntax error
cursor.execute("""INSERT INTO WHEELS(WHEELID,WHEELNAME, PARADIAM, PARAWIDTH, PARAET,PARAHOLE,FACTPARAHOLE,
PARACENTERHOLE,WHEELCOLORNAME,WHEELCOLORNAMESHOW,PARAWEIGHT,VPPRICE,NTSTOCK,
WHEELURL,WHEELIMG,WHEELRANK)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""" ,
(WHEELID, WHEELNAME, PARADIAM, PARAWIDTH, PARAET,PARAHOLE,FACTPARAHOLE,
PARACENTERHOLE,WHEELCOLORNAME,WHEELCOLORNAMESHOW,PARAWEIGHT,VPPRICE,NTSTOCK,
WHEELURL,WHEELIMG,WHEELRANK))
我将其他值设置为None
像:
PARADIAM = None
PARAWIDTH = None
PARAET = None
PARAHOLE = None
和python再次抛出sqlite3.OperationalError: near "%": syntax error
。
答案 0 :(得分:2)
sqlite3
适配器使用?
表示SQL参数,而不是%s
:
cursor.execute("INSERT INTO WHEELS (wheelName,wheelImg) VALUES (?, ?)", (WHEELNAME,WHEELIMG))
您的架构还限制wheelColorNameShow
:
"wheelColorNameShow" TEXT check(typeof("wheelColorNameShow") = 'text')
这意味着它不能再为NULL。您 也包含该列的值:
cursor.execute('insert into wheels (wheelName,wheelImg,wheelColorNameShow) VALUES (?, ?, ?)', ('test', 'test', 'test'))
成功。