12345
12345

Reputation: 35

SQLwith python OperationalError (sqlite)

import sqlite3

conn = sqlite3.connect('MyIndex.db')

c = conn.cursor()

def insert(t, f, d):
    with conn:
        c.execute("INSERT INTO Index VALUES (:t, :frequency, :docID)", {'t': t, 'f': f, 'd': d})


insert('apple', 1, '1/2')

conn.close()

I am using SQLite to build a simple database. I have trouble with the insert function. It keeps showing that:

sqlite3.OperationalError: near "Index": syntax error

But I failed to find any syntax error in my code. Can someone tell me how to fix it? Thanks in advance.

Upvotes: 0

Views: 84

Answers (2)

IAmGroot
IAmGroot

Reputation: 91

Try putting Index in single quotes like this:

c.execute("INSERT INTO 'Index' VALUES (:token, :frequency, :docID)", {'token': token, 'frequency': frequency, 'docID': docID})

Index is a reserved keyword.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799300

"INDEX" is a reserved word in SQL.

INSERT INTO `Index` VALUES ...

Upvotes: 1

Related Questions