查询数据.py 901 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. # -*- coding: UTF-8 -*-
  2. ##============ ============###
  3. import pymysql
  4. conn = pymysql.connect(
  5. host='a.xdc.ink',
  6. port=3306,
  7. user='xdc',
  8. passwd='Xdc123@a',
  9. db='cmd',
  10. charset='utf8mb4'
  11. )
  12. # 获取游标对象
  13. # cursor = conn.cursor()
  14. cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 获取的内容以字典形式显示
  15. query_sql = "select id,name,age from t1 where name=%s" # pymysql 自有的格式
  16. query_sql = "select id,name,age from t1 where {}=%s".format('name') # 同上
  17. row_nums = cursor.execute(query_sql, ('xdc'))
  18. print(row_nums)
  19. """
  20. 获取到数据结果具有迭代器到特效:
  21. 1、可以通过索引取值,可以切片
  22. 2、结果集中到数据每次取出一条就少一条
  23. """
  24. print(cursor.fetchmany(2)) # 获取两条数据
  25. print(cursor.fetchone()) # 获取一条数据
  26. print(cursor.fetchall()) # 获取所有
  27. cursor.close()
  28. conn.close()