delete from all_stock_weekly where trade_date = STR_TO_DATE('20220513','%Y%m%d')
以上是在mysql(Navicat)中正常执行,在python环境中,我用了以下配置和语句,在不同的前提条件下报了几类错,反正就是不能执行:
import pandas as pd
import pymysql
from sqlalchemy import create_engine #导入了2类数据连接包
db=pymysql.Connect(host='127.0.0.1',user='root',passwd='######',db='myfinance')#数据库连接
engine=create_engine('mysql+pymysql://root:#####@127.0.0.1:3306/myfinance') #连接本机的金融mysql数据库
(一)、第一类错误:
我的一个df,用不同的引擎先导入后删除,报错:
df.to_sql("all_stock_weekly",engine,if_exists='append', index=False)#导入数据
pd.read_sql("delete from all_stock_weekly where trade_date < str_to_date('20220513','%Y%m%d')",db)#紧接着删除同样的数据
未能成功执行。错误提示如下:
405 if isinstance(pandas_sql, SQLiteDatabase):
--> 406 return pandas_sql.read_query(
407 sql,
408 index_col=index_col,
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\sql.py in read_query(self, sql, index_col, coerce_float, params, parse_dates, chunksize)
1632 args = _convert_params(sql, params)
1633 cursor = self.execute(*args)
-> 1634 columns = [col_desc[0] for col_desc in cursor.description]
1635
1636 if chunksize is not None:
TypeError: 'NoneType' object is not iterable
此时再返回Navicat中执行删除语句,报错“1205 - Lock wait timeout exceeded; try restarting transaction”
清除线程,Navicat正常删除了。
然后返回python,再次执行“pd.read_sql("delete from all_stock_weekly where trade_date < str_to_date('20220513','%Y%m%d')",db)#紧接着删除同样的数据”,报错:
Execution failed on sql: delete from all_stock_weekly where trade_date < str_to_date('20120511','%Y%m%d')
(2013, 'Lost connection to MySQL server during query ([WinError 10053] 你的主机中的软件中止了一个已建立的连接。)')
unable to rollback
请教各位,这是怎么回事?