欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python对 MySQL 数据库进行增删改查的脚本

程序员文章站 2022-06-15 13:23:08
# -*- coding: utf-8 -*-import pymysqlimport xlrd# import codecs#连接数据库conn = pymysql.connect(host='12...
# -*- coding: utf-8 -*-
import pymysql
import xlrd
# import codecs
#连接数据库
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='test_hvr', charset='utf8')
cursor = conn.cursor()

# 查询数据库
effect_row = cursor.execute("select * from kkpb_account where user_name='18800000000'")
row_1 = cursor.fetchmany(10)
print(row_1)
for row in row_1:
  with open('ha.xls','a')as f:
    f.write(str(row[0]))
#   print(row[0])

# 使用预处理语句创建表
# sql = """create table employee (
#     first_name char(20) not null,
#     last_name char(20),
#     age int,
#     sex char(1),
#     income float )"""
# cursor.execute(sql)

# 数据库插入数据
# s_id='25'
# name='aaa'
# price='154.21'
# sql="insert into stu (id) values ('%s')"%(s_id,)
# add_row=cursor.execute(sql)

# 删除数据
# name='asds'
# sql="delete from user where name = '%s'" % name
# dele_row=cursor.execute(sql)

# 更新数据
# userid='10'
# sql="update user set price=124.21 where userid='%s'"%userid
# upd_row=cursor.execute(sql)

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

以上就是python对 mysql 数据库进行增删改查的脚本的详细内容,更多关于python 操作mysql数据库的资料请关注其它相关文章!