用例图

image.png
image.png
image.pngimage.pngimage.pngimage.png
image.pngimage.png

  1. import mysql.connector
  2. connect = {
  3. "host": "localhost",
  4. "port": "3306",
  5. "user": "test",
  6. "password": "test",
  7. "database": "vega"
  8. }
  9. with mysql.connector.connect(**connect):
  10. pass

image.pngimage.png

  1. import mysql.connector
  2. config = {
  3. "host": "localhost",
  4. "port": 3306,
  5. "user": "test",
  6. "password": "test",
  7. "database": "vega"
  8. }
  9. with mysql.connector.connect(**config) as connect:
  10. cursor = connect.cursor()
  11. sql = "select username, password, email, role_id from t_user"
  12. cursor.execute(sql)
  13. for message in cursor:
  14. print(message)
  15. pass

image.pngimage.pngimage.pngimage.pngimage.png

  1. import mysql.connector
  2. config = {
  3. "host": "localhost",
  4. "port": 3306,
  5. "user": "test",
  6. "password": "test",
  7. "database": "vega"
  8. }
  9. with mysql.connector.connect(**config) as connect:
  10. username = "1 or 1=1"
  11. password = "1 or 1=1"
  12. sql = "select count(1) from t_user where username = %s and password = %s"
  13. cursor = connect.cursor()
  14. cursor.execute(sql, (username, password))
  15. for message in cursor:
  16. print(message)
  17. pass

image.png

  1. import mysql.connector
  2. config = {
  3. "host": "localhost",
  4. "port": 3306,
  5. "user": "test",
  6. "password": "test",
  7. "database": "vega"
  8. }
  9. try:
  10. with mysql.connector.connect(**config) as connect:
  11. sql = "insert into t_user(username, password, email, role_id) values(%s, HEX(AES_ENCRYPT(%s,'turbo')), %s, %s)"
  12. connect.start_transaction()
  13. cursor = connect.cursor()
  14. cursor.execute(sql, ("jack", "jack", "jack@163.com", 2))
  15. connect.commit()
  16. pass
  17. except Exception as e:
  18. if "connect" in dir():
  19. connect.rollback()
  20. print(e)

image.pngimage.pngimage.png

  1. import mysql.connector.pooling as pooling
  2. config = {
  3. "host": "localhost",
  4. "port": 3306,
  5. "user": "test",
  6. "password": "test",
  7. "database": "vega"
  8. }
  9. try:
  10. pool = pooling.MySQLConnectionPool(**config, pool_size=10)
  11. conn = pool.get_connection()
  12. conn.start_transaction()
  13. cursor = conn.cursor()
  14. sql = "update t_user set role_id = %s where id = %s"
  15. cursor.execute(sql, (1, 5))
  16. conn.commit()
  17. except Exception:
  18. if "conn" in dir():
  19. conn.rollback()
  20. print(e)
  1. import mysql.connector.pooling as pooling
  2. config = {
  3. "host": "localhost",
  4. "port": 3306,
  5. "user": "test",
  6. "password": "test",
  7. "database": "vega"
  8. }
  9. try:
  10. pool = pooling.MySQLConnectionPool(**config, pool_size=5)
  11. conn = pool.get_connection()
  12. conn.start_transaction()
  13. cursor = conn.cursor()
  14. sql = "delete from t_user where id = %s"
  15. cursor.execute(sql, (5,))
  16. conn.commit()
  17. except Exception as e:
  18. print(e)

image.png

  1. import mysql.connector.pooling as pooling
  2. config = {
  3. "host": "localhost",
  4. "port": 3306,
  5. "user": "test",
  6. "password": "test",
  7. "database": "vega"
  8. }
  9. try:
  10. pool = pooling.MySQLConnectionPool(**config, pool_size=3)
  11. conn = pool.get_connection()
  12. conn.start_transaction()
  13. cursor = conn.cursor()
  14. sql = "INSERT INTO t_user(USERNAME, PASSWORD, EMAIL, ROLE_ID) VALUES(%s, HEX(AES_ENCRYPT(%s,'trubo')), %s, %s)"
  15. value = [
  16. ["jack", "jack", "jack@163.com", 2],
  17. ["tom", "tom", "tom@163.com", 2],
  18. ["jerry", "jerry", "jerry@163.com", 2]
  19. ]
  20. cursor.executemany(sql, value)
  21. conn.commit()
  22. except Exception as e:
  23. print(e)
  24. pass