Skip to content

Instantly share code, notes, and snippets.

@roblayton
Created June 6, 2015 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roblayton/a7a0977294fe6787834b to your computer and use it in GitHub Desktop.
Save roblayton/a7a0977294fe6787834b to your computer and use it in GitHub Desktop.
Python server for writing names to a MySQL DB
import MySQLdb
import argparse
db = MySQLdb.connect(host="192.168.33.10", user="test", passwd="password", db="test")
cursor = db.cursor()
parser = argparse.ArgumentParser(description="Process some strings.")
parser.add_argument("firstname", type=str, help="firstname")
parser.add_argument("lastname", type=str, help="lastname")
args = parser.parse_args()
firstname = args.firstname
lastname = args.lastname
if firstname and lastname:
try:
cursor.execute("INSERT INTO test.name (firstname,lastname) VALUES (%s,%s)", (firstname, lastname))
db.commit()
except MySQLdb.OperationalError as err:
db.rollback()
raise err
cursor.close()
db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment