如何使用ExecuteMy python在mysql中插入整个数据帧

如何使用ExecuteMy python在mysql中插入整个数据帧,python,python-3.x,Python,Python 3.x,如何使用ExecuteMy在mymysql数据库中一次性插入newDF x=[ [[3141],[3141],[3169],[3251],[3285],[3302]], [[5000],[3141],[3169],[3251],[3285],[3302]] ] y=[ [[5],[7],[5],[2],[3],[8]], [[6],[5],[6],[5],[3],[6]] ] newDF=pd.DataFrame() newDF[['x']]=x newDF[[

如何使用ExecuteMy在my
mysql
数据库中一次性插入newDF

x=[
    [[3141],[3141],[3169],[3251],[3285],[3302]],
    [[5000],[3141],[3169],[3251],[3285],[3302]]
]

y=[
    [[5],[7],[5],[2],[3],[8]],
    [[6],[5],[6],[5],[3],[6]]
]
newDF=pd.DataFrame()
newDF[['x']]=x
newDF[['y']]=y`
sql = "INSERT INTO new_table (`x`,`y`) VALUES (?,?)" number_of_rows = cursor.executemany(sql,list(np.int64(newDF)))

我对执行官不熟悉。但是,我已经成功地使用pandas.dataframe.to_sql。你可以找到。在我的例子中,我使用sqlalchemy和pymysql库来实现这一点

这不是真正的代码,但应该是一个合理的大纲;考虑M为数据帧:

import numpy as np
import pandas as pd
import pymysql as mysql
import sqlalchemy
from sqlalchemy import create_engine
from pandas.io import sql

engine=create_engine('mysql+pymysql://username:password@host:port/db_name')
m.to_sql('table_name', engine, if_exists='append')

嘿,只是让你知道,使用
%s
操作符是一种不好的做法,如果你的输入中有一个被毒害了,你可能会度过糟糕的一天(这里有一个滑稽的例子:)。在Python中,您可以使用
运算符来执行所有
执行
类函数,以避免sql注入漏洞:(位于顶部附近)您需要在问题主体中编写一个实际问题,而不仅仅是在标题中。我如何检查像insert这样的字段序列到表中(字段1,字段2)我相信它与列名匹配。在您的情况下,它会将x&y与表匹配。我相信,如果当前没有与名称匹配的表,它也会创建一个新表。