Skip to content

Instantly share code, notes, and snippets.

@technetbytes
Created February 8, 2016 19:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technetbytes/321de4359c8bfb324382 to your computer and use it in GitHub Desktop.
Save technetbytes/321de4359c8bfb324382 to your computer and use it in GitHub Desktop.
3 Mysql Drivers for python
#!/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