1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #!/usr/bin/env python # -*- coding: utf-8 -*- import MySQLdb #建立连接 conn = MySQLdb.connect(host = '127.0.0.1' ,user = 'root' ,passwd = '1qaz#EDC' ,db = 'test_db' ) cur = conn.cursor(MySQLdb.cursors.DictCursor) #建立游标时,加上"MySQLdb.cursors.DictCursor",让数据查询结果返回字典类型 #对数据进行操作 sql = "select * from user" #定义查询操作的sql语句 cur.execute(sql) #执行sql语句 data = cur.fetchall() #读取数据 #关闭数据库连接 cur.close() conn.close() print data |