2017-08-30 79 views
1

我正在尝试将数据框写入MySQL表。这里是我的代码:使用sqlalchemy通过pandas.to_sql写入到mysql数据库时遇到麻烦,但没有通过没有熊猫的sqlalchemy写入

import pandas as pd 
import sqlalchemy 
connectString="""mysql+pymysql://userName:[email protected]/schema""" 
engine = sqlalchemy.create_engine(connnectString) 
connection=engine.connect() 
myDataFrame.loc[:,['col1','col2']].to_sql('myTable',con=engine,index=True,if_exists='fail') 

代码失败,看起来像一个权限错误。

OperationalError: (pymysql.err.OperationalError) (1142, "CREATE command denied to user 'userName'@'hostIP' for table 'myTable'") [SQL: '\nCREATE TABLE `myTable` (\n\ID BIGINT, \n\t`col1` BIGINT, \n\t`col2` BIGINT\n)\n\n'] 

要测试权限问题,我能够成功地创建并写入一个表,当我运行以下命令:我在数据库上

import pandas as pd 
import sqlalchemy 
connectString="""mysql+pymysql://userName:[email protected]/schema""" 
engine = sqlalchemy.create_engine(connnectString) 
connection=engine.connect() 
command="""create table myTable as 
    SELECT * FROM anotherTable""" 
engine.execute(command) 
connection.close() 

此外,“秀助学金”命令试图写信给我显示我有写权限。数据库安装在红帽企业Linux服务器上,我正在运行来自在同一操作系统上打开的jupyter笔记本中的命令,但在另一台计算机上运行。

任何想法发生了什么?你需要其他信息吗?谢谢。

+0

我昨天有这个问题,我只能使用Windows身份验证字符串格式而不是用户名:密码修复它,但这可能不会对您有所帮助。 – bphi

+0

你是否已经从__same__机器运行第二个脚本作为第一个脚本? – MaxU

+0

是的,在同一台机器上,在同一个jupyter笔记本。 – mootwo7

回答

1

问题是我没有权限在某些模式下编写,但在其他模式下有权限。

看看我上面的代码:

connectString="""mysql+pymysql://userName:[email protected]/schema""" 

在这里,我没有对“模式”写入权限,但具有读取权限。当我将创建使用表:

command="""create table myTable as 
SELECT * FROM anotherTable""" 

“mytable的”竟是:'anotherSchemaWhereIhaveWritePermission“表名”

和MySQL将正确地创建表“表名”在这个其他架构。所以在这里,初始连接中指定的模式并不重要。

当我将运行在大熊猫的所谓等效命令:“'anotherSchemaWhereIhaveWritePermission‘表名’”,在原有的架构

myDataFrame.loc[:,['col1','col2']].to_sql('myTable',con=engine,index=True,if_exists='fail') 

这里,SQL会尝试创建一个名为表我连接,其中我没有写入权限。因此错误。

相关问题