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)")
Related
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
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.
I'm new to Python (learnt how to code with it in 2 days ago). I'm trying to get feeds from MySQL database and insert theme into other table. But nothing inserted.
Here is my code:
cnx = MySQLConnection(**db_config)
if cnx.is_connected():
print("Database connected successfully...")
cursor = cnx.cursor(dictionary=True)
cursor.execute("SELECT * from external_feeds WHERE discipline = 'ALL' AND actif = 1")
rows = cursor.fetchall()
insert_feed = ("INSERT INTO feeds "
"(categorie, urlflux, titreflux, photonews, textnews, date, titrenews, liensnews, slug, photo)"
"VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
for row in rows:
feed = feedparser.parse(row["url"])
feed_link = row["url"]
name = row["name"]
image = row["photo"]
category = row["discipline"]
x = len(feed.entries)
for i in range(x):
feed_title = feed.entries[i].title
print feed_title
feed_url = feed.entries[i].link
print feed_url
feed_published = feed.entries[i].published
dPubPretty = strftime(feed_published, gmtime())
feed_description = feed.entries[i].description
slug = re.sub('[^a-zA-Z0-9 \n\-]', '', feed_url)
slug = slug.replace('httpwww', '')
slug = slug.replace('http', '')
# print insert_feed
data_feed = (category, feed_link, name, None, feed_description, dPubPretty, feed_title, feed_url, slug, image)
try:
cursor.execute(insert_feed, data_feed)
cursor.commit()
except:
cnx.rollback()
cursor.close()
Is there anyone who can help me figure out where the problem is? I am completly new to this so I'm totally lost
I see that you are performing 'cursor.commit()' after inserting the data, which is incorrect, try using 'cnx.commit()'.
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.
I've been trying to upload a block of html into a MySQL table and am getting an error message but it's a bit confusing as far as where the problem is exactly. The code is as follows (this isn't all the code, just the conflict area):
from mysql import connector
import time
def send(self, config, query, queryData):
cnx = connector.connect(**config)
cursor = cnx.cursor()
cursor.execute(query, queryData)
cursor.close()
cnx.close()
#this grabs the list of the succesfulyCapturedPackages and uploads to the wp_posts table.
def upload(self):
txtdate = time.strftime("%m-%d-%Y")
for pkg in self.succesfullyCaptured:
filename = pkg + ".html"
with open(filename,'r') as f:
package_post = f.read()
current_timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
trimmed_package_name = pkg.lower()
trimmed_package_name = trimmed_package_name.replace(" ","-")
tup = self.fetch(self.configExternalDB,"SELECT MAX(ID) from wp_posts")
webURL = "http://www.AWORDPRESSSITE.com/?p=" + str(int(tup[0][0]) + 1)
package_post = connector.conversion.MySQLConverter().escape(package_post)
add_pkg_data = ("INSERT INTO wp_posts (post_author, post_date,"
"post_date_gmt, post_content, post_title, post_status,"
"comment_status, ping_status, post_name, "
"post_modified, post_modified_gmt, "
"post_parent, guid, menu_order, post_type, "
"comment_count) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, "
"%s, %s, %s, %s, %s, %s, %s,)")
data_pkg = (1, current_timestamp, current_timestamp, package_post,
pkg, 'draft', 'open', 'open', trimmed_package_name,
current_timestamp, current_timestamp, 0, webURL, 0, 'post', 0)
self.send(self.configExternalDB , add_pkg_data, data_pkg)
Now what I'm getting is the following:
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 ')' at line 1
BTW the HTML blocks I'm trying to capture look somewhat like this:
<h5><span lang=\"ES-MX\"> text text text $ 1000 USD + 755.00 text text días</span></h5>\n<h6><span lang=\"ES-MX\">Text: Text / Text / Text / Text</span></h6>\n<!--more--><span style=\"font-weight: 600; color: #222222;\">[google-map-v3 shortcodeid=\"TO_BE_GENERATED\" width=\"100%\" height=\"350\" zoom=\"12\" maptype=\"roadmap\" mapalign=\"center\" directionhint=\"false\" language=\"default\" poweredby=\"false\" maptypecontrol=\"true\" pancontrol=\"true\" zoomcontrol=\"true\" scalecontrol=\"true\" streetviewcontrol=\"true\" scrollwheelcontrol=\"false\" draggable=\"true\" tiltfourtyfive=\"false\" enablegeolocationmarker=\"false\" enablemarkerclustering=\"false\" addmarkermashup=\"false\" addmarkermashupbubble=\"false\" addmarkerlist=\" Buenos Aires{}1-default.png|Iguazu{}1-default.png|Bariloche{}1-default.png|Santiago{}1-default.png|\" bubbleautopan=\"true\" distanceunits=\"miles\" showbike=\"false\" showtraffic=\"false\" showpanoramio=\"false\"]</span>\n<h2>TEXT</h2>\n<address>*TEXT</address>\n
... Except bigger (more html text).
So what I'm not sure of at this point is if the HTML characters are not being escaped correctly or if I'm missing something somewhere.
As always, help is greatly appreciated. :)
Try this:
add_pkg_data = ("INSERT INTO wp_posts (post_author, post_date,"
"post_date_gmt, post_content, post_title, post_status,"
"comment_status, ping_status, post_name, "
"post_modified, post_modified_gmt, "
"post_parent, guid, menu_order, post_type, "
"comment_count) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, "
"%s, %s, %s, %s, %s, %s, %s)")
Not sure you need the comma after the final %s.