pymysql query : unable to rollback - mysql

The connection is fine, but the query sentence seems problematic.
query1 = """SELECT * FROM `DATABASE` WHERE `coin` = 'LTC'"""
query2 = """SELECT * FROM `DATABASE` WHERE `coin` = 'LTC' AND `date` > '2019-01-01 15:06:23'"""
And then
import pandas as pd
result = pd.read_sql(query, connection)
It works perfectly fine with query1 but gives such error for query2:
result = pd.read_sql(query, connection)
Traceback (most recent call last):
File "<ipython-input-25-c7c27cfd9a6b>", line 1, in <module>
result = pd.read_sql(query, connection)
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pandas\io\sql.py", line 381, in read_sql
chunksize=chunksize)
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pandas\io\sql.py", line 1413, in read_query
cursor = self.execute(*args)
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pandas\io\sql.py", line 1386, in execute
raise_with_traceback(ex)
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pandas\compat\__init__.py", line 404, in raise_with_traceback
raise exc.with_traceback(traceback)
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pandas\io\sql.py", line 1382, in execute
self.con.rollback()
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pymysql\connections.py", line 808, in rollback
self._execute_command(COMMAND.COM_QUERY, "ROLLBACK")
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pymysql\connections.py", line 1122, in _execute_command
raise err.InterfaceError("(0, '')")
DatabaseError: Execution failed on sql: SELECT * FROM `DATABASE` WHERE `coin` = 'LTC' AND `date` > '2019-01-01 15:06:23'
(0, '')
unable to rollback
I want to know what this "unable to rollback" means and how to solve this multi-condition selection in PyMySQL.

unable to rollback
It means your query has not been successfully executed.
An unclosed connection is usually the cause for that error. You might not have closed the previous connection. You can usually do so with the close method associated with the connection instance.

Related

connecting and inserting data into mysql database 5.5 using Python 2.7

Can you please help me with this issue here.
I am getting below error
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\bots\communication.py", line 1703, in outcommunicate
botslib.runscript(self.userscript,self.scriptname,'outcommunicate',channeldict=self.channeldict,dbconnection=self.dbconnection,db_object=db_object)
File "C:\Python27\lib\site-packages\bots\botslib.py", line 492, in runscript
raise ScriptError(_(u'Userscript "%(modulefile)s": "%(txt)s".'),{'modulefile':modulefile,'txt':txt})
ScriptError: Userscript "C:\Python27\lib\site-packages\bots\usersys\communicationscripts\testmysqldbconnection": "Traceback (most recent call last):
File "C:\Python27\lib\site-packages\bots\botslib.py", line 489, in runscript
return functiontorun(**argv)
File "C:\Python27\lib\site-packages\bots\usersys\communicationscripts\testmysqldbconnection.py", line 17, in outcommunicate
cur.execute("""INSERT INTO ccode (ccodeid,leftcode,rightcode) VALUES (%s, %s, $s)""", ( article ) )
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 159, in execute
query = query % db.literal(args)
TypeError: not all arguments converted during string formatting
MY code as below
import MySQLdb
def connect(channeldict):
return MySQLdb.connect('localhost','XXXXXX', 'XXXXXXXXXX','test')
def outcommunicate(channeldict,dbconnection,db_object):
cur = dbconnection.cursor()
for article in db_object:
cur.execute("""INSERT INTO ccode (ccodeid,leftcode,rightcode)
VALUES (%s, %s, $s)""",
( article[0] , article[1] , article[2] ) )
print 'in channel:', article
# Make sure data is committed to the database
dbconnection.commit()
cur.close()
def disconnect(channeldict,dbconnection):
dbconnection.close()
Can you please advice me

Unknown Column exception in Mysql 1054

I tried all possible means. I included backQuotes for the string as suggested some in stack but nothing worked. It repeats the error as usual.
I also tried some queries that worked in other python files still it shows the same. I also tried queries with string without hyphens even it didn't work. I cant find out whats the problem here.
import MySQLdb
import sys
from PyQt4 import QtCore, QtGui, uic
qtCreatorFile = "Studisplay.ui" # Enter file here.
Ui_MainWindow1, QtBaseClass = uic.loadUiType(qtCreatorFile)
class stuDisplay(QtGui.QMainWindow, Ui_MainWindow1,QtGui.QTableWidget):
def __init__(self,ID):
#super(stuDisplay, self).__init__(parent)
QtGui.QMainWindow.__init__(self)
Ui_MainWindow1.__init__(self)
QtGui.QWidget.__init__(self)
self.setupUi(self)
obj = MySQLdb.connect("localhost", "root", "1234567", "python")
#The value of ID here is 14-VEC-244 I also tried `14-VEC-244` but did not work
sql = 'SELECT MEMname FROM Borrowed WHERE MemberID ='+ str(ID)
cursor = obj.cursor()
cursor.execute(sql)
name=cursor.fetchone()
print name
I get this error:
Traceback (most recent call last): File
"/home/gautham/PycharmProjects/LIBALERT/Login.py", line 105, in
pushButton_clicked
self.call = StuSecond.stuDisplay(StuID) File "/home/gautham/PycharmProjects/LIBALERT/StuSecond.py", line 22, in
init
cursor.execute(sql) File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 226, in
execute
self.errorhandler(self, exc, value) File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in
defaulterrorhandler
raise errorvalue
_mysql_exceptions.OperationalError: (1054, "Unknown column 'VEC' in 'where clause'")
You're passing a string into your WHERE clause, so it must be quoted within the query string that gets passed to the database, something like so:
sql = "SELECT MEMname FROM Borrowed WHERE MemberID = '" + str(ID) + "'"
so that the finished string looks like
sql = "SELECT MEMname FROM Borrowed WHERE MemberID = '14-VEC-244'"
(Note that the single quotes are "forward" quotes, not backticks.)
This would also be an excellent application for a prepared statement; unfortunately I am not familiar with pyqt and so cannot advise you there.

