我是MySQL和熊猫的新手。
我创建了一个随机数据框:
const tenderTableService : TenderTableService = new TenderTableService();
const tenderList= tenderTableService.fetchOngoingTenders();
console.log(tenderList); // -> Promise { <state>: "pending" }
我想将其插入我的本地MySQL数据库:
data_frame = pd.DataFrame(np.random.randint(
0, 100, size=(100, 4)), columns=list('ABCD'))
db = mysql.connector.connect(host="localhost",
user='root',
password='pw123',
database='analytics',
port='3307',
auth_plugin='mysql_native_password')
但是我得到一个错误
data_frame.to_sql(name='test',
con=db, if_exists='append', index=False)
如果我使用sqlalchemy:
pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': Not all parameters were used in the SQL statement
我收到此错误:
engine = create_engine("mysql://root:pw123@localhost/analytics")
con = engine.connect()
df = pd.DataFrame(['A', 'B'], columns=['new_tablecol'])
df.to_sql(name='new_table', con=con, if_exists='append')
con.close()
我知道我使用的是正确的密码。此时,我不知道如何将数据框插入本地数据库。
谢谢您的帮助。
答案 0 :(得分:0)
最终使用:
def get_report(analytics):
"""Queries the Analytics Reporting API V4.
Args:
analytics: An authorized Analytics Reporting API V4 service object.
Returns:
The Analytics Reporting API V4 response.
"""
return analytics.userActivity().search(
body = {
"viewId": VIEW_ID,
"user": {
"type": "CLIENT_ID",
"userId": "310383817.1547668323"
},
"dateRange": {
"startDate": "2019-01-30",
"endDate": "2019-02-01",
}
}
).execute()
´´´
必须导入
coffee-rails
然后最终在我的cronjobs中将to_csv替换为to_sql:
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
engine = create_engine(
'mysql+pymysql://root:password@localhost:3307/analytics')
data_frame = pd.DataFrame(np.random.randint(
0, 100, size=(100, 4)), columns=list('ABCD'))
data_frame.to_sql('new', con=engine)
哪个工作正常。