kjubus kjubus - 3 months ago 8
SQL Question

Can't create an SQL Table in Python using input as name

I have a little issue with my python code. I want to take the input from user and based on that create an SQL table with that name. I take the input in table_name variable and want to do it so:

sql = "CREATE TABLE %s" % table_name
cur.execute(sql)


Let's say I run it with input "books". I get this error:


File "XXX", line 55, in FUNCTIONNAME
cur.execute(sql)

sqlite3.OperationalError: near "books": syntax error


Any ideas on what is wrong? I googled it and above code should be OK, but it isn't...

Answer

That's most probably cause you are not passing the table definition (column names and their type , size). What I see from posted code is that you are passing only table name and so your query becomes below which is totally wrong

CREATE TABLE books