import pymysql # 数据库链接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='111111', db='test', charset='utf8', cursorclass = pymysql.cursors.DictCursor # 返回数据为map类型 ) # 获取操作游标 cur = conn.cursor() # 执行一条sql语句 #cur.execute("INSERT INTO user(name) value(%s)", ['张三']) # 插入多条的 cur.executemany("INSERT INTO user(name) value(%s)", [('张三'),('李四')]) cur.execute('SELECT * FROM user') # 取出查询的第一条数据 print(cur.fetchone()) # 获取查询的全部数据 for row in cur.fetchall(): uid = row['uid'] name = row['name'] print('uid=' + str(uid) + ',name=' + name) # 提交事务 需要时innodb conn.commit() # 回滚事务 #conn.rollback() # 关闭 cur.close() conn.close()