如何将值插入 table 列?

How to insert value into table column?

我正在进入 SQL,试图将一个值插入特定的 table 列,但与我尝试提供的整个字符串相比,它似乎只想占用一个字符.

import sqlite3
import random

conn = sqlite3.connect(r"test.db")
cursor = conn.cursor()
profile_ID = str(random.randint(100,999))

'''
insert into one column
'''
cursor.execute("INSERT INTO Webstore(Running) VALUES(?)", (profile_ID))

这是出现的错误:

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.

上面的查询插入似乎只想接受一个字符而不是 profile_ID 中的三个字符,我尝试发送一个字符串,例如“string 6”,它会给我同样的错误但是在本例中为“8 supplied”(“string 6”中的 8 个字符)。

插入一个值:

cursor.execute("insert into Webstore (Running) values (%s)" % profile_ID)

插入多个值(?在这里工作没有错误):

cursor.execute("insert into Webstore (Running, Status) values (?, ?)", (profile_ID, status))

我不是使用 sqlite 的专家,但我坚信您遇到的问题与此模块无关;相反,这是一个 tuple 问题。我相信您需要更改此行:

cursor.execute("INSERT INTO Webstore(Running) VALUES(?)", (profile_ID))

到这一行:

cursor.execute("INSERT INTO Webstore(Running) VALUES(?)", (profile_ID,))

这两行之间略有不同。 (profile_ID) 已更改为 (profile_ID,)。前者可以是基于 profile_ID 值的任何东西:字符串、整数,甚至是浮点数。但后者绝对是一个元组,我相信你将需要 cursor.execute.

中的第二个变量