import MySQLdb try: conn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘123456‘,db=‘test‘,port=3306,charset=‘gb2312‘) cursor = conn.cursor() sql = "select * FROM user" cursor.execute(sql) print("Row results:",cursor.rowcount) rs1 = cursor.fetchone() print(rs1) rs2 = cursor.fetchmany(3) print(rs2) rs3 = cursor.fetchall() print(rs3) # for row in cursor.fetone(): # id = row[0] # name = row[1] # join_time = row[2] # print "id=%d,name=%s,join_time=%s" % (id,name,join_time ) conn.close() except MySQLdb.Error,e: print "Could not connect to MySQL server."
时间: 2024-10-25 14:31:40