# -*- 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)))