0

I'm trying to place a variable inside an SQLite query, I've read the documentation, I've read similar questions here and articles on Internet, but I keep getting the same two errors, when I hace this code:

import sqlite3
import sys
import time

user_id = (int(time.time()))

db_conn = sqlite3.connect('data.db')
db_curs = db_conn.cursor()

db_curs.execute('insert into Users values(?, "John Doe", "johndoe@yahoo.com", "52g5f2gf5gfg2f45f4f")', user_id)
db_curs.execute('select * from Users')
for registry in db_curs.fetchall():
    print(registry)

db_conn.commit()

db_curs.close()
db_conn.close()

I get the following error in the line where I'm trying to insert the data:

ValueError: parameters are of unsupported type

If I convert the value of user_id to a string, like this:

user_id = (str(int(time.time())))

I get the following error:

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 10 supplied.

Notice that the time since EPOCH is 10 numbers lenght, so I suppose there's the problem, obviously, but I don't know what it is.

Andrés Orozco
  • 2,490
  • 5
  • 32
  • 48
  • `execute` expects a sequence of arguments for placeholders as the second argument, but instead you're passing a single value. Converting it to a string makes a sequence out of it, which is also wrong. Wrap the argument in a tuple or a list. – Ilja Everilä Nov 25 '16 at 21:42

1 Answers1

0

The second parameter to execute needs to be a tuple:

db_curs.execute('insert into Users values(?, "John Doe", "johndoe@yahoo.com", "52g5f2gf5gfg2f45f4f")', (user_id,))
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I've discovered that I didn't noticed something in the documentation, I just need to add a coma to the tuple, like this: **user_id = (int(time.time()),)** – Andrés Orozco Nov 25 '16 at 21:45