使用sqlite3和kivy

2024-06-28 15:01:04 发布

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

conn = sqlite3.connect('business_database.db')
c = conn.cursor()

c.execute("INSERT INTO business VALUES(self.nob_text_input.text, self.post_text_input.text, self.descrip_text_input.text )")

conn.commit()
conn.close()

我想使用kivy中的TextInput(即“self.post\u text\u input.text”等)将记录添加到我的数据库中,但出现以下错误:

OperationalError: no such column: self.nob_text_input.text
  

我尝试在查询中将列放在表名旁边:

c.execute("INSERT INTO business(column1, column2,column3) VALUES(self.nob_text_input.text....)

但我还是犯了同样的错误


Tags: textselfinputexecuteconnect错误businessconn
1条回答
网友
1楼 · 发布于 2024-06-28 15:01:04

将我的评论变成更详细的回答

如果您试图使用字符串中变量(self.nob_text_input.text和朋友)的值,则需要将这些值嵌入字符串中

一种方法是使用格式字符串:

"INSERT INTO business VALUES(%s, %s, %s)" % (self.nob_text_input.text, self.post_text_input.text, self.descrip_text_input.text)

另一种方法是连接字符串:

"INSERT INTO business VALUES(" + self.nob_text_input.text + ", " + self.post_text_input.text + ", " + self.descrip_text_input.text + ")"

相关问题 更多 >