Python sqlite row insert

I am trying to insert the string received as an argument in sqlite db using python:

def addUser(self, name): cursor=self.conn.cursor() t = (name) cursor.execute("INSERT INTO users ( unique_key, name, is_online, translate) VALUES (NULL, ?, 1, 0);", t) self.conn.commit() 

I do not want to use string concatenation, because http://docs.python.org/library/sqlite3.html advises against it.

However, when I run the code, I get an exception

 cursor.execute("INSERT INTO users ( unique_key, name, is_online, translate) VALUES (NULL, ?, 1, 0);", t) pysqlite2.dbapi2.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 7 supplied 

Why is Python splitting a string into characters, and is there a way to prevent this?

EDIT:

changing to t = (name,) gives the following exception

 print "INSERT INTO users ( unique_key, name, is_online, translate) VALUES (NULL, ?, 1, 0)" + t exceptions.TypeError: cannot concatenate 'str' and 'tuple' objects 
+6
source share
2 answers

You need the following:

 t = (name,) 

to make a singleton tuple.

Remember, commas that make a tuple, not brackets!

+8
source

Your variable t not a tuple, I think it is a string with a length of 7 lines. To make a tuple, do not forget to put the final coma:

 t = (name,) 
+1
source

Source: https://habr.com/ru/post/894393/


All Articles