V
V
vladvaraks2021-12-04 09:36:30
MySQL
vladvaraks, 2021-12-04 09:36:30

How to make friends python 2.7, Russian characters and pyodbc?

I'm trying to send a request to the database using pyodbc and python 2.7, if you do not enter Russian letters in the request, then it works, but if you make a condition with Russian characters, then in the console I get an empty list, I think the problem is in the encoding, but I can't understand how to solve.

import pyodbc
import pandas as pd
import sys
from imp import reload
 
reload(sys)
sys.setdefaultencoding('utf-8')
driver = 'DRIVER={MySQL ODBC 3.51 Driver}'
server = 'SERVER=localhost'
port = 'PORT=3306'
db = 'DATABASE=oc12'
user = 'UID=root'
pw = 'PWD=1234'
conn_str = ';'.join([driver, server, port, db, user, pw])
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()
 
b = cursor.execute("SELECT change_log.ParamProd, (SUM(CounterAfter)-SUM(CounterBefore)) AS Schet FROM selling LEFT JOIN (SELECT change_log.ShopKey, change_log.SeekKey, Max(change_log.Parameters) AS ParamProd, Max(change_log.DatetimeEnd) AS DTE FROM change_log WHERE change_log.GuideKey=592384 GROUP BY change_log.SeekKey, change_log.ShopKey)  AS change_log ON (selling.ShopKey = change_log.ShopKey) AND (selling.ResourceShopKey = change_log.SeekKey) WHERE date(TransactionDatetime)='2021-03-18' and change_log.ParamProd LIKE '%АИ-92%' GROUP BY change_log.ParamProd")
print(b.fetchall())

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vladvaraks, 2021-12-05
@vladvaraks

The answer is banal, suddenly someone will encounter such a stupid error code below
# -*- coding: utf-8 -*-
import pyodbc
import pandas as pd
driver = 'DRIVER={MySQL ODBC 3.51 Driver}'
server = 'SERVER=localhost'
port = 'PORT=3306'
db = 'DATABASE=oc12'
user = 'UID=root'
pw = 'PWD=1234'
conn_str = ';'.join([driver, server, port, db, user, pw])
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()
b = cursor.execute( u"SELECT change_log.ParamProd, (SUM(CounterAfter)-SUM(CounterBefore)) AS Schet FROM selling LEFT JOIN (SELECT change_log.ShopKey, change_log.SeekKey, Max(change_log.Parameters) AS ParamProd, Max(change_log.DatetimeEnd) AS DTE FROM change_log WHERE change_log.GuideKey=592384 GROUP BY change_log.SeekKey, change_log.ShopKey) AS change_log ON (selling.ShopKey = change_log.ShopKey) AND (selling.ResourceShopKey = change_log.SeekKey) WHERE date(TransactionDatetime)='2021-03 -18' and change_log.ParamProd LIKE '%AI-92%' GROUP BY change_log.ParamProd")
print(b.fetchall() .encode('encoding required') )

R
rPman, 2021-12-04
@rPman

add this to conn_str
CHARSET=UTF8

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question