Python: MySQL Connection not available - python

I have a routine that is accessed every second, it worked fine for the days and then gave the error:
Exception in store_price
[2023-02-03 05:02:56] - Traceback (most recent call last):
File “/x/db.py", line 86, in store_price
with connection.cursor() as cursor:
File "/root/.pyenv/versions/3.9.4/lib/python3.9/site-packages/mysql/connector/connection_cext.py", line 632, in cursor
raise OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available
Below is my code
def store_price(connection, symbol, last_price, timestamp):
"""
:param connection:
:param symbol:
:param last_price:
:param timestamp:
:return:
"""
table_name = 'deribit_price_{}'.format(symbol.upper())
try:
if connection is None:
print('Null found..reconnecting')
connection.reconnect()
if connection is not None: # this is line # 86
with connection.cursor() as cursor:
sql = "INSERT INTO {} (last_price,timestamp) VALUES (%s,%s)".format(table_name)
cursor.execute(sql, (last_price, timestamp,))
connection.commit()
except Exception as ex:
print('Exception in store_perpetual_data')
crash_date = time.strftime("%Y-%m-%d %H:%m:%S")
crash_string = "".join(traceback.format_exception(etype=type(ex), value=ex, tb=ex.__traceback__))
exception_string = '[' + crash_date + '] - ' + crash_string + '\n'
print(exception_string)

Related

Python code erroring when trying to execute a for loop, It calls ArcGis API Python to access Geocode

I am trying to execute the mentioned code and I got the following error:
Not able to get the exact cause of the error
2023-02-10 14:44:27,611 ERROR : No results. Previous SQL was not a query.
Traceback (most recent call last):
File "<ipython-input-1-00966ada7c84>", line 30, in <module>
for row in rows:
pyodbc.ProgrammingError: No results. Previous SQL was not a query.
Code:
import pyodbc
import pandas as pd
import sqlalchemy
from arcgis.geocoding import geocode
from arcgis.gis import GIS
import logging
logging.basicConfig(filename="C:\\Users\\addr_errors.log",filemode='w', level= logging.ERROR,format= '%(asctime)s %(levelname)s : %(message)s')
try:
gis = GIS('home')
# Connect to the SQL Server database
conn = pyodbc.connect(
"DRIVER=;"
"SERVER=;"
"DATABASE=;"
"Trusted_Connection=yes;")
# Create a cursor from the connection
cursor = conn.cursor()
# Execute a SELECT statement to get the rows to update
rows = cursor.execute("SELECT top 1 * FROM [dbo].[Aff_SC] where pflag is null ORDER BY AffiliationID")
if not rows:
print("No results found")
else:
for row in rows:
# Get the values from the current row
address = row.OldAddress
vaffliationid = row.AffiliationID
print(address)
print(vaffliationid)
# Geocode the address
result = geocode(address, as_featureset=True)
#print(result)
if result is not None:
try:
best_match = result.features[0]
print(best_match)
except IndexError:
best_match = None
print(vaffliationid)
update_query = f"UPDATE [dbo].[Aff_SC] SET pflag = 1 WHERE OldAddress = '{address}' and AffiliationID = '{vaffliationid}'"
cursor.execute(update_query)
if best_match is not None:
# Get the standardized address
standardized_address = best_match.attributes["Match_addr"]
print("standardized_address")
print(standardized_address)
#print(vaffliationid)
update_query = f"UPDATE [dbo].[Aff_SC] SET NewAddress = '{standardized_address}' , pflag = 1 WHERE OldAddress = '{address}' and AffiliationID = '{vaffliationid}'"
cursor.execute(update_query)
# Commit the changes to the database
conn.commit()
# Close the cursor and the connection
cursor.close()
conn.close()
except Exception as e:
logging.exception(e)
#finally:
logging.shutdown()
#close the log file, overwriting the logfile worked after closing the handlers
handlers = logging.getLogger().handlers[:]
for handler in handlers:
handler.close()
logging.getLogger().removeHandler(handler)
Tried to run the print statements in the blocks of query and looks fine to me

Error SQLite objects created in a thread can only be used in that same thread

