无法使用Python mysqldb模块使MySQL源查询正常工作 [英] Can't get MySQL source query to work using Python mysqldb module
问题描述
我有以下几行代码:
sql = "source C:\\My Dropbox\\workspace\\projects\\hosted_inv\\create_site_db.sql"
cursor.execute (sql)
执行程序时,出现以下错误:
When I execute my program, I get the following error:
错误1064:您的SQL语法有错误;在第1行的'source C:\ My Dropbox \ workspace \ projects \ hosted_inv \ create_site_db.sql'附近检查与您的MySQL服务器版本相对应的手册以使用正确的语法
Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'source C:\My Dropbox\workspace\projects\hosted_inv\create_site_db.sql' at line 1
现在我可以将以下内容复制并粘贴到mysql中作为查询:
Now I can copy and past the following into mysql as a query:
source C:\\My Dropbox\\workspace\\projects\\hosted_inv\\create_site_db.sql
它运行完美.当我检查查询日志中由脚本执行的查询时,它表明我的查询如下:
And it works perfect. When I check the query log for the query executed by my script, it shows that my query was the following:
source C:\\My Dropbox\\workspace\\projects\\hosted_inv\\create_site_db.sql
但是,当我手动将其粘贴并执行时,整个create_site_db.sql会在查询日志中展开,并显示该文件中的所有sql查询.
However, when I manually paste it in and execute, the entire create_site_db.sql gets expanded in the query log and it shows all the sql queries in that file.
我在这里缺少有关mysqldb如何进行查询的内容吗?我遇到限制了吗?我的目标是运行一个sql脚本来创建架构结构,但是我不想在外壳进程中调用mysql来获取sql文件.
Am I missing something here on how mysqldb does queries? Am I running into a limitation. My goal is to run a sql script to create the schema structure, but I don't want to have to call mysql in a shell process to source the sql file.
有什么想法吗?谢谢!
推荐答案
正如其他人所说,您不能在MySQLdb Python API中使用命令source
As others said, you cannot use the command source
in MySQLdb Python API
因此,不要运行它,而是加载文件并执行
So, instead of running that, load the file and execute it
假设您的.sql文件具有
Lets say your .sql file has
create database test;
阅读
sql=open("test.sql").read()
然后执行它
cursor.execute(sql);
您将获得新的数据库测试"
You will get new database "test"
这篇关于无法使用Python mysqldb模块使MySQL源查询正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!