python - general connection to a mysql server -
is there way make general connection mysql server , not 1 of databases? found following code snippet. connect method connects specific database called employees
.
import mysql.connector cnx = mysql.connector.connect(user='scott', password='tiger', host='127.0.0.1', database='employees') cnx.close()
yes, can make same connection without specifying database name:
cnx = mysql.connector.connect(user='scott', password='tiger', host='127.0.0.1')
it same connecting terminal using:
mysql -h 127.0.0.1 -u scott -ptiger
note: 127.0.0.1 localhost.
also, not store actual connection information in script. more (if can):
def closeconnection(cnxin, cursorin): cursorin.close() cnxin.close return user = input('enter user name: ') user = user.strip() password = getpass.getpass() host = input('enter 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)
Comments
Post a Comment