python - 插入在带有 Python 的 Sqlite 中不起作用

标签 python sqlite insert

我编写了以下代码:

for device_index in range(total_devices):
    slave_address = device_index+1
    offset = device_index * 10
    for i in range(registers_per_device):
        register = offset + i
        if i==0 :
            values = ('h', register, 85)
        elif i==1 :
            values = ['h', register, 170]
        elif i==2 :
            values = ['h', register, 34]
        elif i==3 :
            values = ['h', register, 1]
        elif i==4 :
            values = ['h', register, 0]
        elif i==5 :
            values = ['h', register, 200]
        elif i==6 :
            values = ['h', register, 200]
        elif i==7 :
            values = ['h', register, slave_address]
        elif i==8 :
            values = ['h', register, 0]
        elif i==9 :
            values = ['h', register, 0]

        c.execute('''INSERT INTO deneme(type, index, value) VALUES(?,?,?)''', values)

        conn.commit()

当我运行此代码时,出现错误:

Traceback (most recent call last): File "C:\Users\SanKontrol\Desktop\initialize_db.py", line 41, in c.execute('''INSERT INTO deneme(type, index, value) VALUES(?,?,?)''', values) OperationalError: near "index": syntax error

我尝试了很多变体,但仍然无法找出问题所在。

最佳答案

INDEX 是 SQL 中的保留字。您必须适本地转义它才能使用它。

'''..., `index`, ...'''

关于python - 插入在带有 Python 的 Sqlite 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40643456/

相关文章:

android - Sqlite 数据库 onCreate 参数

ios - 使用 iCloud 进行应用程序数据库备份,但不同步

mysql - 是否可以在 INSERT ON DUPLICATE ON DUPLICATE KEY UPDATE 中使用 'join'

c++ - AVL 树插入 - 访问冲突读取位置(无法读取内存)

python - 如何从本身就是超链接的 href 中获取 URL?

Python NLTK : parse sentence using conjoint structure, 进入无限递归

Java线程安全的Sqlite?

php - 记录插入和输出取决于它的值

python - matplotlib 中的 100% 堆积面积/直方图,X 轴上有日期

python - 在缺少条目的特定列变量上合并两个表/数组 (*.xlsx) 文件