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 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'm trying to insert data that's already in one mysql table into another, using python. The column names are the same in each table, and objkey is the distinguishing piece of data I have for the item that I'd like to use to tell mysql which columns to look at.
import MySQLdb
db = MySQLdb.connect(host='', user='', passwd='', db='')
cursor = db.cursor
sql = "INSERT INTO newtable (%s, %s, %s, %s) SELECT %s, %s, %s, %s FROM oldtable
WHERE %s;" % ((name, desig, data, num), name, desig, data, num, obj = repr(objkey))
cursor.execute(sql)
db.commit()
db.close()
It says I have a syntax error, but I'm not sure where since I'm pretty sure there should be parentheses around the field names the first time but not the second one. Anyone know what I'm doing wrong?
I'm not exactly sure what you are trying to do with the obj = repr(objkey) line, but python is thinking you are defining variables with this line, not setting sql syntax (if that is indeed your desire here).
sql = "INSERT INTO newtable (%s, %s, %s, %s) SELECT %s, %s, %s, %s FROM oldtable
WHERE %s;" % ((name, desig, data, num), name, desig, data, num, obj = repr(objkey))
should probably be changed to something like:
sql = "INSERT INTO newtable (%s, %s, %s, %s) SELECT %s, %s, %s, %s FROM oldtable
WHERE obj=%;" % ((name, desig, data, num), name, desig, data, num, repr(objkey))
But even then, you would need objkey defined somewhere as a python variable.
This answer may be way off, but you need to defined what you are expecting to achieve with obj = repr(objkey), in order to get more accurate answers.
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.
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.