Trying to insert data to the table with python sending request
def insert_copart(name, lot, vin, odometer, condition, primary_damage, body_style, color, engine, cylinders, drive, fuel, key_present):
query = (
"INSERT INTO copart " "(name, lot, vin, odometer, condition, primary_damage, body_style, color, engine, cylinders, drive, fuel, key_present)"
"VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
args = (name, lot, vin, odometer, condition, primary_damage, body_style, color, engine, cylinders, drive, fuel, key_present)
try:
db_config = read_db_config()
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
cursor.execute(query, args)
if cursor.lastrowid:
print('last insert id', cursor.lastrowid)
conn.commit()
except Error as error:
print(error)
finally:
cursor.close()
conn.close()
But getting error
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 'condition, primary_damage, body_style, color, engine, cylinders, drive, fuel, ke' at line 1`
I checked everything, but no results.
condition is a reserved word in MySQL. I recommend you rename that column and give it a name that isn't a reserved word. If that is not an option, you can escape it by using backticks:
query = (
"INSERT INTO copart "
"(name, lot, vin, odometer, `condition`, primary_damage, body_style, color, engine, cylinders, drive, fuel, key_present)"
"VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
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
So I am trying to execute this query but there is definitely a syntax error I have which I wasn't able to figure out.
If someone could help me that would be greatly appreciated. :)
query = """INSERT IGNORE INTO %s(id, title, body_text, username,time_created,num_of_comments, subreddit, full_link, upvote_ratio) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""
mycursor.execute(query, (post_table_name, tuple1),)
The current runtime error is
result = self._cmysql.convert_to_mysql(*params) _mysql_connector.MySQLInterfaceError: Python type tuple cannot be converted
Table names and field names cannot use substitution. Assuming you're on Python 3, you can use an 'f' string:
query = f"""INSERT IGNORE INTO {post_table_name} (id, title, body_text, username,time_created,num_of_comments, subreddit, full_link, upvote_ratio) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""
mycursor.execute(query, tuple1)
I tried to insert data to the database using python but I got a MySQLdb error
self.cur.execute('''
INSERT INTO book(book_name, book_desc, book_code, book_category, book_author, book_publisher, book_price)
VALUES(%s, %s, %s, %s, %s, %s, %s)
''', (book_title, book_desc, book_code, book_category, book_author, book_publisher, book_price, ))
self.db.commit()
I got this error:
MySQLdb._exceptions.OperationalError: (1366, "Incorrect integer value: '' for column 'book_code' at row 1")
According to the values you shared in the comments, book_code is a string, which is incompatible with the int value in the database. You could overcome this issue by converting it to an int:
self.cur.execute('''
INSERT INTO book(book_name, book_desc, book_code, book_category, book_author, book_publisher, book_price)
VALUES(%s, %s, %s, %s, %s, %s, %s)
''', (book_title, book_desc, int(book_code), book_category, book_author, book_publisher, book_price, ))
I'm trying to insert many rows in MySQL database and for some reason I'm always getting this error.
I have already tried the solutions present in this topic and nothing works.
TypeError: not enough arguments for format string
My Code:
cursor = db.cursor()
row = (date, timetoserve, ipcliente, cacheCode, bytesint, method,\
url.scheme, url.hostname, url.port, url.path, auth, route[0], route[1], contentType)
items.append(row)
if Inputs % 100 == 0:
sql = "INSERT INTO LogTbl \
(DateConnection, TimeToServe, ClientIP, CacheCode, Bytes, Method,\
RequestProtocol, RequestIP, RequestPort, RequestFolder, Auth, RouteLeft, RouteRight, ContentType)\
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.executemany(sql, items)
items = []
db.commit()
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.