Having issues with using mysql SELECT in a function

Started learning mySQL and got stuck on why this command is not working. I had success with UPDATE commands and SELECT * outside the function so I am guess I am making a mistake in calling the function or perhaps the %s needs to be different... My google foo did not find anything so I hope you all can help me!
Thank you so much for looking!
CODE:
def CheckBalance(UserName, BetAmount): #checks to make sure they can afford the bet. Returns 0 for no 1 for yes
import mysql.connector
cnx = mysql.connector.connect(user='root', password='Password',
host='127.0.0.1',
database='crapsdatabase')
c = cnx.cursor()
BankRoll = c.execute("SELECT PlayerBank FROM player WHERE PlayerName = %s", UserName)
if(BankRoll < BetAmount) or (BetAmount < 0):
c.close()
return 0
if(BankRoll >= BetAmount):
c.close()
return 1
From our main program I import the UpdateDatabase and call it
from plugins.database.UpdateDatabase import UpdateBets
a = UpdateBets.CheckBalance("bob", 100)
print(a)
This gives the following error:
C:\python\python.exe C:/Users/Ray/Desktop/bot/plugins/CRAPS/CrapsUpdated.py
Traceback (most recent call last):
File "C:/Users/Ray/Desktop/bot/plugins/CRAPS/CrapsUpdated.py", line 3, in <module>
a = UpdateBets.CheckBalance("bob", 100)
File "C:\Users\Ray\Desktop\bot\plugins\database\UpdateDatabase.py", line 16, in CheckBalance
BankRoll = c.execute("SELECT PlayerBank FROM player WHERE PlayerName = %s", UserName)
File "C:\python\lib\site-packages\mysql\connector\cursor.py", line 515, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\python\lib\site-packages\mysql\connector\connection.py", line 488, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\python\lib\site-packages\mysql\connector\connection.py", line 395, 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 MySQL server version for the right syntax to use near '%s' at line 1
You should escape string literal in the query with apostrophes, so it should be like this:
c.execute("SELECT PlayerBank FROM player WHERE PlayerName = '%s'", UserName)

MySQL python connector IndexError: bytearray index out of range

