本文共 790 字,大约阅读时间需要 2 分钟。
#Author Kangimport pymysql#连接方法conn = pymysql.connect(host='10.3.152.35',port=3306,user='kang',passwd='123456',db='test')#创建当前游标cursor = conn.cursor()#effect_row 为结果行数effect_row = cursor.execute("select * from student")#打印当前游标语句查询的结果print(cursor.fetchall())#建立需要插入的数据data = [ ('MM',33), ('BearBear',4), ('KK',10)]#执行插入多条语句cursor.executemany('insert into student(name,age) values(%s,%s)',data)#执行插入单条带变量的语句#data = cursor.execute("insert into user(username,age,department) values('{0}','{1}','{2}')".format(username,age,department))#提交语句conn.commit()#执行更新语句cursor.execute('update student set name="MK" where id = 1')conn.commit()#执行删除语句cursor.execute('delete from student where name = "KK"')conn.commit()cursor.close()conn.close()
转载于:https://blog.51cto.com/12965094/2361750