栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

python 连接数据库 增删查改

python 连接数据库 增删查改

1.插入数据
2.更新数据
进阶:遍历表,插入,并更新数据

import time
import pymysql

conn= pymysql.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='154524',
        db ='test',
        )
cur = conn.cursor()
#插入一条数据
cur.execute("insert into student values('5','8om','3 year 2 class','9')")
#修改查询条件的数据
cur.execute("update student set class='3 year 1 class' where name = 'Tom'")
cur.execute('select ssh from {}'.format("pro_test"))
#遍历表的数据,读取并更新
list = []          ## 空列表
for promsg in cur:
    ssh_url=promsg[0]
    print(ssh_url)
    upadtemsg="update pro_test set url='"+http_url+"' where ssh ='"+ssh_url+"'"
    list.append(upadtemsg)  

for item in list :
    cur.execute(item)

cur.close()
conn.commit()
conn.close()

参考文章:python连接数据库
通过mysqlclient操作MySQL数据库

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

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

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