How to connect Python3.4.1 to Mysql 5.1 ?

jaahush at gmail.com jaahush at gmail.com
Fri Jun 27 13:50:33 EDT 2014


I am sure you could google this
but here's what I've done in the past


1) file 1 has the login info.
   I make it to prompt for user's password
   (you could hardcode the password in this file)

import getpass

USERNAME = "yourusername"
DBNAME   = "yourdatabasename"
PASSWORD = getpass.getpass("Enter password for database {0}: ".format(DBNAME))

login_info = {
               'host'     : "your.sql.server.domain.name",
               'user'     : USERNAME,
               'password' : PASSWORD,
               'database' : DBNAME,
               'port'     : yoursqlport,
             }

2) file 2 has the SQL code

import mysql.connector
from file_1_above import login_info

db = mysql.connector.Connect(**login_info)
cursor = db.cursor()


>From here on, you use "cursor" to operate on your database.





More information about the Python-list mailing list