Zhiguang Huo (Caleb)
Tuesday Nov 21st, 2023
object: use python to access mysql
pre-requisit:
## pip install mysql
## pip install mysql-connector
## pip install mysql-connector-python
import mysql.connector
https://chat.openai.com/share/18cd0931-81e6-4be4-b3a1-40a73e5232d3
select MySQL
MYSQL_ADDON_HOST="bpujmkvwzwgpe3ppgpeg-mysql.services.clever-cloud.com"
MYSQL_ADDON_DB="bpujmkvwzwgpe3ppgpeg"
MYSQL_ADDON_USER="uryjoqt0ohxsfzsn"
MYSQL_ADDON_PORT="3306"
MYSQL_ADDON_PASSWORD='hCkQynjDSNXpzUbpYUjL'
MYSQL_ADDON_URI="mysql://uryjoqt0ohxsfzsn:hCkQynjDSNXpzUbpYUjL@bpujmkvwzwgpe3ppgpeg-mysql.services.clever-cloud.com:3306/bpujmkvwzwgpe3ppgpeg"
import mysql.connector
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
print(myconn)
## <mysql.connector.connection.MySQLConnection object at 0x13aa86350>
## <mysql.connector.connection.MySQLConnection object at 0x13aa86350>
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
mycursor.execute("CREATE DATABASE mydatabase")
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
mycursor.execute("SHOW DATABASES")
for db_name in mycursor:
print(db_name)
## (bytearray(b'bpujmkvwzwgpe3ppgpeg'),)
## (bytearray(b'information_schema'),)
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
mycursor.execute("SHOW TABLES")
for table_name in mycursor:
print(table_name)
## (bytearray(b'customers'),)
## (bytearray(b'products'),)
## (bytearray(b'users'),)
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
mycursor.execute("DROP TABLE customers")
mycursor.execute("SHOW TABLES")
for table_name in mycursor:
print(table_name)
## (bytearray(b'products'),)
## (bytearray(b'users'),)
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
sql = "DROP TABLE IF EXISTS customers"
mycursor.execute(sql)
mycursor.execute("SHOW TABLES")
for table_name in mycursor:
print(table_name)
## (bytearray(b'products'),)
## (bytearray(b'users'),)
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
mycursor.execute("CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))")
mycursor.execute("SHOW TABLES")
for table_name in mycursor:
print(table_name)
## (bytearray(b'customers'),)
## (bytearray(b'products'),)
## (bytearray(b'users'),)
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
# sql = "INSERT INTO customers (name, address) VALUES ('John', 'Highway21')"
# mycursor.execute(sql)
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val)
myconn.commit() ## commit changes to the database
print(mycursor.rowcount, "record inserted.")
## 1 record inserted.
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
## (1, 'John', 'Highway 21')
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [
('Peter', 'Lowstreet 4'),
('Amy', 'Apple st 652'),
('Hannah', 'Mountain 21'),
('Michael', 'Valley 345'),
('Sandy', 'Ocean blvd 2'),
('Betty', 'Green Grass 1'),
('Richard', 'Sky st 331'),
('Susan', 'One way 98'),
('Vicky', 'Yellow Garden 2'),
('Ben', 'Park Lane 38'),
('William', 'Central st 954'),
('Chuck', 'Main Road 989'),
('Viola', 'Sideway 1633')
]
mycursor.executemany(sql, val)
myconn.commit()
print(mycursor.rowcount, "was inserted.")
## 13 was inserted.
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
## (1, 'John', 'Highway 21')
## (2, 'Peter', 'Lowstreet 4')
## (3, 'Amy', 'Apple st 652')
## (4, 'Hannah', 'Mountain 21')
## (5, 'Michael', 'Valley 345')
## (6, 'Sandy', 'Ocean blvd 2')
## (7, 'Betty', 'Green Grass 1')
## (8, 'Richard', 'Sky st 331')
## (9, 'Susan', 'One way 98')
## (10, 'Vicky', 'Yellow Garden 2')
## (11, 'Ben', 'Park Lane 38')
## (12, 'William', 'Central st 954')
## (13, 'Chuck', 'Main Road 989')
## (14, 'Viola', 'Sideway 1633')
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
mycursor.execute("SELECT * FROM customers LIMIT 5")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
## (1, 'John', 'Highway 21')
## (2, 'Peter', 'Lowstreet 4')
## (3, 'Amy', 'Apple st 652')
## (4, 'Hannah', 'Mountain 21')
## (5, 'Michael', 'Valley 345')
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
mycursor.execute("SELECT * FROM customers LIMIT 5 OFFSET 2")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
## (3, 'Amy', 'Apple st 652')
## (4, 'Hannah', 'Mountain 21')
## (5, 'Michael', 'Valley 345')
## (6, 'Sandy', 'Ocean blvd 2')
## (7, 'Betty', 'Green Grass 1')
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
mycursor.execute("SELECT address, name FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
## ('Highway 21', 'John')
## ('Lowstreet 4', 'Peter')
## ('Apple st 652', 'Amy')
## ('Mountain 21', 'Hannah')
## ('Valley 345', 'Michael')
## ('Ocean blvd 2', 'Sandy')
## ('Green Grass 1', 'Betty')
## ('Sky st 331', 'Richard')
## ('One way 98', 'Susan')
## ('Yellow Garden 2', 'Vicky')
## ('Park Lane 38', 'Ben')
## ('Central st 954', 'William')
## ('Main Road 989', 'Chuck')
## ('Sideway 1633', 'Viola')
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
# sql = "SELECT * FROM customers WHERE address = 'Yellow Garden 2'"
# mycursor.execute(sql)
sql = "SELECT * FROM customers WHERE address = %s"
adr = ("Yellow Garden 2", )
mycursor.execute(sql, adr)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
## (10, 'Vicky', 'Yellow Garden 2')
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
sql = "SELECT * FROM customers WHERE address LIKE '%way%'"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
## (1, 'John', 'Highway 21')
## (9, 'Susan', 'One way 98')
## (14, 'Viola', 'Sideway 1633')
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
sql = "SELECT * FROM customers ORDER BY name"
#sql = "SELECT * FROM customers ORDER BY name DESC" ## descending order
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
## (3, 'Amy', 'Apple st 652')
## (11, 'Ben', 'Park Lane 38')
## (7, 'Betty', 'Green Grass 1')
## (13, 'Chuck', 'Main Road 989')
## (4, 'Hannah', 'Mountain 21')
## (1, 'John', 'Highway 21')
## (5, 'Michael', 'Valley 345')
## (2, 'Peter', 'Lowstreet 4')
## (8, 'Richard', 'Sky st 331')
## (6, 'Sandy', 'Ocean blvd 2')
## (9, 'Susan', 'One way 98')
## (10, 'Vicky', 'Yellow Garden 2')
## (14, 'Viola', 'Sideway 1633')
## (12, 'William', 'Central st 954')
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
sql = "DELETE FROM customers WHERE address = 'Mountain 21'"
mycursor.execute(sql)
myconn.commit()
print(mycursor.rowcount, "record(s) deleted")
## 1 record(s) deleted
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
sql = "DELETE FROM customers WHERE address = %s"
adr = ("Yellow Garden 2", )
mycursor.execute(sql, adr)
myconn.commit()
print(mycursor.rowcount, "record(s) deleted")
## 1 record(s) deleted
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley 345'"
mycursor.execute(sql)
myconn.commit()
print(mycursor.rowcount, "record(s) affected")
## 1 record(s) affected
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
sql = "UPDATE customers SET address = %s WHERE address = %s"
val = ("Valley 345", "Canyon 123")
mycursor.execute(sql, val)
myconn.commit()
print(mycursor.rowcount, "record(s) affected")
## 1 record(s) affected
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
sql = "DROP TABLE IF EXISTS users"
mycursor.execute(sql)
mycursor.execute("CREATE TABLE users (id INT(255), name VARCHAR(255), fav INT(255))")
sql = "INSERT INTO users (id, name, fav) VALUES (%s, %s, %s)"
val = [
(1, 'John', 154),
(2, 'Peter', 154),
(3, 'Amy', 155),
(4, 'Hannah', 167),
(5, 'Michael', 189)
]
mycursor.executemany(sql, val)
myconn.commit()
print(mycursor.rowcount, "was inserted.")
## 5 was inserted.
mycursor.execute("SELECT * FROM users LIMIT 5")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
## (1, 'John', 154)
## (2, 'Peter', 154)
## (3, 'Amy', 155)
## (4, 'Hannah', 167)
## (5, 'Michael', 189)
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
sql = "DROP TABLE IF EXISTS products"
mycursor.execute(sql)
mycursor.execute("CREATE TABLE products (id INT(255), name VARCHAR(255))")
sql = "INSERT INTO products (id, name) VALUES (%s, %s)"
val = [
(154, 'Chocolate Heaven'),
(155, 'Tasty Lemons'),
(156, 'Vanilla Dreams')
]
mycursor.executemany(sql, val)
myconn.commit()
print(mycursor.rowcount, "was inserted.")
## 3 was inserted.
mycursor.execute("SELECT * FROM products LIMIT 5")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
## (154, 'Chocolate Heaven')
## (155, 'Tasty Lemons')
## (156, 'Vanilla Dreams')
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
sql = "SELECT * \
FROM users \
INNER JOIN products ON users.fav = products.id"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
## (1, 'John', 154, 154, 'Chocolate Heaven')
## (2, 'Peter', 154, 154, 'Chocolate Heaven')
## (3, 'Amy', 155, 155, 'Tasty Lemons')
myconn = mysql.connector.connect(
host=MYSQL_ADDON_HOST,
user=MYSQL_ADDON_USER,
password=MYSQL_ADDON_PASSWORD,
port=MYSQL_ADDON_PORT,
database=MYSQL_ADDON_DB
)
mycursor = myconn.cursor()
sql = "SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
INNER JOIN products ON users.fav = products.id"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
## ('John', 'Chocolate Heaven')
## ('Peter', 'Chocolate Heaven')
## ('Amy', 'Tasty Lemons')