Below code works perfectly in python2 with MySQLDB, how can I make it Python3 compatible?
I have debugged and searched for similar questions.
Error:
Exception ignored in: > Traceback (most recent call last): File > > > "/usr/local/lib/python3.4/dist-packages/pymysql/cursors.py", line 41, in del File > "/usr/local/lib/python3.4/dist-packages/pymysql/cursors.py", line 47, in close ReferenceError: weakly-referenced object no longer exists –
class Database():
def __init__(self):
self.host = 'localhost'
self.user = 'user'
self.password = 'pwd'
self.db = 'dbname'
self.connection = pymysql.connect(host=self.host, user=self.user, passwd=self.password, db=self.db,use_unicode=True, charset="utf8")
self.cursor = self.connection.cursor()
def storeToDB(self,ID,string,g,e):
import datetime
curr_time = datetime.datetime.now()
status = 0
try:
self.cursor.execute("""INSERT INTO master_data (`job_id`,`sstring`,`grl`,`erl`,`status`,`insert_timestamp`) \
VALUES (%s,%s,%s,%s,%s,%s)""",(jobID,search_string,g,e,status,curr_time))
self.connection.commit()
except:
self.connection.rollback()
if __name__ == "__main__":
db = Database()
db.storeToDB(20,"hello","something.com","e.com")
Try to do some opt before db operation finish :
cursor.close()
connection.close()
Related
Why do I have this error?
from psycopg2 import pool
from logger_base import log
import sys
class Conexion:
_DATABASE = 'test_db'
_USERNAME = 'postgres'
_PASSWORD = 'Sxmhz97*/'
_HOST = '127.0.0.1'
_PORT = 5432
_MIN_CON = 1
_MAX_CON = 5
_pool = None
#classmethod
def obtenerPool(cls):
if cls._pool is None:
try:
cls._pool = pool.SimpleConnectionPool(cls._MIN_CON, cls._MAX_CON,
host=cls._HOST,
user=cls._USERNAME,
password=cls._PASSWORD,
database=cls._DATABASE,
port=cls._PORT)
return cls._pool
except Exception as e:
log.error(f'Ocurrio un error al obterner el pool: {e}')
sys.exit()
#classmethod
def obtenerConexion(cls):
conexion = cls.obtenerPool().getconn()
log.debug(f'Conexion obtenida del pool: {conexion}')
return conexion
#classmethod
def liberarConexion(cls, conexion):
cls.obtenerPool().putconn(conexion)
log.debug(f'Regresamos la conexion al pool: {conexion}')
#classmethod
def cerrarConexiones(cls):
cls.obtenerPool().closeall()
if __name__ == '__main__':
conexion1 = Conexion.obtenerConexion()
conexion2 = Conexion.obtenerConexion()
print(conexion1)
print(conexion2)
03:01:55 PM: DEBUG [Conexion.py:33] Conexion obtenida del pool: <connection object at 0x0000022F9BED29B0; dsn: 'user=postgres password=xxx dbname=test_db host=127.0.0.1 port=5432', closed: 0>
Traceback (most recent call last):
File "f:\DATOS\Python\capa_datos\Conexion.py", line 49, in <module>
conexion2 = Conexion.obtenerConexion()
File "f:\DATOS\Python\capa_datos\Conexion.py", line 32, in obtenerConexion
conexion = cls.obtenerPool().getconn()
AttributeError: 'NoneType' object has no attribute 'getconn'
Apparently cls._pool was not None in obtenerPool(cls).
So the body of the statement if cls._pool is None: was not executed, and since the only return statement of obtenerPool is inside this if statement, it returned None.
I'm writing a python code to read from mysql database:
def create_server_connection(host, user, password):
connection = None
try:
connection = pymysql.connect(host='localhost',
user='root',
password='pwd',
database='raw_data',
cursorclass=pymysql.cursors.DictCursor)
print("MySQL Database connection successful")
except err as error:
print(f"Error: '{error}'")
return connection
def read_query(connection, query):
cur = connection.cursor()
result = None
try:
cur.execute(query)
result = cur.fetchall()
return result
except err as error:
print(f"Error: '{error}'")
return cur
def get_Tables_byName(cursor, tableName):
q1 = f'''
SELECT table_name FROM raw_data.tables
where table_name like '{tableName}'; '''
res = []
cursor.execute(q1)
for row in cursor:
res.append(row[0])
return res
get_Tables_byName(cursor,'data_31942010201')
If I want to call get_Tables_byName function, what should I put in the first parameter? If I put cursor, the error message shows NameError: name 'cursor' is not defined
Im trying convert to str
import mysql.connector
from mysql.connector import Error
def VeriEkleme(Deger1,Deger2):
try:
connection = mysql.connector.connect(host='localhost',database='pythonregister',user='pyroot',password='')
if connection.is_connected():
print("MySQL bağlantısı aktif edildi.")
mySql_insert_query = """INSERT INTO userinformations (Username, Password) VALUES """(Deger1,Deger2)
cursor = connection.cursor()
result = cursor.execute(mySql_insert_query)
connection.commit()
print("Record inserted successfully into Laptop table")
cursor.close()
except Error as e:
print("Error while connecting to MysqL", e)
def Register():
Username = input("Username:")
Password = input("Pass:")
VeriEkleme(str(Username),str(Password))
def EnterSystem():
Login = "login"
Answer = input("Login or Register?: ").lower()
if Answer == Login:
print("eşit")
Register()
else:
EnterSystem()
EnterSystem()
Login or Register?: login
eşit
Username:s
Pass:s
MySQL bağlantısı aktif edildi.
Traceback (most recent call last):
File "C:/Users/Relov/PycharmProjects/PygameProje1/PygameProjesi.py", line 36, in <module>
EnterSystem()
File "C:/Users/Relov/PycharmProjects/PygameProje1/PygameProjesi.py", line 32, in EnterSystem
Register()
File "C:/Users/Relov/PycharmProjects/PygameProje1/PygameProjesi.py", line 24, in Register
VeriEkleme(str(Username),str(Password))
File "C:/Users/Relov/PycharmProjects/PygameProje1/PygameProjesi.py", line 11, in VeriEkleme
mySql_insert_query = """INSERT INTO userinformations (Username, Password) VALUES """(Deger1,Deger2)
TypeError: 'str' object is not callable
Process finished with exit code 1
You're calling str as it is a function.
"""INSERT INTO userinformations (Username, Password) VALUES """(Deger1,Deger2)
I recommend using Prepared Statements. This is safe against SQL Injection attacks.
mySql_insert_query = """INSERT INTO userinformations (Username, Password) VALUES (%s, %s)"""
cursor = connection.cursor()
result = cursor.execute(mySql_insert_query, (Deger1, Deger2))
I have the following code:
import psycopg2
conn = psycopg2.connect(database="****", user="postgres", password="*****", host="localhost", port="5432")
print ("Opened database successfully")
cur = conn.cursor()
cur.execute('''select * from xyz''')
print ("Table created successfully")
conn.commit()
conn.close()
Like this i have some 50 -60 complex queries, but problem is some times the postgre sql Database throws the below error
Traceback (most recent call last):
File "C:/Users/ruw2/Desktop/SQL.py", line 87, in <module>
cur.execute('''select * from xyz;''')
psycopg2.DatabaseError: server conn crashed?
server closed the connection unexpectedly.
This probably means the server terminated abnormally before or while processing the request.
Looks like the connection is lost, how can i auto connect the Postgre database
Appreciate your help
I would rely on decorators - retry and reconnect:
import psycopg2
from tenacity import retry, wait_exponential, stop_after_attempt
from typing import Callable
def reconnect(f: Callable):
def wrapper(storage: DbStorage, *args, **kwargs):
if not storage.connected():
storage.connect()
try:
return f(storage, *args, **kwargs)
except psycopg2.Error:
storage.close()
raise
return wrapper
class DbStorage:
def __init__(self, conn: string):
self._conn: string = conn
self._connection = None
def connected(self) -> bool:
return self._connection and self._connection.closed == 0
def connect(self):
self.close()
self._connection = psycopg2.connect(self._conn)
def close(self):
if self.connected():
# noinspection PyBroadException
try:
self._connection.close()
except Exception:
pass
self._connection = None
#retry(stop=stop_after_attempt(3), wait=wait_exponential())
#reconnect
def get_data(self): # pass here required params to get data from DB
# ..
cur = self._connection.cursor()
cur.execute('''select * from xyz''')
# ..
Catch the exception and reconnect:
while True:
conn = psycopg2.connect(database="****", user="postgres", password="*****", host="localhost", port="5432")
cur = conn.cursor()
try:
cur.execute('''select * from xyz''')
except psycopg2.OperationalError:
continue
break;
My kwarg table in **kwarg is not getting recognizing when I invoke it.
class Database():
def set_db_setting(self, host, username, passwd, database):
try:
self.host = host
self.username = username
self.passwd = passwd
self.database = database
self.db = pymysql.connect(host=host, user=username, passwd=passwd, db=database)
print('connected to: {}'.format(database))
return self.db
except:
print('\nerror connecting to database\n')
def db_select(self, *selected_fields, **kwargs):
self.selected_fields = selected_fields = list(selected_fields)
self.table = (kwargs['table']
if 'table' in kwargs
else selected_fields.pop())
try:
with self.db.cursor() as cursor:
sql_tld_id_query = Database.query_stmt_list[0]+ ', '.join(selected_fields) + Database.query_stmt_list[4] + table + Database.query_stmt_list[5] + where_field + '=' + 'www.website.com'
print("sql_tld_id_query is {}".format(sql_tld_id_query))
except Exception as gatherid_err:
print("exception was {}".format(gatherid_err))
self.db.rollback()
I'm invoking it like:
dbclass = Database()
dbclass.set_db_setting('localhost', 'root', 'password', 'garbagedb')
dbclass.db_select('id', 'name', table='tld', where_field='name')
I'm getting an error like:
name 'table' is not defined
FULL TRACEBACK
invoked via:
import traceback
traceback.print_stack()
`
File "dbcrud.py", line 56, in <module>
dbclass.db_select('id', 'name', table='tld', where_field='name')
File "dbcrud.py", line 31, in db_select
traceback.print_stack()
self.selected_fields is ['id', 'name']
exception was name 'table' is not defined
What am I doing wrong here?
I added line continuations to make this fit horizontally...
sql_tld_id_query = Database.query_stmt_list[0]+ ', '.join(selected_fields) + \
Database.query_stmt_list[4] + table + Database.query_stmt_list[5] + \
where_field + '=' + 'www.website.com'
the table in bold should be self.table
you blindly catch all exceptions with you try except Exception block, which probably was hiding the real issue from you. It is better to find out what specific kind of exception you want to catch, and only filter those out. For example if I wanted to have a calculator program that didn't crash when a user tried to divide by zero, I would use try: ... except ZeroDivisionError as e: ...