加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 10:12:36 所属栏目:Python 来源:网络整理
导读:? # -*- coding: utf-8 -*- import pymysql xlrd import codecs # 连接数据库 conn = pymysql.connect(host= ' 127.0.0.1 ' ,port=3306,user= root '' ,db= test_hvr utf8 ' )cursor = conn.cursor() 查询数据库 effect_row = cursor.execute( " select * fr

?

# -*- coding: utf-8 -*-
import pymysql
 xlrd
 import codecs
#连接数据库
conn = pymysql.connect(host='127.0.0.1',port=3306,user=root'',db=test_hvrutf8')
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.xlsa)as f:
        f.write(str(row[0]))
     print(row[0])

 使用预处理语句创建表 sql = """CREATE TABLE EMPLOYEE (          FIRST_NAME  CHAR(20) NOT NULL,          LAST_NAME  CHAR(20),1)">          AGE INT,1)">          SEX CHAR(1),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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读