记录维维的每一篇Hello World

9.jpg

连接MySQL

  1. import pymysql
  2. sqlHost = "localhost" # 本地连接
  3. sqlUser = "root" # 用户
  4. sqlPassWord = "root" # 密码
  5. # 打开数据库连接
  6. db = pymysql.connect(host=sqlHost,
  7. user=sqlUser,
  8. password=sqlPassWord,
  9. charset='utf8')

创建数据库并创建数据表

  1. # python + pymysql 创建数据库
  2. import pymysql
  3. # 创建连接
  4. sqlHost = "localhost" # 本地连接
  5. sqlUser = "root" # 用户
  6. sqlPassWord = "root" # 密码
  7. sqlDatabase = "one" # 数据库名字
  8. # 打开数据库连接
  9. db = pymysql.connect(host=sqlHost,
  10. user=sqlUser,
  11. password=sqlPassWord,
  12. charset='utf8')
  13. # 创建游标
  14. cursor = db.cursor()
  15. # 创建数据库的sql(如果数据库存在就不创建,防止异常)
  16. sql = "CREATE DATABASE IF NOT EXISTS %s" % sqlDatabase
  17. # 执行创建数据库的sql
  18. cursor.execute(sql)
  19. db.commit()
  20. db.close()
  21. # 二次连接
  22. # 打开数据库连接
  23. db = pymysql.connect(host=sqlHost,
  24. user=sqlUser,
  25. password=sqlPassWord,
  26. database=sqlDatabase,
  27. charset='utf8')
  28. # 使用cursor()方法获取操作游标
  29. cursor = db.cursor()
  30. # 使用execute方法执行SQL语句
  31. cursor.execute("SELECT VERSION()")
  32. # 数据表名字
  33. sqlTableName = "peoples"
  34. # 如果数据表已经存在使用 execute() 方法删除表。
  35. cursor.execute(f"DROP TABLE IF EXISTS {sqlTableName}")
  36. # 创建数据表SQL语句
  37. sqlCreateTable = f"""CREATE TABLE {sqlTableName} (
  38. 姓名 CHAR(20) NOT NULL,
  39. 手机号 CHAR(40),
  40. 公司 CHAR(100),
  41. 工作 CHAR(40),
  42. 邮箱 CHAR(100));"""
  43. cursor.execute(sqlCreateTable)
  44. # 提交到数据库运行并关闭
  45. db.commit()
  46. db.close()

创建数据表并插入数据

  1. from faker import Faker
  2. import pymysql
  3. sqlHost = "localhost" # 本地连接
  4. sqlUser = "root" # 用户
  5. sqlPassWord = "root" # 密码
  6. sqlDatabase = "one" # 数据库名字
  7. # 打开数据库连接
  8. db = pymysql.connect(host=sqlHost,
  9. user=sqlUser,
  10. password=sqlPassWord,
  11. database=sqlDatabase,
  12. charset='utf8')
  13. # 使用cursor()方法获取操作游标
  14. cursor = db.cursor()
  15. # 使用execute方法执行SQL语句
  16. cursor.execute("SELECT VERSION()")
  17. # 数据表名字
  18. sqlTableName = "peoples"
  19. # 如果数据表已经存在使用 execute() 方法删除表。
  20. cursor.execute(f"DROP TABLE IF EXISTS {sqlTableName}")
  21. # 创建数据表SQL语句
  22. sqlCreateTable = f"""CREATE TABLE {sqlTableName} (
  23. 姓名 CHAR(20) NOT NULL,
  24. 手机号 CHAR(40),
  25. 公司 CHAR(100),
  26. 工作 CHAR(40),
  27. 邮箱 CHAR(100));"""
  28. cursor.execute(sqlCreateTable)
  29. # SQL 插入语句
  30. fd = Faker('zh_CN')
  31. for i in range(12):
  32. sqlInsertData = "INSERT INTO %s (姓名, 手机号, 公司, 工作, 邮箱)"\
  33. "VALUES ('%s', '%s', '%s', '%s', '%s');" % \
  34. (sqlTableName,
  35. fd.name(),
  36. fd.phone_number(),
  37. fd.bs(),
  38. fd.job(),
  39. fd.company_email())
  40. cursor.execute(sqlInsertData)
  41. # 提交到数据库执行
  42. db.commit()
  43. # 关闭数据库连接
  44. db.close()

