使用python3使用SQLALchemy报错"BLOB/TEXT column ‘code’ used in key specification without a key length
代码如下:
df = ts.get_stock_basics() #获取所有股票基本信息。
engine = create_engine('mysql://root:[email protected]/finance_data?charset=utf8')
df.to_sql('stock_basics',engine)
报错如下:
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1170, "BLOB/TEXT column 'code' used in key specification without a key length") [SQL: u'CREATE INDEX ix_stock_basics_code ON stock_basics (code)']
在mysql中,BLOB/TEXT类型字段设置索引时,只能为BLOB/TEXT数据的前N个字符
使用
df.to_sql('stock_basics',engine,if_exists='append',dtype={'code':VARCHAR(length=6)})
限定code字段的字符数
from sqlalchemy import create_engine
from sqlalchemy.types import VARCHAR
import tushare as ts
engine =create_engine('mysql+pymysql://root:psw@s1/test?charset=utf8')
df =ts.get_stock_basics()
df.to_sql('stock_basics',engine,if_exists='append',dtype={'code':VARCHAR(length=6)})