欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

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

时间:2021-05-12 09:12:05|栏目:Python代码|点击:
# -*- 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()

上一篇:Django中的“惰性翻译”方法的相关使用

栏    目:Python代码

下一篇:13个最常用的Python深度学习库介绍

本文标题:python对 MySQL 数据库进行增删改查的脚本

本文地址:http://www.codeinn.net/misctech/120062.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有