Python Google API build - python

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.

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

Getting error from mysql request in python

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)")

need help to fix this issue

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, ))

Executemany gives "TypeError: not enough arguments for format string"

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()

Syntax error of Inserting data into mysql in python 3.4 with mysql.connector

I am getting data from the web and inserting it into mysql database by the following code. There seems to be a problem in my SQL syntax for adding records in the data table. The error message goes as follows:
Can someone help with this?
Error message:
> File "D:\Clouds\Dropbox\programming\Python\get youbike info.py", line
> 33, in <module>
> cursor.execute(insert_data) File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line 559,
> in execute
> self._handle_result(self._connection.cmd_query(stmt)) File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line
> 494, in cmd_query
> result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query)) File
> "C:\Python34\lib\site-packages\mysql\connector\connection.py", line
> 396, in _handle_result
> raise errors.get_exception(packet) mysql.connector.errors.ProgrammingError: 1064 (42000): You have an
> error in your SQL syntax; check the manual that corresponds to your
> MariaDB server version for the right syntax to use near '%s, %s, %s,
> %s, %s, %s, %s, %s, %s)' at line 1
My Code
import urllib.request
import gzip
import json
import mysql.connector
url = "http://data.taipei/youbike"
urllib.request.urlretrieve(url,"data.gz")
f=gzip.open('data.gz','r')
jdata = f.read()
f.close()
data = json.loads(jdata.decode('utf-8'))
stationNo =1
cnx=mysql.connector.connect(user='root', host='127.0.0.1', database='bike')
cursor = cnx.cursor()
for key,value in data["retVal"].items():
sno = value["sno"]
sna = value["sna"]
sarea = value["sarea"]
lat = value["lat"]
lng = value["lng"]
ar = value["ar"]
sareaen = value["sareaen"]
snaen = value["snaen"]
aren = value["aren"]
print("NO." + sno + " " + sna)
stationNo+=1
insert_data = ("INSERT INTO info "
"(sno, sna, sarea, lat, lng, ar, sareaen, snaen, aren) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)")
cursor.execute(insert_data)
cnx.commit()
cursor.close()
cnx.close()
you are not passing any data to the values section, the placeholder %s are going blank as there you are not passing data
insert_data = ("INSERT INTO info "
"(sno, sna, sarea, lat, lng, ar, sareaen, snaen, aren) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)")
cursor.execute(insert_data,(sno, sna, sarea, lat, lng, ar, sareaen, snaen, aren))
try this
To help fault-find, do a
print "("INSERT INTO info "
"sno, sna, sarea, lat, lng, ar, sareaen, snaen, aren) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)")
and check the values for any double-quote marks; the presence of any will definitely cause the SQL to break.
Also, by "getting data from the web" and inserting it straight into your code as you've done opens up your app to some very serious SQL-insertion attacks, so perhaps it is best to spend some time sanitizing the web input, in addition to stripping quotation marks.

Categories