谁能帮我? PANDAS和MySql

时间:2018-11-22 17:09:11

标签: python mysql pandas

我需要帮助。我是python中的新程序。

我的代码是:

import redis
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import time
import mysql.connector

mydb = mysql.connector.connect(
  host="XXXXXXXXXXXXXXXX",
  user="XXXXXXXXXXX",
  passwd="XXXXXXX"
)
df = pd.read_sql("SELECT * FROM XXXXXXXXXXXXXX;", mydb) 
df['AskTop'] = 2
df['BidTop'] = 2
df['Price'] = 2 
df['EMA_Corta'] = 2
df['EMA_Larga'] = 2
df['Señal'] = 2
df['positions'] = 2
df['Señal_Trading'] = 2
df['Precio_transaccion'] = 2
df['Posicion'] = "BUY"
df['Log_Ret'] = 2
df.to_sql(name = 'PEPEPEPEPEPE', con = mydb)
print (df)

我应该将此数据存入数据库,但是出现以下错误

  

“并非在SQL语句中使用了所有参数”)   pandas.io.sql.DatabaseError:对sql'SELECT name FROM执行失败   sqlite_master WHERE type ='table'AND name = ?;':并非所有参数都是   在SQL语句中使用

1 个答案:

答案 0 :(得分:0)

您应该使用df.loc[row,col]指定值。

  

con:sqlalchemy.engine.Engine或sqlite3.Connection doc#您由于连接类型错误而出错

import pandas as pd
from sqlalchemy import create_engine

mydb = create_engine("mysql://xx:xx@localhost/xxxx")

df = pd.read_sql("SELECT * FROM test;",con = mydb) 

df.loc[0,'posicion'] = "buy"

print(df) #If empty means you will insert nothing

df.to_sql(name = "test", con = mydb, index=False, if_exists='append')