查询数据

  1. from faker import Faker
  2. import pymysql
  3. sqlHost = "localhost" # 本地连接
  4. sqlUser = "root" # 用户
  5. sqlPassWord = "root" # 密码
  6. sqlDatabase = "one" # 数据库名字
  7. # 打开数据库连接
  8. db = pymysql.connect(host=sqlHost,
  9. user=sqlUser,
  10. password=sqlPassWord,
  11. database=sqlDatabase,
  12. charset='utf8')
  13. # 使用cursor()方法获取操作游标
  14. cursor = db.cursor()
  15. # 使用execute方法执行SQL语句
  16. cursor.execute("SELECT VERSION()")
  17. # 数据表名字
  18. sqlTableName = "peoples"
  19. # SQL 查询语句
  20. sqlSelectData = "SELECT * FROM %s" % sqlTableName
  21. try:
  22. cursor.execute(sqlSelectData)
  23. # 获取所有记录列表
  24. results = cursor.fetchall()
  25. for row in results:
  26. print(row)
  27. except:
  28. pass
  29. # 提交到数据库执行
  30. db.commit()
  31. # 关闭数据库连接
  32. db.close()

更新数据

  1. import pymysql
  2. sqlHost = "localhost" # 本地连接
  3. sqlUser = "root" # 用户
  4. sqlPassWord = "root" # 密码
  5. sqlDatabase = "one" # 数据库名字
  6. # 打开数据库连接
  7. db = pymysql.connect(host=sqlHost,
  8. user=sqlUser,
  9. password=sqlPassWord,
  10. database=sqlDatabase,
  11. charset='utf8')
  12. # 使用cursor()方法获取操作游标
  13. cursor = db.cursor()
  14. # 使用execute方法执行SQL语句
  15. cursor.execute("SELECT VERSION()")
  16. # 数据表名字
  17. sqlTableName = "peoples"
  18. # 更新语句
  19. sqlUpData = "UPDATE %s SET 公司 = 'New company'" % sqlTableName
  20. cursor.execute(sqlUpData)
  21. # 提交到数据库执行
  22. db.commit()
  23. # 关闭数据库连接
  24. db.close()

删除数据

  1. import pymysql
  2. sqlHost = "localhost" # 本地连接
  3. sqlUser = "root" # 用户
  4. sqlPassWord = "root" # 密码
  5. sqlDatabase = "one" # 数据库名字
  6. # 打开数据库连接
  7. db = pymysql.connect(host=sqlHost,
  8. user=sqlUser,
  9. password=sqlPassWord,
  10. database=sqlDatabase,
  11. charset='utf8')
  12. # 使用cursor()方法获取操作游标
  13. cursor = db.cursor()
  14. # 使用execute方法执行SQL语句
  15. cursor.execute("SELECT VERSION()")
  16. # 数据表名字
  17. sqlTableName = "peoples"
  18. # SQL删除记录语句
  19. sqlDeleteData = "DELETE FROM %s WHERE 工作 = '程序员'" % sqlTableName
  20. cursor.execute(sqlDeleteData)
  21. # 提交到数据库执行
  22. db.commit()
  23. # 关闭数据库连接
  24. db.close()

删除数据表

  1. import pymysql
  2. sqlHost = "localhost" # 本地连接
  3. sqlUser = "root" # 用户
  4. sqlPassWord = "root" # 密码
  5. sqlDatabase = "one" # 数据库名字
  6. # 打开数据库连接
  7. db = pymysql.connect(host=sqlHost,
  8. user=sqlUser,
  9. password=sqlPassWord,
  10. database=sqlDatabase,
  11. charset='utf8')
  12. # 使用cursor()方法获取操作游标
  13. cursor = db.cursor()
  14. # 使用execute方法执行SQL语句
  15. cursor.execute("SELECT VERSION()")
  16. # 数据表名字
  17. sqlTableName = "peoples"
  18. # table删除记录语句
  19. cursor.execute("DROP TABLE %s" % sqlTableName)
  20. # 提交到数据库执行
  21. db.commit()
  22. # 关闭数据库连接
  23. db.close()

删除数据库

  1. import pymysql
  2. sqlHost = "localhost" # 本地连接
  3. sqlUser = "root" # 用户
  4. sqlPassWord = "root" # 密码
  5. sqlDatabase = "one" # 数据库名字
  6. # 打开数据库连接
  7. db = pymysql.connect(host=sqlHost,
  8. user=sqlUser,
  9. password=sqlPassWord,
  10. database=sqlDatabase,
  11. charset='utf8')
  12. # 使用cursor()方法获取操作游标
  13. cursor = db.cursor()
  14. # 使用execute方法执行SQL语句
  15. cursor.execute("SELECT VERSION()")
  16. # database删除记录语句
  17. cursor.execute("DROP DATABASE %s" % 'one')
  18. # 提交到数据库执行
  19. db.commit()
  20. # 关闭数据库连接
  21. db.close()

注意事项

1、MySQL方言与python不兼容

2、增删改查等操作必须提交到数据库执行

3、数据库可视化工具推荐http://www.navicat.com.cn/download/navicat-premium,好用但需要付费

python环境

电脑:Windows10

python:3.10.4