zoukankan      html  css  js  c++  java
  • mysqlclient---MySQLdb的增删改查

    简单了解

    import MySQLdb
     
    #建立数据库连接
    connect = MySQLdb.connect(host=host, user=dbuser, passwd=dbpass, db=dbname, charset=charset)
    #cursor = connect .cursor() #这种方式查询出来的结果是tuple
    cursor = connect.cursor(cursorclass=MySQLdb . cursors . DictCursor) #这种方式的查询结果是dict
     
    #INSERT --单条数据
    sql = “INSERT INTO user (name, age, gneder) VALUES (%s, %s, %s)”
    param = ('bright', '27', '1')
    cursor.execute(sql, param)
    cursor.close
    connect.commit() #注意:如果不调用此方法插入语句不会真正执行,ADD/DELETE/UPDATE操作必须执行此函数。
    #INSERT --多条数据
    sql = “INSERT INTO user (name, age, gneder) VALUES (%s, %s, %s)”
    param = (('ww', '26', '1'), ('wqbin', '25', '1'))
    cursor.executemany(sql, param)
    cursor.close
    connect.commit() 
     
    #DELETE
    sql = "DELETE FROM user WHERE id=1"
    cursor.execute(sql)
    cursor.close()
    connect.commit()
     
    #UPDATE
    sql = "UPDATE user SET name=%s,age=%s WHERE id=1"
    param = ('wqbin', 25)
    cursor.execute(sql, param)
    cursor.close()
    connect.commit()
     
    #SELETE
    result = []
    sql = "SELECT * FROM user WHERE id>1 LIMIT 30"
    cursor.execute(sql)
    for row in self.__cursor.fetchall():
        result.append(row)
    cursor.close()
    connect.close() #关闭数据库连接
     
    print(result)

    OK

  • 相关阅读:
    PostGIS解压版安装
    gulp监听文件变化,并拷贝到指定目录
    pre在火狐中不换行
    Undefined symbols for architecture i386: "_crc32", referenced from:
    响应式自动化开发流程-Windows 版
    Gulp入门教程
    SVG折线图
    git-版本控制
    log4j
    CSS:描述样式
  • 原文地址:https://www.cnblogs.com/wqbin/p/12675699.html
Copyright © 2011-2022 走看看