Created
February 8, 2016 19:05
-
-
Save technetbytes/321de4359c8bfb324382 to your computer and use it in GitHub Desktop.
3 Mysql Drivers for python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
hostname = '127.0.0.1' | |
username = 'root' | |
password = 'root' | |
database = 'schooldb' | |
# Simple routine to run a query on a database and print the results: | |
def doQuery( conn ) : | |
cur = conn.cursor() | |
cur.execute( "SELECT fname, lname FROM students" ) | |
for firstname, lastname in cur.fetchall() : | |
print firstname, lastname | |
print "Using MySQLdb…" | |
import MySQLdb | |
myConnection = MySQLdb.connect( host=hostname, user=username, passwd=password, db=database ) | |
doQuery( myConnection ) | |
myConnection.close() | |
print "Using pymysql…" | |
import pymysql | |
myConnection = pymysql.connect( host=hostname, user=username, passwd=password, db=database ) | |
doQuery( myConnection ) | |
myConnection.close() | |
print "Using mysql.connector…" | |
import mysql.connector | |
myConnection = mysql.connector.connect( host=hostname, user=username, passwd=password, db=database ) | |
doQuery( myConnection ) | |
myConnection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment