how to disable quotes around string when passed as query in python? - python

ok
I am sending a query to my mysql server somthing like this
INSERT INTO accesslist (user, ip, sshid, status) VALUES ("root", "138.117.37.88", "1335", "Failed")
for it i wrote my query as
query = ('INSERT INTO %s (user, ip, sshid, status) VALUES (%s, %s, %s, %s)')
data_entry = (tablename, user, ip, sshid, status)
but what it is doing is it is adding quotes around tablename and mysql is throwing an error for that. It is sending as
INSERT INTO "accesslist" (user, ip, sshid, status) VALUES ("root", "138.117.37.88", "1335", "Failed")
so how can i remove the quotes around my tablename?
If i put a static tablename then it is working fine.

Your solution seems correct. Not to digress, but I recommend you to use str.format as it provides more options and support named kwargs. Besides it is a newer and recommended approach.
Tutorial

Related

inserting data to mysql using python QT and mysql database

I'm struggling to insert or retrive data from database I tried and I watched many tutorials but every time it stops in cur.execute(). i found problem is in the value like self.FirstNmae.text() funtion stop here
def SignupFunction(self):
try:
connection = pymysql.connect(host=cr.host, user=cr.user, password=cr.password, database=cr.database)
cur = connection.cursor()
cur.execute("insert into users (FirstNmae , LastName, Email,Password , Confirm_Paswword ,Answer) value( %s, %s, %s, %s, %s, %s)",
(
self.FirstNmae.text(),
self.LastName.text(),
self.Email.text(),
self.Password.text(),
self.Confirm_Paswword.text(),
self.Answer.text()
))
connection.commit()
connection.close()
self.labelResult.setText("Data Inserted ")
except Exception as e:
self.labelResult.setText("Error Inserting Data")
hello friend i'm not able to comment so i will post as answer from your code you are a new developer , your function is good there are no problem in it i think that the problem is in you database look at the database if you are missed something or you mess write attributes like FirstNmae

Unable to insert data to MySQL using Python and scrapy pipelines