My script used to migrate data from SQLite to Postgres in Python. I'm using threading module to speed up transfer tables but I got error sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. Can someone help me edit it? Thanks.
My script :
import psycopg2, sqlite3, sys
import time
import threading
sqdb="C://Users//duongnb//Desktop//Python//SqliteToPostgreFull//testmydb6.db"
sqlike="table"
pgdb="testmydb13"
pguser="postgres"
pgpswd="1234"
pghost="127.0.0.1"
pgport="5432"
consq=sqlite3.connect(sqdb)
cursq=consq.cursor()
tabnames=[]
cursq.execute('''SELECT name FROM sqlite_master WHERE type="table" AND name LIKE "'''+sqlike+'''%";''')
tabgrab = cursq.fetchall()
for item in tabgrab:
tabnames.append(item[0])
print(tabgrab)
def copyTable(table):
cursq.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name = ?;", (table,))
create = cursq.fetchone()[0]
cursq.execute("SELECT * FROM %s;" %table)
rows=cursq.fetchall()
colcount=len(rows[0])
pholder='%s,'*colcount
newholder=pholder[:-1]
try:
conpg = psycopg2.connect(database=pgdb, user=pguser, password=pgpswd,
host=pghost, port=pgport)
curpg = conpg.cursor()
curpg.execute("DROP TABLE IF EXISTS %s;" %table)
create = create.replace("AUTOINCREMENT", "")
curpg.execute(create)
curpg.executemany("INSERT INTO %s VALUES (%s);" % (table, newholder),rows)
conpg.commit()
if conpg:
conpg.close()
except psycopg2.DatabaseError as e:
print ('Error %s' % e)
sys.exit(1)
finally:
print("Complete")
consq.close()
if __name__ == "__main__":
start_time = time.time()
for table in tabnames:
p = threading.Thread(target = copyTable, args = (table,))
p.start()
for table in tabnames:
p.join()
duration = time.time() - start_time
print("Duration {duration} seconds ")
My error:
Exception in thread Thread-10:
Traceback (most recent call last):
File "C:\Users\duongnb\AppData\Local\Continuum\anaconda3\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\duongnb\AppData\Local\Continuum\anaconda3\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "c:\Users\duongnb\Desktop\Python\SqliteToPostgreFull\MigrateThreading.py", line 25, in copyTable
cursq.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name = ?;", (table,))
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 9744 and this is thread id 12712.

Conditional statement not going as planned with None type in Python

This script is inserting 'NULL' values in a table depending on the date.
But there is another table that contains 'NULL' values depending on if it has been sent or not. Which in this case it has not been sent so I want to ignore them. But the code continues on and gives me this traceback:
The Traceback is as following:
Traceback (most recent call last):
File "C:\projects\docs\script.py", line 43, in <module>
if dr3 < date_sql[0] < dt:
TypeError: '<' not supported between instances of 'datetime.datetime' and 'NoneType'
Been strugling for a long time, so really appreciate your guidance as I have tried to find a solution.
Python code is:
import pyodbc
from datetime import date, datetime
import dateutil.relativedelta
conn = pyodbc.connect(
r'DRIVER={SQL Server};'
r'SERVER=server;'
r'DATABASE=db;'
)
dt = datetime.today()
dr3 = dt - dateutil.relativedelta.relativedelta(months=3)
cursor = conn.cursor()
sent_date = cursor.execute("""SELECT sent_date, id
FROM Department.Documents""")
def fetch_date():
for row in sent_date:
r = row
return r
date_sql = fetch_date()
if sent_date != None:
if dr3 < date_sql[0] < dt:
try:
value = None
cursor.execute("""UPDATE Department.Customer SET name=?, address=?, email=?,
phone=?""", (value, value, value, value))
cursor.commit()
except pyodbc.Error as ex:
print(str(ex))
cursor.rollback()
else:
cursor.close()
print("Connection closed")

ProgrammingError: not all arguments converted during string formatting

