I am trying to write a function to create a table from user inputs (written in python3.10). Here is the code I have:
`def getDatabaseCursor(host, username, password, database):
global mydb
mydb = mysql.connector.connect(
host=host,
user=username,
password=password,
database=database
)
global mycursor
mycursor = mydb.cursor()
Classes.sqlCursor(mycursor, mydb)
print("\nTables in database:")
mycursor.execute("SHOW TABLES")
for x in mycursor:
print('-'+x[0])
print()
print("Please choose a table to view/modify")
table = input()
mycursor.execute("SHOW TABLES")
for x in mycursor:
if (x[0]) == table:
print()
options(table)
else:
print("Create table?")
create = input("y/n ")
if create == "y":
createTable(table)
return None
return None
return None
def createTable(tableName):
print("Create table called:", tableName)
print("How many columns?")
columns = input()
columns = int(columns)
count = 1
columnNames = []
while columns != 0:
print("Column", count, "name:")
columnX_Names = input()
columnNames.append(columnX_Names)
columns = columns - 1
count = count + 1
print("Confirm column names")
for x in columnNames:
print(x)
answer = input("y/n ")
if answer == "y":
print(columnNames[0])
print(tableName)
mycursor.execute("CREATE TABLE "+tableName+" (temp VARCHAR(255))")
count = 0
for x in columnNames:
sql = "ALTER TABLE "+tableName+" ADD COLUMN ("+x+" VARCHAR(255))"
mycursor.execute(sql)
print(sql)
print(sql)
mycursor.execute("ALTER TABLE "+tableName+" DROP COLUMN TEMP")`
However, everytime I run the code it delivers an error message when it tries to create a table. Error code:
Traceback (most recent call last):
File "/Users/daniel/PycharmProjects/ObjectOrientedProgramming/Admin.py", line 226, in <module>
getDatabaseCursor("localhost", username, password, database)
File "/Users/daniel/PycharmProjects/ObjectOrientedProgramming/Admin.py", line 78, in getDatabaseCursor
createTable(table, mydb)
File "/Users/daniel/PycharmProjects/ObjectOrientedProgramming/Admin.py", line 161, in createTable
mydb.cursor().execute("CREATE TABLE "+tableName+" (temp VARCHAR(255))")
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/mysql/connector/connection.py", line 1430, in cursor
self.handle_unread_result()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/mysql/connector/connection.py", line 1741, in handle_unread_result
raise InternalError("Unread result found")
mysql.connector.errors.InternalError: Unread result found
I have tried rewriting the string for the SQL query and triple checked that it is the correct syntax. I have tried printing the SQL query and it displays exactly correct. Anybody know what is causing this or what I have missed?
>Solution :
The problem is that you’re re-using the same cursor to execute a new CREATE TABLE
query, when that cursor still has unread results remaining from the previous SHOW TABLES
query.
You have to fetch all the results from the cursor, before using that same cursor to execute a new query. Or else use a new cursor for the new query.