如何用python在sqlite3中插入空值

2024-10-01 04:44:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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'并保持其他列为空(第一列是wheelId,我希望它能自动递增)。在

我使用了网站上几乎所有的方法:

  1. cursor.execute("""INSERT INTO WHEELS (wheelName,wheelImg) VALUES (%s,%s)""" , (WHEELNAME,WHEELIMG))

    Python抛出sqlite3.OperationalError: near "%": syntax error

  2. 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。在


Tags: noneintegerfloatsqlite3cursorvarcharwheelnamewheelid
1条回答
网友
1楼 · 发布于 2024-10-01 04:44:54

sqlite3适配器使用?作为SQL参数,而不是%s

cursor.execute("INSERT INTO WHEELS (wheelName,wheelImg) VALUES (?, ?)", (WHEELNAME,WHEELIMG))

您的架构还限制wheelColorNameShow

^{pr2}$

这意味着它不能再为空。您还需要为该列包含一个值:

cursor.execute('insert into wheels (wheelName,wheelImg,wheelColorNameShow) VALUES (?, ?, ?)', ('test', 'test', 'test'))

成功。在

相关问题 更多 >