How to fix UnicodeDecodeError while fetching result from db in python? - python

I am using a postgres database and connecting it using psycopg2 connector. Here is my code below
import psycopg2
conn = psycopg2.connect(database=db, user=user, password=password, host=host, port=port)
cur = conn.cursor()
try:
cur.execute(sql_query)
result = cur.fetchall()
except Exception as e:
print("Could not get results")
conn.rollback()
Now the problem is I receive this error when I try to retrieve data from db
Traceback (most recent call last):
File "/home/ec2-user/connection/dbaccess.py", line 119, in get_results_of_query
result = cur.fetchall()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 20: ordinal not in range(128)
Now on looking up online, I found some posts where they asked to use set_client_encoding('UTF8')
So this is what I did
conn = psycopg2.connect(database=db, user=user, password=password, host=host, port=port)
cur = conn.cursor()
conn.set_client_encoding('UTF8')
And then I tried to run it again. But I still receive the same error like before. What am I doing wrong?

Related

SQL Database file not showing up in folder?

I am trying to create a database file using the following code:
def dbconnect():
try:
sqliteConnection = sqlite3.connect('SQLite_Python.db')
cursor = sqliteConnection.cursor()
print("Database created and Successfully Connected to SQLite")
sqlite_select_Query = "select sqlite_version();"
cursor.execute(sqlite_select_Query)
record = cursor.fetchall()
print("SQLite Database Version is: ", record)
cursor.close()
except sqlite3.Error as error:
print("Error while connecting to sqlite", error)
finally:
if sqliteConnection:
sqliteConnection.close()
print("The SQLite connection is closed")
conn = dbconnect()
conn.close()
yet when I run the code, although there are no file errors, it doesnt print anything or create a SQL file in the folder of the python code.
I can't seem to figure out what is going wrong.

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.

How to fetch data from SQL query in Python

Good Afternoon,
I wrote a query in MySQL, and I want to execute the same query in Python.
The Code I wrote as Follows.
1.
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='AdventureWorks2012',
user='root',
password='r##*****')
sql_select_Query = "select * from Person.person"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
However, I'm getting following error message while running part two-
''File "", line 5
password='r##*****')
^
SyntaxError: unexpected EOF while parsing''
Any suggestion please how to overcome this problem?
You are missing the except clause:
try:
connection = mysql.connector.connect(host='localhost',
database='AdventureWorks2012',
user='root',
password='r##*****')
except Exception as e:
print(e)
You should check the documentation for more information.

Using pymysql - unicode

I'm trying to using pymysql for database connection.
But when I call procedure and get unicode value result, it breaks.
conn = pymysql.connect(host=host, user=user, password=pw, db=db, charset='utf8mb4', use_unicode=True)
cs = conn.cursor()
cs.callproc('procedure_name', (arg, ))
for row in cs:
str=row[0]
The unicode data in str, it views "\ub2e4\uc0b0\uc9c0\uc564\uc9c0-\uc5f0\uad6c"
It means "다산지앤지-연구소".
I'm trying encode and decode, it just be more like byte-style.
How can I solve this problem?

Python mysql.connector error

I am facing an error while executing a python script.
Checked with directly using the users and password.
Checked with saved the users and pass in variables too.
Script:
import mysql.connector,sys
try:
cnx = mysql.connector.connect (host='localhost', user = 'user', password = 'user#543', port= 3306)
except mysql.connector.errors.ProgrammingError, e:
print time.strftime("%Y-%m-%d %H:%M:%S"),"Error %s" % (e)
sys.exit(1)
except mysql.connector.errors.InterfaceError, e1:
print time.strftime("%Y-%m-%d %H:%M:%S"),"Error %s" % (e1)
sys.exit(1)
cursor = db.cursor()
cursor.execute("select version()")
result_set = cursor.fetchall()
print result_set
Error:
Traceback (most recent call last):
File "test.py", line 3, in <module>
cnx = mysql.connector.connect (host='localhost', user = 'user', password = 'user#543', port= 3306)
AttributeError: 'str' object has no attribute 'connector'
It's a typo. There's a space between connect and the argument list. Change the line to:
cnx = mysql.connector.connect(
host='localhost', user='user', password='user#543', port=3306
)

Categories