zoukankan      html  css  js  c++  java
  • python mysql基本操作

    #链接mysql
    #插入数据
    import MySQLdb

    conn = MySQLdb.connect(host = '127.0.0.1',user = 'root',passwd = '',db = 'mytest')
    cur = conn.cursor()
    reConn = cur.execute("insert into userinfo(id,name) value(3,'tony')")
    conn.commit()
    cur.close()
    conn.close()

    print reConn

    #查询数据
    import MySQLdb

    conn = MySQLdb.connect(host = '127.0.0.1',user = 'root',passwd = '',db = 'mytest')
    cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) #以字典形式获取数据
    cur = conn.cursor() #以元组形式获取数据
    #reConn = cur.execute("select * from userinfo")
    sql = "select name from userinfo where id = %s"
    #sql = "select id from userinfo where id = %s"
    params = 1
    #params = 'dendi'
    #reConn = cur.execute(sql,params)
    reConn = cur.execute("select * from userinfo")
    #fetchall把查找到的数据一下拿出来
    #fetchone逐个取数据
    # data = cur.fetchall()
    data = cur.fetchone()
    print data
    data = cur.fetchone()
    print data
    cur.close()
    conn.close()
    # print data
    print reConn

    #更新数据
    import MySQLdb

    conn = MySQLdb.connect(host = '127.0.0.1',user = 'root',passwd = '',db = 'mytest')
    cur = conn.cursor()
    sql = "update userinfo set name = 'aaa' where id = '%s'"
    params = 1
    reConn = cur.execute(sql,params)
    conn.commit()
    #conn.rollback()
    cur.close()
    conn.close()

    print reConn

    #删除数据
    import MySQLdb

    conn = MySQLdb.connect(host = '127.0.0.1',user = 'root',passwd = '',db = 'mytest')
    cur = conn.cursor()
    sql = "delete from userinfo where id = '%s'"
    params = 1
    reConn = cur.execute(sql,params)
    conn.commit()
    #conn.rollback()
    cur.close()
    conn.close()

    print reConn



    参考博客:http://www.cnblogs.com/wupeiqi/articles/4198124.html
  • 相关阅读:
    goj 来自不给标题的菜鸟出题组(巴什博弈+素数判定)
    goj 城市交通线(简单并查集)
    ACM_鸡兔同笼(二元一次方程)
    ACM_魔仙岛探险(深搜)
    ACM_螺旋填数
    ACM_开心消消乐
    构建工具是如何用 node 操作 html/js/css/md 文件的
    学习使用ExpressJS 4.0中的新Router
    请求时token过期自动刷新token
    Express的基本使用
  • 原文地址:https://www.cnblogs.com/3one/p/5601818.html
Copyright © 2011-2022 走看看