Commit ea64c9eb authored by aoh's avatar aoh
Browse files

Add read_sql.py



Add example code for how to read from sql.
Signed-off-by: aoh's avataraoh <aoh@lysator.liu.se>
parent c33ce0cc
#!/usr/bin/env python
import mysql.connector
from mysql.connector import errorcode
try:
cnx = mysql.connector.connect(user='lysns',
password='password',
port='2223',
host='hades',
database='lysns')
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cursor = cnx.cursor()
print("################################################################################")
print("Domain")
print("################################################################################")
query = ("SELECT id,name,uid,dirtybit,deletebit FROM domain")
cursor.execute(query)
for (id, name, uid, dirtybit, deletebit) in cursor:
print("{}, {}, {}, {}, {}".format(
id, name, uid, dirtybit, deletebit))
print("################################################################################")
print("Record types")
print("################################################################################")
query = ("SELECT id,name FROM record_types")
cursor.execute(query)
for (id, name) in cursor:
print("{}, {}".format(
id, name))
print("################################################################################")
print("Subdomain")
print("################################################################################")
query = ("SELECT id, name, ttl, class, rr, destination, domainid, dirtybit, deletebit FROM subdomain")
cursor.execute(query)
for (id, name, ttl, clas, rr, destination, domainid, dirtybit, deletebit) in cursor:
print("{}, {}, {}, {}, {}, {}, {}, {}".format(
id, name, ttl, clas, rr, destination, domainid, dirtybit, deletebit))
cursor.close()
cnx.close()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment