• python 用mysqldb方式操作数据库


    # -*- coding: utf-8 -*-
    from User import User
    import time, MySQLdb
    
    #连接
    conn = MySQLdb.connect(host='localhost', user='root', passwd='root', db='test', charset='utf8')
    cursor = conn.cursor()
    
    #写入
    # sql = "insert into Users values(null, %s, %s)"
    # for i in range(0, 10):
    #     param = ('liuruichao', 'nicai')
    #     result = cursor.execute(sql, param)
    #     print (result)
    # conn.commit()
    # cursor.close()
    # conn.close()
    
    #更新
    # sql = 'update Users set username=%s where id = %s'
    # param = ('buzhidao', 2)
    # result = cursor.execute(sql, param)
    # print (result)
    # conn.commit()
    # cursor.close()
    # conn.close()
    
    #删除
    # sql = 'delete from Users where id = %s'
    # param = (2)
    # result = cursor.execute(sql, param)
    # print (result)
    # conn.commit()
    # cursor.close()
    # conn.close()
    
    
    #查询
    list = []
    sql = 'select * from Users'
    cursor.execute(sql)
    rs = cursor.fetchall()
    for id, username, password in rs:
        print ('id : {}, username : {}, password : {}'.format(id, username, password))
        user = User()
        user.id = id
        user.username = username
        user.password = password
        list.append(user)
    cursor.close()
    conn.close()
    print ('总共有{}人'.format(len(list)))
    


  • 相关阅读:
    git简单使用命令
    localStorage的用法
    CSS3 进阶
    ASP.NET应用程序与页面生命周期
    IT专业人士如何更有效的学习专业知识
    jsonp跨域原理解析
    sql注入原理
    ajax跨域调用
    aspx、ashx以及cs的关系,viewState
    Js处理json数据
  • 原文地址:https://www.cnblogs.com/liuruichao/p/4005858.html
Copyright © 2020-2023  润新知