MySQLdb:熊猫数据帧到 SQL 数据库错误:并非所有参数在字符串格式化期间转换



所以,我想将数据帧移动到数据库中的表中,但我遇到了一些问题。我使用 pandas 库制作了数据帧,并希望将数据上传到数据库中的表中。

我的代码的相关片段和错误详细信息如下:

#CONNECTION TO SQL DATABASE
db=sql.connect(host="localhost",user="root",passwd="password",db="database_name")
cur=db.cursor()
#CONVERTING DATAFRAME(df_final) INTO TABLE
df_final.to_sql(con=db, name='finaltable', if_exists='replace', chunksize=5000)

错误详细信息:

Traceback (most recent call last):
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesM
ySQLdbcursors.py", line 238, in execute
query = query % args
TypeError: not all arguments converted during string formatting
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesp
andasiosql.py", line 1400, in execute
cur.execute(*args)
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesM
ySQLdbcursors.py", line 240, in execute
self.errorhandler(self, ProgrammingError, str(m))
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesM
ySQLdbconnections.py", line 52, in defaulterrorhandler
raise errorclass(errorvalue)
_mysql_exceptions.ProgrammingError: not all arguments converted during string fo
rmatting
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pt.py", line 77, in <module>
df_final.to_sql(con=db, name='finaltable', if_exists='replace', chunksize=50
00)
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesp
andascoregeneric.py", line 2127, in to_sql
dtype=dtype)
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesp
andasiosql.py", line 450, in to_sql
chunksize=chunksize, dtype=dtype)
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesp
andasiosql.py", line 1502, in to_sql
table.create()
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesp
andasiosql.py", line 561, in create
if self.exists():
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesp
andasiosql.py", line 549, in exists
return self.pd_sql.has_table(self.name, self.schema)
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesp
andasiosql.py", line 1514, in has_table
return len(self.execute(query, [name, ]).fetchall()) > 0
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesp
andasiosql.py", line 1412, in execute
raise_with_traceback(ex)
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesp
andascompat__init__.py", line 403, in raise_with_traceback
raise exc.with_traceback(traceback)
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesp
andasiosql.py", line 1400, in execute
cur.execute(*args)
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesM
ySQLdbcursors.py", line 240, in execute
self.errorhandler(self, ProgrammingError, str(m))
File "C:UsersDELLAppDataLocalProgramsPythonPython36libsite-packagesM
ySQLdbconnections.py", line 52, in defaulterrorhandler
raise errorclass(errorvalue)
pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT name FROM 
sqlite_ma
ster WHERE type='table' AND name=?;': not all arguments converted during string
formatting

因此,由于更新,显然我的方法已经过时了,因此对于连接位,我使用了"sqlalchemy"库中的"create_engine"而不是"mysqldb.connect(("来访问数据库:

#relevant import:
from sqlalchemy import create_engine
#accessing database:
engine = create_engine("mysql://root:PASSWORD@host/database_name")
con = engine.connect()
#uploading dataframe to database:
df.to_sql(con=con, name='final_table', if_exists='replace', chunksize=10000)

对我来说,数据中似乎有空值。尝试在调用 .to_sql(( 之前填充数据帧中的空值

df_final.fillna('default string')

或者删除空行

df_final.dropna(inplace=True)

最新更新