python对 MySQL 数据库进行增删改查的脚本
作者:lucky8492 时间:2024-01-18 07:04:24
# -*- 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()
来源:https://www.cnblogs.com/carey9420/p/12180640.html
标签:python,MySQL,数据库,增删查改
0
投稿
猜你喜欢
Python产生一个数值范围内的不重复的随机数的实现方法
2023-09-19 21:48:09
javascript中可能用得到的全部的排序算法
2024-04-29 13:45:34
详解pandas数据合并与重塑(pd.concat篇)
2023-06-02 00:05:37
解决django接口无法通过ip进行访问的问题
2023-09-15 14:07:27
Python魔法方法 容器部方法详解
2021-02-05 19:25:43
python反转(逆序)字符串的6种方法详细
2023-03-14 10:38:41
javaScript通用数据类型校验函数
2009-07-06 12:49:00
Django 自定义分页器的实现代码
2023-06-20 15:21:03
Go http client 连接池不复用的问题
2024-02-03 05:17:15
基于Django OneToOneField和ForeignKey的区别详解
2021-10-17 11:01:21
sqlserver 三种分页方式性能比较[图文]
2011-09-30 11:16:20
通俗讲解python 装饰器
2022-04-30 08:20:47
用python做个代码版的小仙女蹦迪视频
2022-10-14 12:48:59
vue实现图片懒加载的方法分析
2024-04-27 16:10:29
Micropython固件使用Pico刷固件并配置VsCode开发环境的方法
2022-09-21 16:22:14
基于hashlib模块--加密(详解)
2023-10-08 15:46:29
python之pygame模块实现飞机大战完整代码
2023-08-31 12:10:01
Windows中安装使用Virtualenv来创建独立Python环境
2023-10-21 19:44:52
pycharm设置虚拟环境与更换镜像教程
2023-06-10 06:58:05
Python多线程爬虫实战_爬取糗事百科段子的实例
2021-02-15 16:58:21