栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

# -*- 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数据库的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/16743.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号