1. # 安装pymsql
  2. pip install pymysql
  1. --登录接口造数sql语句
  2. --login_01、测试成功登录、把test01、123456加密先删除后写入users表
  3. delete from users where username='test01'
  1. # 导入
  2. import pymysql
  3. # 初始化数据库
  4. # 连接数据库
  5. '''
  6. 在客户机中申请内存,把数据服务器上一个具体的数据库,拷贝到客户机的内存中
  7. '''
  8. dbinfo = {'host': '真实的ip地址', 'db': '数据库名字', 'user': '数据库的账号', 'passwd': '数据库的密码'}
  9. conn = pymysql.connect(**dbinfo) # 连接数据库,**DBinfo标识解包字典,变成host=的形式
  10. # 创建游标
  11. '''
  12. 在客户机内存中继续申请内存,用来将来存储数据操作的结果(增删改查)
  13. '''
  14. cursor = conn.cursor()
  15. # 指定sql语句
  16. '''
  17. 必须是字符串类型
  18. '''
  19. file = open('login.sql', encoding='utf-8')
  20. # 执行sql语句
  21. '''
  22. 执行的结果会存储到游标中
  23. '''
  24. for sql in file:
  25. # 去除开头和结尾的空格,并且排除备注
  26. if sql.strip() and sql.strip()[:2] != '--':
  27. # 执行sql
  28. cursor.execute(sql)
  29. # print(sql) # delete from users where username='test01'
  30. # 获取数据库实际行数,从游标中获取一行数据(元祖)
  31. db_actual=cursor.fetchone()
  32. # print(db_actual) # (1, 1)
  33. file.close()
  34. # 提交数据
  35. '''
  36. 把客户机内存中的数据存储到数据库服务器中
  37. '''
  38. conn.commit()
  39. # 断开数据库连接
  40. '''
  41. 释放客户机申请的内存,清理数据
  42. '''
  43. conn.close()

无备注```python

导入

import pymysql

dbinfo = {‘host’: ‘真实的ip地址’, ‘db’: ‘数据库名字’, ‘user’: ‘root’, ‘passwd’: ‘123456’} conn = pymysql.connect(**dbinfo)
cursor = conn.cursor() file = open(‘login.sql’, encoding=’utf-8’) for sql in file: if sql.strip() and sql.strip()[:2] != ‘—‘: cursor.execute(sql)

  1. # print(sql) # delete from users where username='test01'
  2. db_actual=cursor.fetchone()
  3. # print(db_actual) # (1, 1)

file.close() conn.commit() conn.close()

```