在python3中从phpmyadmin获取数据时打问号

kkih6yb8  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(248)

你好,我赢了10个希腊。我想从mysql中获取相同的数据(文件路径)。问题是当我检索路径而不是我遇到的文件夹的名称时??????。下面是代码示例

import pymysql as MySQLdb

dbid = "client"
password = "pass"
database = "clients"
serverip = "192.168.168.150"
db = None
cur = None
try:
    db = MySQLdb.connect(host=serverip, user=dbid, passwd=password, db=database, connect_timeout=20)
    cur = db.cursor()
except MySQLdb.Error as e:
    print("Cannot connect with the Data Base : "), e

query1 = "SELECT path FROM `instructions` WHERE clientmac ='%s'" % (str(5555))
try:
    row = None
    cur.execute(query1)
    db.commit()
    row = cur.fetchall()

except MySQLdb.Error as e:
    print("Problem while Executing query :", query1, "error : ", e)
print(row)
db.close()
cur.close()

python3使用unicode,所以我认为问题不是来自python,而是来自phpmyadmin。字段路径为“utf8\u general\u ci”。一切看起来都很好,但为什么 'C:\\Users\\Γιαννης\\Documents\\Arduino' 我要走了 'C:\\Users\\???????\\Documents\\Arduino'

k3bvogb1

k3bvogb11#

我找到了答案。我只想在执行查询之前进行一些额外的编码。我把代码贴在下面。

import pymysql as MySQLdb

dbid = "client"
password = "pass"
database = "clients"
serverip = "192.168.168.150"
socketip = "192.168.168.18"

db = None
cur = None
try:
    db = MySQLdb.connect(host=serverip, user=dbid, passwd=password, db=database, use_unicode=True, connect_timeout=20)
    cur = db.cursor()
except MySQLdb.Error as e:
    print("Cannot connect with the Data Base : "), e
query1 = "SELECT path FROM `instructions` WHERE clientmac ='%s'" % (str(5555))

try:
    row = None
    db.set_charset('utf8')
    cur.execute('SET NAMES utf8;')
    cur.execute('SET CHARACTER SET utf8;')
    cur.execute('SET character_set_connection=utf8;')
    cur.execute(query1)
    db.commit()
    row = cur.fetchall()

except MySQLdb.Error as e:
    print("Problem while Executing query :", query1, "error : ", e)
print(row)
db.close()
cur.close()

相关问题