zoukankan      html  css  js  c++  java
  • 使用pymysql对mysql进行增删改差

    import pymysql
    db=pymysql.connect(host='localhost',user='root',passwd='xxxxxx',db='pymysql',port=3306)
    def select_one():
    sql='select * from tb_user'
    cursor=db.cursor()
    cursor.execute(sql)
    data=cursor.fetchone()
    return data

    def select_by_id(id):
    sql='select * from tb_user where id=%d'%id
    cursor=db.cursor()
    cursor.execute(sql)
    data=cursor.fetchone()
    return data
    def select_all():
    sql = 'select * from tb_user'
    cursor = db.cursor()
    cursor.execute(sql)
    datas= cursor.fetchall()
    return datas

    def delete_one(id):
    sql='delete from tb_user where id=%d'%id
    cursor=db.cursor()
    cursor.execute(sql)

    def insert_one():
    sql='insert into tb_user(id,name,age) values(2,"wanchao",18)'
    cursor=db.cursor()
    cursor.execute(sql)

    def update():
    sql='update tb_user set name="gaohan" where name="wanchao"'
    cursor=db.cursor()
    cursor.execute(sql)

    if __name__=='__main__':
    print('查询一条语句:{}'.format(select_one()))
    print('按id查询一条语句:{}'.format(select_by_id(2)))
    print('查询多条语句:')
    [ print(data) for data in select_all()]
    print('删除:')
    delete_one(2)
    [print(data) for data in select_all()]
    print('添加')
    insert_one()
    [print(data) for data in select_all()]
    print('修改:')
    update()
    [print(data) for data in select_all()]

     

    
    
    
    
    
  • 相关阅读:
    三、Nginx配置文件说明
    Nginx配置文件说明
    二、Nginx离线安装
    一、Nginx简述
    yum 下载全量依赖 rpm 包及离线安装(终极解决方案)
    Java注解
    windows常用运行命令和dos命令
    MySQL学习记录一之存储引擎篇
    查询某条数据的上一条和下一条数据的Sql语句
    Single Number II
  • 原文地址:https://www.cnblogs.com/wantao/p/8832377.html
Copyright © 2011-2022 走看看