I am trying to insert a data into a BLOB column in MySQL Server it is keep giving me this error:
ProgrammingError: not all arguments converted during string formatting
I could not define why so please help,
P.S.
the type of the column in MySQL is set to LONGBLOB
here is my code:
#from mysql.connector import MySQLConnection, Error
import MySQLdb
def update_blob(filename):
# read file
pic = open(filename)
data = pic.read()
# prepare update query and data
query = "UPDATE image " \
"SET picture = ? "
print data
###############
hostname = ''
username = ''
password = ''
database = ''
try:
conn = MySQLdb.connect( host=hostname, user=username, passwd=password, db=database )
print 'connected'
cursor = conn.cursor()
cursor.execute(query, data)
conn.commit()
except Error as e:
print(e)
finally:
cursor.close()
conn.close()
and the error:
ProgrammingError Traceback (most recent call last)
<ipython-input-35-389eb7e8c3c0> in <module>()
----> 1 update_blob('hovik.jpg')
<ipython-input-34-48db763c9aee> in update_blob(filename)
21 print 'connected'
22 cursor = conn.cursor()
---> 23 cursor.execute(query, data)
24 conn.commit()
25 except Error as e:
>/usr/lib/python2.7/dist-packages/MySQLdb/cursors.pyc in execute(self, query, args)
238 query = query % args
239 except TypeError as m:
--> 240 self.errorhandler(self, ProgrammingError, str(m))
241
242 if isinstance(query, unicode):
>/usr/lib/python2.7/dist-packages/MySQLdb/connections.pyc in defaulterrorhandler(***failed resolving arguments***)
50 raise errorvalue
51 if errorclass is not None:
---> 52 raise errorclass(errorvalue)
53 else:
54 raise Exception(errorvalue)
`ProgrammingError: not all arguments converted during string formatting`
According to the Python Database Specification in PEP 249, the format used in a query to show where to insert the parameters depends on the paramstyle member of the database module:
if it is qmark, use ? (question mark)
if it is numeric, use :1, :2 etc. (numeric, positional style)
if it is named, use :name (named style)
if it is format, use %s (ANSI C printf format codes)
if it is pyformat, use %(name)s (Python extended format codes)
AFAIR, MySQLdb uses format, so you should replace your ? with %s.
(If MySQLdb would properly use prepared statements, it would be qmark and ? was the right way to go.)
Sorted!!!! just found the solution,
1 - apparently i could not use ? because of the format specifier,
2 - and i also did not add the con for not only being able to retrive but also to insert in the database,
here is the example of the code that worked for me:
import MySQLdb
hostname = ''
username = ''
password = ''
database = ''
myConnection = MySQLdb.connect( host=hostname, user=username, passwd=password, db=database )
def doQuery() :
fin = open("hovik.jpg",'rb')
contents = fin.read()
fin.close()
with myConnection:
cur = myConnection.cursor()
sql = "INSERT INTO image VALUES (%s)"
ret = cur.execute(sql, [contents])
doQuery()
myConnection.close()

Python MYSQL Database update statement issue

I'm trying to get the following Python MYSQL update statement correct(With Variables):
try:
connection = mysql.connector.connect\
(host = "localhost", user = "root", passwd ="", db = "crawling")
except:
print("Keine Verbindung zum Server")
sys.exit(0)
cursor = connection.cursor()
cursor.execute("TRUNCATE meta;")
connection.commit()
cursor.execute("ALTER TABLE meta AUTO_INCREMENT =1;")
connection.commit()
for j in range(1, int(outerElements)):
for i in range(1, int(innerElements)):
partner_ID = 6
location_ID = 20
headline = driver.find_element_by_xpath("//div[#id='productList']/div["+str(j)+"]/div["+str(i)+"]/div/div[2]/h2/a").text
price = driver.find_element_by_xpath("//div[#id='productList']/div["+str(j)+"]/div["+str(i)+"]/div/div[2]/div[2]/span[2]").text[:-1]
deeplink = driver.find_element_by_xpath("//div[#id='productList']/div["+str(j)+"]/div["+str(i)+"]/div/div[2]/h2/a").get_attribute("href")
print("Header: " + headline + " | " + "Price: " + price + " | " + "Deeplink: " + deeplink + " | " + "PartnerID: " + str(partner_ID) + " | " + "LocationID: " + str(location_ID))
cursor.execute('''UPDATE meta SET (price_id, Header, Price, Deeplink, PartnerID, LocationID) \
VALUES(%s, %s, %s, %s, %s, %s)''', ['None'] + [headline] + [price] + [deeplink] + [partner_ID] + [location_ID])
connection.commit()
cursor.close()
connection.close()
When conducting the code, I´m getting the following error message:
Traceback (most recent call last):
File "C:/Users/hmattu/PycharmProjects/untitled1/localhost_crawl.py", line 97, in test_sel
VALUES(%s, %s, %s, %s, %s, %s)''', ['None'] + [headline] + [price] + [deeplink] + [partner_ID] + [location_ID])
File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line 507, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 722, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 640, 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 '= (price_id, Header, Price, Deeplink, PartnerID, LocationID)
Can anybody help me out? Any feedback is appreciated.
Try to change
UPDATE meta SET (price_id, Header, Price, Deeplink, PartnerID, LocationID)
for
UPDATE meta SET (`price_id`, `Header`, `Price`, `Deeplink`, `PartnerID`, `LocationID`)

Categories