gpt4 book ai didi

pymysql - "pymysql.err.InternalError: (1046, u' 没有选择数据库从命令行运行 Python 脚本时出现 ')"错误消息

转载 作者:行者123 更新时间:2023-12-04 02:18:17 26 4
gpt4 key购买 nike

我正在尝试运行通过 PyMySQL 连接到 MySQL 数据库的 Python 脚本。该脚本有效:

import pymysql
cnx = pymysql.connect(read_default_file = "/directory/my.cnf", cursorclass = pymysql.cursors.DictCursor)
cursor = cnx.cursor()
# Do stuff.

当我在解释器中运行脚本时,我没有收到任何错误,但是当我尝试从命令行运行它时,我收到以下错误:

Traceback (most recent call last):
File "s02_prepare_data_RNN.py", line 264, in <module>
(omniture, urls, years, global_regions) = get_omniture_data("omniture_results")
File "s02_prepare_data_RNN.py", line 76, in get_omniture_data
sso_to_accountid = get_sso_accountids()
File "s02_prepare_data_RNN.py", line 31, in get_sso_accountids
cursor.execute(query)
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/cursors.py", line 134, in execute
result = self._query(query)
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/cursors.py", line 282, in _query
conn.query(q)
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/connections.py", line 768, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/connections.py", line 929, in _read_query_result
result.read()
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/connections.py", line 1125, in read
first_packet = self.connection._read_packet()
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/connections.py", line 893, in _read_packet
packet.check_error()
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/connections.py", line 369, in check_error
err.raise_mysql_exception(self._data)
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/err.py", line 120, in raise_mysql_exception
_check_mysql_exception(errinfo)
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/err.py", line 115, in _check_mysql_exception
raise InternalError(errno, errorvalue)
pymysql.err.InternalError: (1046, u'No database selected')

最佳答案

修改我的代码:

import pymysql
cnx = pymysql.connect(read_default_file = "/directory/my.cnf", cursorclass = pymysql.cursors.DictCursor)
cursor = cnx.cursor()
# Do stuff.

到:

import pymysql
cnx = pymysql.connect(read_default_file = "/directory/my.cnf", cursorclass = pymysql.cursors.DictCursor)
cursor = cnx.cursor()
if __name__ == "__main__":
# Do stuff.

修复了错误。我认为 Python 可能会在建立连接之前尝试执行查询,所以我尝试将我的程序的主要部分放在 if __name__ == "__main__": 下并修复它。不过,我仍然不是 100% 正在发生的事情。我原以为代码会等待建立连接,然后再继续执行以下几行,但此修复表明情况并非如此。

还值得注意的是,我只是在具有 Python 2.6 的服务器上从命令行运行原始脚本时才收到错误。当我在装有 Python 2.7 的本地计算机上从命令行运行原始脚本时,我没有收到错误。

无论如何,if __name__ == "__main__": 是很好的 Python 风格,所以我一定会在未来使用它。

关于pymysql - "pymysql.err.InternalError: (1046, u' 没有选择数据库从命令行运行 Python 脚本时出现 ')"错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32742747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com