1.pymysql Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' 解决办法

1.我想查询Mysql数据库的路径,show global variables like "%datadir%"

我用的是Anconda ,python3.68

能够导入import mysql;import pymysql;import MySQLdb

 

2.用pymysql实现如下:源程序见备注1

     但是出现警告:C:\ProgramData\Anaconda3\envs\tcy_p36\lib\site-packages\pymysql\cursors.py:329: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 517")

3.解决办法:用mysql替代。

import mysql.connector  as mysqlcon
db_name='new_futures'

#连接
#==================
con = mysqlcon.connect(host = "127.0.0.1",user = "root",password = "root", database = db_name,
    charset = 'utf8mb4')
#==================
#获取游标
cursor = con.cursor()

#判断数据库是否存在
#==================

cursor.execute('show global variables like "%datadir%"')

cursor.fetchall()#[('datadir', 'D:\\Future_Data\\')]

================================================================

备注1:

import numpy as np
import pandas as pd
import pymysql

db_name='new_futures'

#连接
#==================
con = pymysql.connect(host = "127.0.0.1",user = "root",password = "root",
    # database = "new_futures",
    charset = 'utf8mb4', cursorclass = pymysql.cursors.DictCursor)
#==================
#获取游标
cursor = con.cursor()

#判断数据库是否存在
#==================
cursor.execute('show databases')
result=cursor.fetchall()
if ({'Database': db_name} in result)  or ((db_name,) in result):
    con.select_db(db_name)# 选择要操作的数据库
else:                                    # 创建数据库
    cursor.execute("create database if not exists "+db_name)
    con.commit()

args_str = cursor.mogrify("%s", 'datadir')
print(args_str)
cursor.execute('show global variables like'+args_str)

cursor.fetchall()
'datadir'
C:\ProgramData\Anaconda3\envs\tcy_p36\lib\site-packages\pymysql\cursors.py:329: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 517")
  self._do_get_result()
Out[39]: [{'Variable_name': 'datadir', 'Value': 'D:\\Future_Data\\'}]

 

 

 

你可能感兴趣的:(mysql)