python - 使用 SQLite3 将元组作为参数值传递的语法错误
问题描述
我对 Python 和 SQL 完全陌生,但我正在学习一个教程,但我真的无法让它工作。请忽略糟糕的字符串结构,但我会在它工作时整理它。我正在尝试发送两个值以插入表上的新行,它们在游标执行命令中传递给它。
def update_height(conn, location, timestamp_height):
sql_update_height_table = """ INSERT INTO """ + str(location) + """_cushion(timestamp,height)
VALUES(?,?) """
print (sql_update_height_table)
cur = conn.cursor()
cur.execute(sql_update_height_table,timestamp_height)
conn.commit()
return cur.lastrowid
结果print(sql_update_height_table)
:
INSERT INTO 0xEE738a9e_height(timestamp,height)
VALUES(?,?)
这是错误:
sqlite3.OperationalError: near "0xEE738a9e": syntax error
我正在关注本教程,我真的看不出我做错了什么,我一直在寻找几个小时。
timestamp_height
是一个有两个条目的元组。
解决方案
我不知道你为什么需要使用这样的表名,但0x
通常意味着 HEX 表示。用反引号引用名称。
sql_update_height_table = """ INSERT INTO `""" + str(location) + """_cushion`(timestamp,height)
VALUES(?,?) """
推荐阅读
- android - Android数据绑定找不到包
- ios - 如何将 CBDescriptor 值转换为字符串?
- sql - 创建表,其中每一列都是从选择查询中检索到的不同值
- jquery - 我无法将工具提示定位在 div 的右上角
- mysql - MySQL,使用 find_in_set 返回行未按预期工作
- excel - VBA - Excel 2016 中单元格中多个值的复选框
- php - 在项目列表中删除php中的文件
- python - 使用朴素贝叶斯的两个字典之间的相似性概率
- c# - 如何正确地将字符串传递给ffmpeg?
- go - 如何在特定部分创建具有接口名称的文件,然后从另一个文件中解析以使用 GO 中的数据修改接口?