0
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.

2 Answers 2

1

"INDEX" is a reserved word in SQL.

INSERT INTO `Index` VALUES ...
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.