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

时间:2015-09-23 14:47:30

标签: pymysql

我试图运行一个通过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')

3 个答案:

答案 0 :(得分:1)

修改我的代码:

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风格,所以我确保将来会使用它。

答案 1 :(得分:0)

有时,当数据库URI的构建不正确时,可能仅会发生这种情况。对我来说,当我的pymysql连接字符串(URL)看起来像这样时发生了错误:

mysql+pymysql://:@localhost/

要修复它,它必须看起来像:

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

答案 2 :(得分:0)

如果未选择要连接的数据库,则必须添加此行。

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)