0

I am trying to insert some data into a sqlite database by following : -

param = '("593863695396044801","Ivan F. Siahaan","307783731","None","65","83","Thu Apr 30 19:45:13 +0000 2015","xyz")'

>>> conn.execute("INSERT INTO data "+param) Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.OperationalError: near ")": syntax error

I initialized connection to database as follows :-

from sqlite3 import dbapi2 as sqlite

conn = sqlite.connect('dataStore')

What wrong I am doing here ?

1 Answer 1

1

You are getting your SQL syntax wrong; both the quoting and the INSERT statement syntax are incorrect.

SQLite doesn't support double quotes around string values, only single quotes. From the SQLite keywords documenation:

'keyword' A keyword in single quotes is a string literal.
"keyword" A keyword in double-quotes is an identifier.

You are using double quotes, but for string literals you need to use single quotes instead.

You are also missing the VALUES keyword; see the INSERT statement documentation; the pattern is INSERT INTO <table> VALUES (<value list>).

You really want to leave quoting to the database itself, however. Use SQL parameters here:

statement = "INSERT INTO data VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
params = (593863695396044801, "Ivan F. Siahaan", 307783731, None, 65, 83, "Thu Apr 30 19:45:13 +0000 2015", "xyz")
conn.execute(statement, params)

Note that this would also let you use the Python None singleton to insert a NULL in the database.

Sign up to request clarification or add additional context in comments.

3 Comments

I modified param as param = "('593863695396044801','Ivan F. Siahaan','307783731','None','65','83','Thu Apr 30 19:45:13 +0000 2015','xyz')" But it still produces same error ..
@AkashRana: you are missing VALUES as well.
Strangely I can accept your answer only after 8 minutes .. :)

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.