I'm inserting some data to a database and most of the queries are inserted correctly but I keep getting at least one random query error.
I'm using Python 3, MySQL 5.6.17 and MySQL python connector 2.1.3 (upgraded after having the same problem with 2.0.2).
The queries are run in a Multiprocessing Pool map_async().
multiprocessing.pool.RemoteTraceback: bytearray index out of range
Traceback (most recent call last):
File "./../../../my-python-script.py", line 930, in insert_into_database
mysql_query(mysql_script, values) # <-- My mysql wrapper function
File "./../../../my-python-script.py", line 297, in mysql_query
for row in results:
File "./../../../mysql/connector/cursor.py", line 450, in _execute_iter
result = next(query_iter)
File "./../../../mysql/connector/connection.py", line 520, in cmd_query_iter
yield self._handle_result(self._send_cmd(ServerCmd.QUERY, statements))
File "./../../../mysql/connector/connection.py", line 405, in _handle_result
self._socket.recv(), self.python_charset)
File "./../../../mysql/connector/protocol.py", line 238, in parse_column
(packet, _) = utils.read_lc_string(packet[4:]) # catalog
File "./../../../mysql/connector/utils.py", line 199, in read_lc_string
if buf[0] == 251: # \xfb
IndexError: bytearray index out of range
The above exception was the direct cause of the following exception:
IndexError: bytearray index out of range
Or sometimes I get (from the "for row in results" line)
File "./../../../mysql/connector/cursor.py", line 450, in _execute_iter
result = next(query_iter)
File "./../../../mysql/connector/connection.py", line 520, in cmd_query_iter
yield self._handle_result(self._send_cmd(ServerCmd.QUERY, statements))
File "./../../../mysql/connector/connection.py", line 384, in _handle_result
elif packet[4] == 0:
IndexError: bytearray index out of range
The above exception was the direct cause of the following exception:
IndexError: bytearray index out of range
My setup is something like
class InsertData:
def __init__(self):
with(multiprocessing.Pool(2) as Pool:
Pool.map_async(self.insert_into_database(),set(1,2,3.....))
Pool.close()
Pool.join()
def insert_into_database(self,values):
# use the values to do some calculations then insert to database
mysql_query(mysql_script, values)
def mysql_query(script, values):
cursor.execute(query, values, multi = True)
And the sql script
'INSERT INTO table1 ( column1 ) VALUES ( "x" ); '
'SET #table1 = LAST_INSERT_ID(); '
'INSERT INTO table2 ( column1, column2 ) VALUES ( "y", #table1 ); '
'SET #table2 = LAST_INSERT_ID(); '
...
I'm currently looking at the connector.py and the utils code trying to figure what's happening. But this is too advanced for me.
https://github.com/mysql/mysql-connector-python/blob/master/lib/mysql/connector/connection.py#L368
https://github.com/mysql/mysql-connector-python/blob/master/lib/mysql/connector/utils.py#L167
In a desperate attempt I've tried setting the buffered to True https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursorbuffered.html
I need to read up on bytearrays but I suspect that my query script is causing the problem because I didn't have (I think?) this problem when I ran the queries one connector at a time cursor.execute(query, values, multi = False)
When I send the database connection as described in Accessing a MySQL connection pool from Python multiprocessing the problem disappears.
Something like
mysql_conn = None
def db_conn():
global mysql_conn
mysql_conn = connector.connect(...)
class InsertData:
def __init__(self):
with(multiprocessing.Pool(2, initializer = db_conn) as Pool:
Pool.map_async(self.insert_into_database(),set(1,2,3.....))
Pool.close()
Pool.join()
def insert_into_database(self,values):
# use the values to do some calculations then insert to database
self.mysql_query(mysql_script, values)
def mysql_query(script, values):
cursor = mysql_conn.cursor()
cursor.execute(query, values, multi = True)

Python threaded timer running function with passed variable

I'm trying to run a function (f) every x seconds (in my case 60) which will close an active database connection if one exists and upon completion opens it again.
I am using threading.timer although I'm having trouble passing the connection into the function, and in some situations the function runs repeatedly with nothing else running.
The function needs to return the connection to globals after it completes and I'm finding it hard to pass the connection to the function and assign the return globally from within the function which is how I believe the threading.timer works:
enter code from socketIO_client import SocketIO
import logging
import json
import MySQLdb as mdb
import os
import threading
con = mdb.connect('localhost','username','password','databaseName')
cur = con.cursor()
def f(con):
if 'con' in globals():
con.close()
print ("Connection closed")
os.system('php -f /home/ubuntu/grab.php')
con = mdb.connect('localhost','username','password','databaseName')
cur = con.cursor()
print ("DB Connection opened")
con = mdb.connect('localhost','username','password','databaseName')
cur = con.cursor()
threading.Timer(60,f,con).start(); ######PROBLEM LINE
return con
def on_connect():
print "Connecting to database"
areas = ['EH','BE']
socketIO.emit('subscribe_areas', areas)
def on_message(answer):
print("\nNew message received")
array = (json.loads(answer))
print (array)
runningIdentity = array["value"]
berthID = array["to"]
area = array["area"]
if berthID:
query = ("SELECT crs FROM signalBerth WHERE signalBerth=\'%s\';"%(berthID))
cur.execute(("%s")%(query))
reply = cur.fetchall()
for row in reply:
crs= row[0]
query = "UPDATE service SET lastSeen = \'%s\' WHERE runningIdentity=\'%s"%(crs,runningIdentity)+"\';" #berthID == crs, need to alter
print (("%s")%(query))
cur.execute(("%s")%(query))
con.commit()
print("affected rows = {}".format(cur.rowcount))
socketIO = SocketIO('http://www.realtimetrains.co.uk', 41280) #opens connection
socketIO.on('connect', on_connect) #sends subscription
socketIO.on('message', on_message) #reads data, creates mysql and executes it
con = f(con) ######FIRST CALL TO FUNCTION
socketIO.wait() #Keeps connection openhere
Error:
Traceback (most recent call last): File "input.py", line 49, in
socketIO.wait() #Keeps connection open File "build/bdist.linux-x86_64/egg/socketIO_client/init.py", line 175,
in wait File
"build/bdist.linux-x86_64/egg/socketIO_client/init.py", line 194,
in _process_events File
"build/bdist.linux-x86_64/egg/socketIO_client/init.py", line 202,
in _process_packet File
"build/bdist.linux-x86_64/egg/socketIO_client/init.py", line 327,
in _on_event File "input.py", line 36, in on_message
cur.execute(("%s")%(query)) File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 155, in
execute
charset = db.character_set_name()
_mysql_exceptions.InterfaceError: (0, '') Exception in thread Thread-1: Traceback (most recent call last): File
"/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run() File "/usr/lib/python2.7/threading.py", line 1082, in run
self.function(*self.args, **self.kwargs) TypeError: f() argument after * must be a sequence, not Connection
Perhaps there is a more suited method to my needs, however the important bit it that the connection is closed, the function run and the connection opened again every minute or so. Thought about a cron job, but I'd rather keep my code doing everything.
According to Timer object, its third parameter is args. It is a list, but you pass only the con instead.
You need to replace your problem line with:
threading.Timer(60, f, (con,)).start()