wbf980728

python连接mysql数据库

1. 先下载pymysqll模块,进入命令提示符界面运行以下命令

 

 在python中执行以下命令可查询数据库

# import pymysql#调用模块
# db = pymysql.connect(user = \'root\',password=\'wbf980728\',database=\'wang\',charset=\'utf8\')#打开数据库连接
# cursor = db.cursor()#获取操作游标
# demo = cursor.execute(\'select * from students\')#运行SOL语句
# lists = cursor.fetchall()#接收返回的结果
# # print(lists)
# for i in lists:
#     print(i)

 

在python中操作mysql数据库增删改数据

# import pymysql#调用模块
# db = pymysql.connect(user = \'root\',password=\'wbf980728\',database=\'wang\',charset=\'utf8\')#打开数据库连接
# cursor = db.cursor()#获取操作游标
# demo = cursor.execute(\'INSERT INTO students (s_id,c_id,s_name,s_gender,s_score) VALUES (9,4, "零零", "女", 77)\')
# demo = cursor.execute(\'DELETE  FROM students WHERE s_id=8\')
# demo = cursor.execute(\'UPDATE students SET s_name="kk", s_score=66 WHERE s_id=1;\')
# lists = cursor.fetchall()#接收返回的值
# db.commit()#提交mysql语句
# print(demo)

 

关闭数据库

#import pymysql#调用模块
#db = pymysql.connect(user = \'root\',password=\'wbf980728\',database=\'wang\',charset=\'utf8\')#打开数据库连接
#cursor = db.cursor()#获取操作游标
#data=db.close() #关闭数据库
#print(data)

 

分类:

技术点:

相关文章:

  • 2021-12-03
  • 2018-01-08
  • 2021-11-01
  • 2021-11-02
  • 2021-11-03
  • 2021-08-07
  • 2021-12-03
  • 2021-08-13
猜你喜欢
  • 2021-11-29
  • 2021-11-05
  • 2021-12-01
  • 2021-09-17
  • 2021-11-29
  • 2021-11-02
相关资源
相似解决方案