当前位置:Gxlcms > 数据库问题 > pymysql

pymysql

时间:2021-07-01 10:21:17 帮助过:6人阅读

# -*- coding: UTF-8 -*-

import pymysql

#  创建连接
conn = pymysql.connect(host=‘172.16.200.49‘, port=3306,
                       user=‘bigberg‘, password=‘111111‘,
                       db=‘study‘)

# 创建游标
cursor = conn.cursor()

# 执行SQL,并返回受影响行数
effect_row = cursor.execute("select * from student")
print(effect_row)
# 执行SQL,并返回受影响行数
# data = [
#     (‘S1‘,‘2018-04-21‘,‘M‘),
#     (‘S2‘,‘2018-05-11‘,‘F‘),
#     (‘S3‘,‘2018-04-25‘,‘M‘)
# ]
# sql = "insert into student (name,register_date,gender) values (%s,%s,%s)"
# effect_row = cursor.executemany(sql,data)
# 提交,不然无法保存
conn.commit()

# 关闭游标
cursor.close()

# 关闭连接
conn.close()

  3.2 获取查询的数据

# -*- coding: UTF-8 -*-

import pymysql

#  创建连接
conn = pymysql.connect(host=‘172.16.200.49‘, port=3306,
                       user=‘bigberg‘, password=‘111111‘,
                       db=‘study‘)

cursor = conn.cursor()
sql = "select * from student where stu_id>3"
cursor.execute(sql)
# 获取第一行数据
# result = cursor.fetchone()

# 获取前n条数据
# result = cursor.fetchmany(3)

# 获取所有数据
result = cursor.fetchall()
print(result)

conn.commit()
cursor.close()
conn.close()

  3.3 fetch数据类型

import pymysql

#  创建连接
conn = pymysql.connect(host=‘172.16.200.49‘, port=3306,
                       user=‘bigberg‘, password=‘111111‘,
                       db=‘study‘,)
# 游标设置为字典类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

  

 

pymysql

标签:etc   man   str   strong   行数据   comm   div   exec   3.3   

人气教程排行