I have tried hours to get around this but still cannot make it to work properly. I am using scrapy to scrape data from a website and then trying to insert that into MySQL database. Here is my database code:
import MySQLdb
class Database:
host = 'localhost'
user = 'root'
password = 'test123'
db = 'scraping_db'
def __init__(self):
self.connection = MySQLdb.connect(self.host, self.user, self.password, self.db,use_unicode=True, charset="utf8")
self.cursor = self.connection.cursor()
def insert(self, query,params):
try:
self.cursor.execute(query,params)
self.connection.commit()
except Exception as ex:
self.connection.rollback()
def __del__(self):
self.connection.close()
Here is my pipeline code where I am making insert query and passing to the above class' insert method:
from con import Database
class LinkPipeline(object):
def __init__(self):
self.db=Database()
def process_item(self, item, spider):
query="""INSERT INTO links (title, location,company_name,posted_date,status,company_id,scraped_link,content,detail_link,job_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s,%s)"""
params=(item['title'], item['location'], item['company_name'], item['posted_date'], item['status'], item['company_id'], item['scraped_link'], item['content'], item['detail_link'],item['job_id'])
self.db.insert(query,params)
return item
This totally works fine on my local machine. But on server I get following error:
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 \')
When I print the params and query from exception block I have this:
query variable:
INSERT INTO links (title, location,company_name,posted_date,status,company_id,scraped_link,content,detail_link,job_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s,%s)
params variable:
((u'Account Leader, Sr',), (u'Sydney',), (u'\n Halliburton',), (datetime.datetime(2018, 4, 9, 21, 55, 46, 789575),), ('Pending',), ([u'0e4554ac6dcff427'],), (u'https://www.site.com.au/rc/clk?jk=3f41218887882940&fccid=0e4554ac6dcff427&vjs=3',), 'Job Content', 'https://jobs.halliburton.com/job/Account-Leader%2C-Sr-IS/437741300/?feedId=162400', ([u'3f41218887882940'],))
I feel the the tuple data is the culprit of MySQL string breaking somewhere due to quotes. But I am very new to Python not sure I checked in another question on SO to follow this syntax to insert into MySQL database i.e:
self.db.insert(query,params)
The above code works fine on my local machine but fails on server. Please guide me in right direction.
Thank you very much!
It very much looks like the tuple encapsulation is your issue. What is the output of:
print( repr( item['location'] ))
That's "print the (coder's) representation of item['location']" (rather than trying to be smart about printing.
>>> print( repr( item['location'] ))
('Sydney',) # A tuple, 1-long, containing a string
>>> print( repr( item['location'] ))
'Sydney' # A string
If it's the first, then your passed data structure inside of item apparently has an extra layer of encapsulation for which your code does not account. The quick and dirty approach to get you up and running:
def process_item(self, item, spider):
query="""INSERT INTO links (title, location,company_name,posted_date,status,company_id,scraped_link,content,detail_link,job_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s,%s)"""
params=(item['title'][0], item['location'][0], ...
self.db.insert(query,params)
return item
Note that this is hardly a robust solution, API-wise: what happens if one of those embedded tuples is zero length? (Hint: Exception). I've also not filled out the rest, because it looks like you have some elements in item that are not encapsulated at all, and others which are doubly encapsulated.
Additionally, you may have some encoding errors with your data after this as some of your elements are unicode and others are not. For example:
(u'Sydney',) ... ('Pending',)
You may want to check exactly what your schema requires.

Escape quotes in MySQL query on python

I am trying solve problem with quotes in my sql queries. I know that many questions were asked on stackoverflow. But no one worked for me. May be because I am using other framework (pyMySQL) for sql connection. I tried parameterized mysql query:
conn = pymysql.connect(host ='host', user='user', passwd='user', db='db', charset='utf8mb4')
cur = conn.cursor()
cur.execute("USE db")
cur.execute("UPDATE table SET callback_data = %s, message = %s, photo = %s, location = %s, first_name_last_name = %s WHERE user_id=%s",
(callback_data, message, photo, location, first_name_last_name, user_id))
cur.connection.commit()
cur.close()
conn.close()
But I always get error with %s (error message: expected, got %s). My python version is 3.6.1. How I can escape quotes in my sql query? Thanks.
First, as Ilja sayed you have to use backticks for mysql reserved words.
Then for text/varchar fields you can use json dumps:
import json
[...]
cur.execute("UPDATE `table` SET callback_data = %s, message = %s, photo = %s, location = %s, first_name_last_name = %s WHERE user_id=%s", % (
json.dumps(callback_data),
json.dumps(message),
json.dumps(photo),
json.dumps(location),
json.dumps(first_name_last_name),
json.dumps(user_id)
)

SQL insert works in Oracle SQL Developer, but not in python

I have what would appear to be a straight forward insert statement for Oracle SQL. It works properly in Oracle SQL Developer but the same command will not work in Python, complaining about
cx_Oracle.DatabaseError: ORA-00933: SQL command not properly ended.
This happens on the line for the cursor.execute() call. The query itself is:
insert into TestNamesTable (TestName, TheUser, TheProject) values ('mytest.s', 'bjurasz', 'Beta');
If run in SQL Developer I get a new row. Inside Python I get the termination error. From what I can tell its properly formed and terminated.
Here is how I construct the query in python:
sql = "insert into TestNamesTable (TestName, TheUser, TheProject) values ('%s', '%s', '%s');" % (diagname, username, project)
print sql
cursor.execute(sql)
connection.commit()
Try this:
# These are just random definitions, must be of type table requires
diagname = "DEFINE HERE"
username = "SOMEBODY"
project = "PROJECT NAME"
# Assuming you've defined your connection before
cursor.execute("""insert into TestNamesTable (TestName, TheUser, TheProject) values (:diagname, :username, :project)""",
{"diagname": diagname, #cx_Oracle likes this bind-variable looking syntax
"username": username.
"project": project})
connection.commit()

Python MySQLdb variables as table names

I have a syntax error in my python which which stops MySQLdb from inserting into my database. The SQL insert is below.
cursor.execute("INSERT INTO %s (description, url) VALUES (%s, %s);", (table_name.encode("utf-8"), key.encode("utf-8"), data[key].encode("utf-8")))
I get the following error in my stack trace.
_mysql_exceptions.ProgrammingError: (1064, "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 ''four' (description, url) VALUES ('', 'http://imgur.com/a/V8sdH')' at line 1")
I would really appreciate assistance as I cannot figure this out.
EDIT:
Fixed it with the following line:
cursor.execute("INSERT INTO " + table_name + " (description, url) VALUES (%s, %s);", (key.encode("utf-8"), data[key].encode("utf-8")))
Not the most sophisticated, but I hope to use it as a jumping off point.
It looks like this is your SQL statement:
cursor.execute("INSERT INTO %s (description, url) VALUES (%s, %s);", (table_name.encode("utf-8"), key.encode("utf-8"), data[key].encode("utf-8")))
IIRC, the name of the table is not able to be parameterized (because it gets quoted improperly). You'll need to inject that into the string some other way (preferably safely -- by checking that the table name requested matches a whitelisted set of table names)... e.g.:
_TABLE_NAME_WHITELIST = frozenset(['four'])
...
if table_name not in _TABLE_NAME_WHITELIST:
raise Exception('Probably better to define a specific exception for this...')
cursor.execute("INSERT INTO {table_name} (description, url) VALUES (%s, %s);".format(table_name=table_name),
(table_name.encode("utf-8"),
key.encode("utf-8"),
data[key].encode("utf-8")))

Categories

Resources