Created
June 6, 2015 23:18
-
-
Save roblayton/a7a0977294fe6787834b to your computer and use it in GitHub Desktop.
Python server for writing names to a MySQL DB
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
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