MYSQL closing and reestablishing connection to execute many queries - python

when i tried executing 30+ queries using a single connection in python script the queries were executed but the changes were not reflected in the database but when i closed and reestablished the connection for every 10 queries and executed the queries seemed to work why did this happen but this problem hasn't occured when i tried executing same 30+ queries using my command line why is it so
why should we reestablish the connection
This didn't work
import MySQLdb
con = MySQLdb.connect(hostname,user, password, dbname, port)
cur = con.cursor()
cur.execute(query1)
.
.
.
cur.execute(query50)
cur.execute('commit')
cur.close()
con.close()
This Worked
import MySQLdb
con = MySQLdb.connect(hostname,user, password, dbname, port)
cur = con.cursor()
cur.execute(query1)
.
.
cur.execute(query10)
cur.execute('commit')
cur.close()
con.close()
#reestablished connection
con = MySQLdb.connect(hostname,user, password, dbname, port)
cur = con.cursor()
cur.execute(query11)
.
.
cur.execute(query20)
cur.execute('commit')
cur.close()
con.close()
#reestablished connection
con = MySQLdb.connect(hostname,user, password, dbname, port)
cur = con.cursor()
cur.execute(query21)
.
.
cur.execute(query30)
cur.execute('commit')
cur.close()
con.close()

Related

Python with psycopg2 and pgAdmin4 how can i retrieve bytea data

I have uploaded a jpg image with the bytes() function to the bytea field.
INSERT CODE
conn = None
try:
# read data from a picture
imagen = open("imagen.jpg", "rb").read()
# connect to the PostgresQL database
conn = psycopg2.connect(host="localhost", database="test", user="postgres", password="admin")
# create a new cursor object
cur = conn.cursor()
# execute the INSERT statement
cur.execute("INSERT INTO nuevotest(id,data) " +
"VALUES(%s,%s)",
(1, bytes(imagen)))
# commit the changes to the database
conn.commit()
# close the communication with the PostgresQL database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
SELECT CODE:
conn = None
try:
conn = psycopg2.connect(host="localhost", database="test", user="postgres", password="admin")
# create a new cursor object
cur = conn.cursor()
# execute the INSERT statement
cur.execute(""" SELECT data
FROM nuevotest
WHERE id=1 """,
)
# commit the changes to the database
conn.commit()
imagen = cur.fetchall()
print(type(imagen))
print(imagen)
# close the communication with the PostgresQL database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
But what i was expectiong was a list or tuple with the byte code, not this:
PyCharm console with a (memory direction? idk)
I have no idea how to work with that.
Depends on what you are planning on doing after you retrieve the data. As #adrian_klaver said, you can use a buffer to write the output. Like this you would send it to a file:
with open(your_output_file, 'wb') as im:
im.write(the_in_memory_data)
Using PIL you can output it to the image viewer of the system without saving it.

python mysql connection auto connect on error

I have a problem with my python mysql connection which I need help with.
My setup is two Pi's running servers on each one. One Pi (SolartPi) has Mysql database collecting data. The other pi (OfficePi) is connecting to the solarPi database to retrieve and update data over a network connection.
My main script works all ok until I have to reboot the SolarPi for a maintenance or power problem and the connection to the OfficePi is lost. The python script on the officePi then goes into a fault loop "2006, MYSQL Server has gone away" Below is a sample of this script.
import MySQLdb
connSolar = MySQLdb.connect("192.xxx.x.x", "external", "xxxxx", "xxxxx")
#eternal connection to solar pi database
cursSolar = connSolar.cursor()
while 1:
try:
cursSolar.execute("SELECT * FROM dashboard")
connSolar.commit()
for reading in cursSolar.fetchall():
heatingDemand = reading[2] #get heating demand from dB
print heatingDemand
except (MySQLdb.Error, MySQLdb.Warning) as e:
print (e)
connSolar.close()
So I tried rewriting this with the script from stackoverflow and a web site as shown below, but this now terminates the program when SolarPi is rebooted with the following error
_mysql_exceptions.OperationalError: (2003, 'Can\'t connect to MySQL server on \'192.xxx.x.x' (111 "Connection refused")')
import MySQLdb
class DB:
con = None
def connect(self):
self.conn = MySQLdb.connect("192.xxx.x.x", "xxxxx", "xxxxxx", "house") #eternal connection to solar pi database
def query(self, sql):
try:
cursor = self.conn.cursor()
cursor.execute(sql)
except (AttributeError, MySQLdb.OperationalError):
self.connect()
cursor = self.conn.cursor()
cursor.execute(sql)
return cursor
while 1:
db = DB()
sql = "SELECT * FROM dashboard"
cur = db.query(sql)
for reading in cur.fetchall():
heatingDemand = reading[2] #get heating demand from dB
print heatingDemand
Is there a way for the OfficePi to keep trying to connect to SolarPi mysql database when it has shut down.
Change your code to check a valid connection each loop otherwise pass:
import MySQLdb
class DB:
def connect(self):
try:
self.conn = MySQLdb.connect("192.xxx.x.x", "xxxxx", "xxxxxx", "house")
except (MySQLdb.Error, MySQLdb.Warning) as e:
print (e)
self.conn = None
return self.conn
def query(self, sql):
try:
cursor = self.conn.cursor()
cursor.execute(sql)
except (AttributeError, MySQLdb.OperationalError):
self.connect()
cursor = self.conn.cursor()
cursor.execute(sql)
return cursor
while 1:
db = DB()
conn = db.connect()
if conn:
sql = "SELECT * FROM dashboard"
cur = db.query(sql)
for reading in cur.fetchall():
heatingDemand = reading[2] #get heating demand from dB
print heatingDemand

Python MySQL - Returning Cursor and Connection from Function

Previously I've always used something like...
def getConn():
cnx = mysql.connector.connect(user='user', password='pass',
host='hostIP',
database='database')
cur = cnx.cursor(buffered=True)
return [cnx, cur]
To return cnx and cur objects for use. That works fine.
I now need to use an SSH connection to a DB.
The example below executes the query within the function, but won't return the cnx or cur objects for use afterwards, so I get a print of the result, followed by the error
mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query
I appear to be having the same issue (although a different error returned) to Why won't Python return my mysql-connector cursor from a function?
That question deals with why - I'd like to know if there is a solution.
def returnConnect():
mypkey = paramiko.RSAKey.from_private_key_file('/Users/me/.ssh/id_rsa')
sql_hostname = '127.0.0.1'
with SSHTunnelForwarder(
(ssh_host, ssh_port),
ssh_username=ssh_user,
ssh_pkey=mypkey,
remote_bind_address=(sql_hostname, sql_port)) as tunnel:
cnx = mysql.connector.connect(host='127.0.0.1', user=sql_username,
passwd=sql_password, db=sql_main_database,
port=tunnel.local_bind_port)
cur = cnx.cursor(buffered=True)
sql = "select * from table"
cur.execute(sql)
result = cur.fetchall()
print result
return [cnx, cur]
conn = returnConnect()
cnx = conn[0]
cur = conn[1]
sql = "select * from table"
cur.execute(sql)
result = cur.fetchall()
cnx.close()
print result
Python calls special method __exit__ after with..as execution, so your connection closes after returning from the context of with. Assign the tunnel forvarder to a variable like follow and you`ll be able to use the connection outside the function scope
tunnel = SSHTunnelForwarder(
(ssh_host, ssh_port),
ssh_username=ssh_user,
ssh_pkey=mypkey,
remote_bind_address=(sql_hostname, sql_port))
Read more about compound with statement in the docs.

PyMySQL Error inserting values into table. - Python

I am trying to make a login system with python and mysql. I connected to the database, but when I try to insert values into a table, it fails. I'm not sure what's wrong. I am using python 3.5 and the PyMySQL module.
#!python3
import pymysql, sys, time
try:
print('Connecting.....')
time.sleep(1.66)
conn = pymysql.connect(user='root', passwd='root', host='127.0.0.1', port=3306, database='MySQL')
print('Connection suceeded!')
except:
print('Connection failed.')
sys.exit('Error.')
cursor = conn.cursor()
sql = "INSERT INTO login(USER, PASS) VALUES('test', 'val')"
try:
cursor.execute(sql)
conn.commit()
except:
conn.rollback()
print('Operation failed.')
conn.close()
I think it may have to do with the order of the statements in the connection. According to the PyMySQL github (found here) the correct order is host, port, user, passwd, db.
Like this :
user = input("User: ")
pass = input("Pass: ")
sql = "INSERT INTO login(USER, PASS) VALUES('%s', '%s')"%(user, pass)
btw you should connect like this :
conn = pymysql.connect(
host='127.0.0.1',
user='root',
passwd='root',
db='MySQL
)

How to return errors from PYODBC

I'm making a connection to SQL Server to execute a stored procedure. What is the correct way to 'poll' the server to determine whether the stored procedure finished running successfully or returned an error if the SP takes longer than 60 seconds / 3600 seconds, etc?
import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server}; SERVER=ServerName; PORT=1433;DATABASE=dbname;UID=%s;PWD=%s' % (username, password))
cnxn.execute("EXECUTE msdb.dbo.sp_start_job 'TestSP'")
<pyodbc.Cursor object at 0x0000000002D6DDB0>
How can I determine the status of the SP?
Consider wrapping the execute in a try/except to catch exceptions (which encompass errors). If no error is raised, execute is assumed to run correctly. Also, use the timeout variable (in seconds) as the database should raise OperationError if timeout occurs.
cnxn = pyodbc.connect('DRIVER={SQL Server}; SERVER=ServerName; PORT=1433; \
DATABASE=dbname;UID={0};PWD={1}'.format(username, password))
cnxn.timeout = 60
cursor = cnxn.cursor()
try:
cnxn.execute("EXECUTE msdb.dbo.sp_start_job 'TestSP'")
except Exception as e:
print(e)
It looks like you've skipped making a cursor, so you need to do that, then fetch the results. Try this:
import pyodbc
connection = pyodbc.connect('DRIVER={SQL Server}; SERVER=ServerName; PORT=1433;DATABASE=dbname;UID=%s;PWD=%s' % (username, password))
cursor = connection.cursor()
cursor.execute("EXECUTE msdb.dbo.sp_start_job 'TestSP'")
rows = cursor.fetchall()
for row in rows:
# Do stuff
print(row)

Categories