是的,你可以在同一个连接,而无需指定数据库名称:
cnx = mysql.connector.connect(user='scott', password='tiger', host='127.0.0.1')
这将是一样的使用从终端连接:
的mysql -h 127.0 .0.1 -u scott -ptiger
注意:127.0.0.1是您的本地主机。
此外,我通常不会将实际的连接信息存储在脚本中。我会做更像这样的事情(如果可以的话):
def CloseConnection(cnxIn, cursorIn):
cursorIn.close()
cnxIn.close
return
user = input('Enter your user name: ')
user = user.strip()
password = getpass.getpass()
host = input('Enter the host name: ')
host = host.strip()
cnx = mysql.connector.connect(user=user, password=password, host=host)
cursor = cnx.cursor(buffered=True)
cursor.execute ('select VERSION()')
row = cursor.fetchone()
CloseConnection(cnx, cursor)