Getting InternalError: Unread result found after inserting new data - python

I'm new to python and MySQL but I've been trying to program a schema which has a user entity with 2 subtypes: student and instructor. When I use this function
def create_user(userName, email, Instructor):
mycursor = mydb.cursor()
generatedKey = uuid.uuid4()
id = generatedKey.bytes
generatedKey = uuid.uuid4()
PCID = generatedKey.bytes
sql = "INSERT INTO User(UserID,UserName, UserEmail) VALUES (%s, %s, %s)"
val = (id, userName, email)
mycursor.execute(sql, val)
mydb.commit()
if(Instructor):
sql = "INSERT INTO PostCreator(PCID, CreatorType) VALUES (%s, %s)"
val = (PCID, "Instructor")
mycursor.execute(sql, val)
mydb.commit()
sql = "INSERT INTO Instructor(InstructorId,PCID) VALUES (%s, %s)"
val = (id, PCID)
mycursor.execute(sql, val)
mydb.commit()
else:
sql = "INSERT INTO PostCreator(PCID, CreatorType) VALUES (%s, %s)"
val = (PCID, "Student")
mycursor.execute(sql, val)
mydb.commit()
sql = "INSERT INTO Student(StudentId,PCID) VALUES (%s, %s)"
val = (id, PCID)
mycursor.execute(sql, val)
mydb.commit()
print("Registered", userName)
to create a student I don't have any issues. However when I try to create an instructor the system either times out, or creates an instructor but and following calls on the system results in a: InternalError: Unread result found after inserting new data.
I have no idea why making a student is ok but instructors don't work. Their tables are created the same in the SQL.

Related

I'm trying to insert values into MySQL table in Python, but I keep getting a error when I try it

I have created a table named 'Patient':
import mysql.connector as mysql
db=mysql.connect(host="localhost", user="root", password="xxxx",
database='project')
cursor = db.cursor()
pat = 'create table Patient(ID char(10) primary key,Token int(10),Name
varchar(20),Phone int(10),Email char(20),Age int(3),BG_needed
char(3),Quantity char(2),Gender char(1),Date date)'
cursor.execute(pat)
sql = 'Insert into
Patient(ID,Token,Name,Phone,Email,Age,BG_needed,Quantity,Gender)
values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
val = ('pat1','2','Aaron','93242995','aArons12#gmail.com','20','B-','3L','M',
'2022-10-01')
cursor.execute(sql, val)
db.commit()
for x in cursor:
print(x)
And I'm getting the output as:
DataError: Column count doesn't match value count at row 1
Can you please help me find the error?
I'm sorry if you think I'm asking a silly question, I'm just in 11th grade, and this topic wasn't taught to us. I'm trying to learn this on my own...
There are too many problems in your script. Your number of parameters don't match.
import mysql.connector as mysql
db = mysql.connect(host="localhost", user="root",
password="xxxx",database='project')
cursor = db.cursor()
pat = 'create table Patient(ID char(10) primary key,Token int(10),Name
varchar(20),Phone int(10),Email char(20),Age int(3),BG_needed
char(3),Quantity char(2),Gender char(1),Date date)'
cursor.execute(pat)
sql = 'Insert into
Patient(ID,Token,Name,Phone,Email,Age,BG_needed,Quantity,Gender,Date)
values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
val = ('pat1','2','Aaron','93242995','aArons12#gmail.com','20','B-
','3L','M','2022-10-01')
cursor.execute(sql, val)
db.commit()
for x in cursor:
print(x)
It was an easy fix. Hope that you find it useful

How to solve pymysql.err.programmingError during upload using pymysql

I want to create a dataframe and update it to mysql.
If there is a duplicate key, it will be updated and if there is no duplicate key, it will be inserted.
user = 'test'
passw = '...'
host = '...'
port = '...'
database = '...'
conn = pymysql.connect(host=host,
port=port,
user=user,
password=passw,
database=database,
charset='utf8')
curs = conn.cursor()
data = list(dataframe.itertuples(index=False, name=None))
sql = "insert into naversbmapping(brand, startdate, enddate, cost, daycost) values (%s, %s, %s, %s, %s) on duplicate key update brand = %s, startdate = %s, enddate = %s, cost = %s, daycost = %s"
curs.executemany(sql, data)
conn.commit()
conn.close()
However, I get the following error. How do I fix it?
pymysql.err.ProgrammingError: (1064, "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 '%s, startdate = %s, enddate = %s, cost = %s, daycost = %s' at line 1")
)
You use following MySQL constriuct so that you don't need the data twice as you have the double number of values on your original, but are only sending it once
$sql = "INSERT INTO naversbmapping(brand, startdate, enddate, cost, daycost) VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE brand = VALUES(brand), startdate = VALUES(startdate), enddate = VALUES(enddate), cost = VALUES(cost), daycost = VALUES(daycost)")

