报错:
DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': not all arguments converted during string formattingb
报错的代码如下:
import pymysql
import pandas as pd
con = pymysql.connect(host='localhost',user='root',password='pass',db='mydatabase',charset='utf8')
path = 'test.csv'
df = pd.read_csv(path,encoding='GBK')
df.to_sql(name='test',con=con)
to_sql 报错信息截图如下:
解决方法如下:
使用sqlalchemy 连接数据库。
import pymysql
import pandas as pd
from sqlalchemy import create_engine
con = create_engine('mysql+pymysql://root:pass@localhost:3306/mydatabase?charset=utf8')
pd.read_sql(sql,con)
path = 'test.csv'
df = pd.read_csv(path,encoding='GBK')
df.to_sql(name='test',con=con)