Python-SQL Error : Commands out of sync; you can't run this command now

26 Views Asked by At

I am facing the error mentioned in the title when I try to run this code. (this is a part of a bigger project).

Basically, for some reason, it is not allowing me to insert rows into my table after creation. Could someone tell me the solution and also the exact cause of this problem?

`  import mysql.connector as sqlcon
 
   mydb = sqlcon.connect(host="localhost", user="user", passwd="root")
   cursor = mydb.cursor()
   cursor.execute("CREATE DATABASE IF NOT EXISTS Railway;"
   "USE Railway;"
 
                   "Create Table IF NOT EXISTS RailwayTrains("
                   "TrainNo INT PRIMARY KEY,"
                   "STATUS Varchar(10),"
                   "SeatsRemaining INT);"
 
                   "Create Table IF NOT EXISTS RailwayTickets("
                   "PNR varchar(11) Primary Key,"
                   "name Varchar(20) NOT NULL,"
                   "TrainNo varchar(20),"
                   "date Varchar(10),"
                   "contact varchar(15));")
 
   st='Insert into RailwayTickets Values(%s,%s,%s,%s,%s)'
   val=("12","12","12","12","12")
   cursor.execute(st,val)`

THE ENTIRE ERROR MESSAGE

What I expected this program to do was basically to first create the said tables and db and then insert the rows into the table after that.

I tried to instead include the insert statement in the intial execute command itself (just for testing), but in that case, the last command just gets skipped and all else gets executed. {NOTE: while doing this i actually put in the values instead of %S}

0

There are 0 best solutions below