0

I have this code in python 2.4:

import datetime, time, sqlite
def metodo(date):
        conn = sqlite.connect('dataBase.db')
        c = conn.cursor()
        c.execute("INSERT INTO f values(?,?)", (3,date))
        conn.commit()
        conn.close()

end_date=datetime.datetime(2012, 10, 21, 0, 0)
print end_date

metodo(end_date)

and a sqlite database with int and TIMESTAMP

create table f(id int, time TIMESTAMP)

but i obtain this in python:

Traceback (most recent call last):
File "prueba.py", line 13, in ?
metodo(end_date)
File "prueba.py", line 6, in metodo
c.execute("INSERT INTO f values(?,?)", (3,date))
File "/usr/lib/python2.4/site-packages/sqlite/main.py", line 255, in execute
self.rs = self.con.db.execute(SQL % parms)
TypeError: not all arguments converted during string formatting

This code run well. The problem is with timestamp:

import datetime, time, sqlite
def metodo(date):
        conn = sqlite.connect('dataBase.db')
        c = conn.cursor()
        c.execute("INSERT INTO a values(%s)", date)
        conn.commit()
        conn.close()


metodo("stringTest")

and a sqlite database with int and TIMESTAMP

create table a(test text)
2
  • what happens if you convert it to UNIX time before passing it to SqLite? Commented May 14, 2013 at 22:22
  • There is no sqlite module on Python 2.4 by default. The first snippet works fine with sqlite3 (from stdlib) and Python 2.7. Commented May 16, 2013 at 0:49

1 Answer 1

1

I think you should parameterize it like this:

def metodo(date):
        conn = sqlite.connect('dataBase.db')
        c = conn.cursor()
        c.execute("INSERT INTO a values(%s)", (date,))
        conn.commit()
        conn.close()
Sign up to request clarification or add additional context in comments.

1 Comment

sqlite3 uses '?' parameter holders.

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.