-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure_mysql_connection.py
66 lines (54 loc) · 2.06 KB
/
azure_mysql_connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import mysql.connector
from mysql.connector import errorcode
# Obtain connection string information from the portal
config = {
'host':'*.mysql.database.azure.com',
'user':' ',
'password':' ',
'database':' '
}
# Construct connection string
try:
conn = mysql.connector.connect(**config)
print("Connection established")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with the user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cursor = conn.cursor()
#create table
# Drop previous table of same name if one exists
cursor.execute("DROP TABLE IF EXISTS inventory;")
print("Finished dropping table (if existed).")
# Create table
cursor.execute("CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);")
print("Finished creating table.")
# Insert some data into table
cursor.execute("INSERT INTO inventory (name, quantity) VALUES (%s, %s);", ("Man", 10))
print("Inserted",cursor.rowcount,"row(s) of data.")
cursor.execute("INSERT INTO inventory (name, quantity) VALUES (%s, %s);", ("Woman", 15))
print("Inserted",cursor.rowcount,"row(s) of data.")
cursor.execute("INSERT INTO inventory (name, quantity) VALUES (%s, %s);", ("Child", 10))
print("Inserted",cursor.rowcount,"row(s) of data.")
# Read data
cursor.execute("SELECT * FROM inventory;")
rows = cursor.fetchall()
print("Read", cursor.rowcount, "row(s) of data.")
# Print all rows
for row in rows:
print("Data row = (%s, %s, %s)" % (str(row[0]), str(row[1]), str(row[2])))
# Update a data row in the table
cursor.execute("UPDATE inventory SET quantity = %s WHERE name = %s;", (100, "Man"))
print("Updated",cursor.rowcount,"row(s) of data.")
# Delete a data row in the table
cursor.execute("DELETE FROM inventory WHERE name=%(param1)s;", {'param1':"Child"})
print("Deleted",cursor.rowcount,"row(s) of data.")
# Cleanup
conn.commit()
cursor.close()
conn.close()
print("Done.")