首页 > 解决方案 > 使用 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是一个有两个条目的元组。

标签: pythonsqlite

解决方案


我不知道你为什么需要使用这样的表名,但0x通常意味着 HEX 表示。用反引号引用名称。

sql_update_height_table = """ INSERT INTO `""" + str(location) + """_cushion`(timestamp,height)
                VALUES(?,?) """

推荐阅读