"pymysql.err.InternalError: (1046, u'No database selected')" error message when running Python script from command line

"pymysql.err.InternalError: (1046, u'No database selected')" error message when running Python script from command line

我正在尝试 运行 一个通过 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风格,以后一定会用的

有时,当数据库 URI 构建不正确时,这可能会发生。对我来说,当我的 pymysql 连接字符串 (URL) 如下所示时发生错误:

mysql+pymysql://:@localhost/

要修复它,它需要看起来像:

mysql+pymysql://<my-database-user-name>:@localhost/<my-database-name> # 或将 localhost 替换为您的特定主机名

如果你没有select连接中的数据库,你必须添加这一行。

connection = pymysql.connect(host=DB_host, user=DB_user, password=DB_password)  # NO DB_NAME SPECIFED
cursor = connection.cursor()
cursor.execute(f'CREATE DATABASE IF NOT EXISTS {DB_NAME}')  # ADD THIS LINE!
connection.select_db(DB_NAME)