import pymysql # 创建连接 conn = pymysql.connect(host='192.168.71.140', port=3306, user='root', passwd='521521', db='oldboy') # 创建游标 cursor = conn.cursor() #批量插入数据 data = [ ("N1","10", "2018-7-10","M"), ("N2","11", "2018-7-11","M"), ("N3","12","2018-7-12","F"), ] cursor.executemany("insert into student (name,age,register_date,gender) values(%s,%s,%s,%s)",data)#插入数据 # 执行SQL,并返回收影响行数 #effect_row = cursor.execute("select * from student") #print(cursor.fetchone())#打印出一条数据fetchone() #print(cursor.fetchmany(3))#打印三条数据 print(cursor.fetchall())#打印所有数据 # 执行SQL,并返回受影响行数 # effect_row = cursor.execute("update hosts set host = '1.1.1.2' where nid > %s", (1,)) # 执行SQL,并返回受影响行数 # effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)]) # 提交,不然无法保存新建或者修改的数据 conn.commit() # 关闭游标 cursor.close() # 关闭连接 conn.close()
必须还得给mysql得root账号授权才可以远程连接:
在刷新一下;