kjubus kjubus - 11 months ago 49
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

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

File "XXX", line 55, in FUNCTIONNAME

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 Source

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