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