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()]

     

    
    
    
    
    
  • 相关阅读:
    HDU 5585 Numbers
    HDU 3308 LCIS
    POJ 2991 Crane
    POJ 1436 Horizontally Visible Segments
    POJ 3667 Hotel
    HaiHongOJ 1003 God Wang
    【SDOI 2008】 递归数列
    5月19日省中提高组题解
    【HDU 1588】 Gauss Fibonacci
    【POJ 3233】Matrix Power Series
  • 原文地址:https://www.cnblogs.com/wantao/p/8832377.html
Copyright © 2011-2022 走看看