我正在尝试使用以下代码将 pandas 数据框写入 MySQL 数据库。
import pandas as pd
import numpy as np
from pandas.io import sql
import MySQLdb
df = pd.DataFrame([[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], list('AAABBBBABCBDDD'), [1.1, 1.7, 2.5, 2.6, 3.3, 3.8,4.0,4.2,4.3,4.5,4.6,4.7,4.7,4.8]]).T
db = MySQLdb.connect("192.168.56.101","nilani","123","test")
cursor = db.cursor()
cursor.execute("DROP TABLE IF EXISTS TEST")
sql = """CREATE TABLE TEST (
ID INT NOT NULL,
COL1 CHAR(20),
COL2 CHAR(20),
COL3 CHAR(20))"""
cursor.execute(sql)
sql.write_frame(df, con=db, name='TEST', flavor='mysql')
db.close()
我一直在提到这个问题和其他资源。无论如何我都会收到以下错误。会是什么原因?
sql.write_frame(df, con=db, name='TEST', flavor='mysql')
AttributeError: 'str' object has no attribute 'write_frame'