Reputation: 47
I'm trying to create a table in MySQL using python-flask. The table name is the 'id' parameter, which I pull from my server. I want this id to be the name of the table that I'm creating, but when I try to do so, I get the following error:
'mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '46490210 (user varchar(50), message varchar(150))' at line 1'
Here's the python-sql code for reference.
#the file name is chat_db.py
def createTable(id):
cursor = db.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS %s (user varchar(50), message varchar(150))",(id,))
db.commit()
Here's the Flask code (without the decorators):
import chat_db as roomdb
import random
def rand_id():
return random.randint(10000000,99999999)
def roomTable():
roomdb.createTable(rand_id())
roomTable()
Please help out! :)
Upvotes: 1
Views: 298
Reputation: 49410
Onlym Number aren't alowwed in mysql, so you need a Letter before the number
In my example i used the Letter A
, but it can be of course any Text you like.
But i don't see why you want to do such a thing in a loop.
def createTable(id):
cursor = cnx.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS A%s (user varchar(50), message varchar(150))",(id,))
cnx.commit()
Upvotes: 1