Python sql statement error

I've run into a problem while trying to execute an insert statement from python.
Here is my function definition:
def fill_course(param_string):
ar = param_string.split("|")
db = connect()
sql = (
"INSERT INTO COURSE(`NAME`, `DURATION`, `DEPT`) "
"VALUES (%s, %s, %s)"
)
data = ar[0], ar[1], ar[2]
cursor = db.cursor()
cursor.execute(sql, data)
db.commit()
if cursor.rowcount == 0:
res = 0
elif cursor.rowcount == 1:
res = 1
db.close()
print(res)
return res
I've followed this link as a reference.
The error I am getting is :
File "database.py", line 25
"INSERT INTO COURSE "VALUES (%s, %s, %s)"
^
SyntaxError: invalid syntax
I am not able to understand which part of the syntax is wrong here?
Please write the following string
"INSERT INTO COURSE(`NAME`, `DURATION`, `DEPT`) "
"VALUES (%s, %s, %s)"
like below:
"INSERT INTO COURSE(`NAME`, `DURATION`, `DEPT`) VALUES (%s, %s, %s)"
or concatenate the two strings. As it is now, there is a syntax error.

Python scraper how to add mysql insert

What is wrong in my code ? I want to add %s to my mysql db.
titlux = tree.xpath('//*[#id="offer-title"]/h1/text()')
pretx = tree.xpath('//*[#id="offer-price-stock"]/div[3]/span/#content')
print "%s," % titlux
print "%s," % pretx
print "\n"
conn = ..............
x = conn.cursor()
try:
x.execute ("""INSERT INTO produse (titlu, pret) VALUES (%s, %s)""")
conn.commit()
except:
conn.rollback()
conn.close()
You're missing the replacement variables and some quotes in your SQL insert. Change it to:
x.execute ("""INSERT INTO produse (titlu, pret) VALUES ("%s", "%s")""" % (titlux[0], pretx[0]))
#Alastair has the right answer but if you want to see the query you're using
print "INSERT INTO produse (titlu, pret) VALUES (%s, %s)" % (titlux, pretx)
x.execute ("""INSERT INTO produse (titlu, pret) VALUES (%s, %s)""" % (titlux, pretx))

Error when inserting row to table with MySQLdb

What is wrong below?
import MySQLdb as mysql
import datetime
db = mysql.connect("localhost","root","passworld","employees" )
cursor = db.cursor()
sql = "INSERT INTO employee(id, firstname, surname, sex, employmentdate) VALUES (%s, %s, %s, %s, '%s')" %(id, firstname, surname, sex, employmentdate)
dater = datetime.datetime(2005,1,1)
cursor.execute(["012345","Mark", "Rooney", "M", dater])
OperationalError: (1054, "Unknown column 'Mark' in 'field list'")
You should pass your sql statement and params to cursor.execute():
sql = "INSERT INTO employee(id, firstname, surname, sex, employmentdate) VALUES (%s, %s, %s, %s, '%s')"
cursor.execute(sql, ["012345","Mark", "Rooney", "M", dater])
db.commit()

Categories