首页 > 代码库 > python mysql curros.executemany 批量添加

python mysql curros.executemany 批量添加

#添加的表结构字段分辨是(id,title,summary,visits,accountName,grabTime)

#其中id,是int自增主键,在添加操作的时候,不需要对id进行操作

conn = mysql.connector.connect(user=‘root‘,password=‘‘,database=‘newsdb‘,host=‘127.0.0.1‘, port=‘3306‘)
cursor = conn.cursor()

#此处Sql语句必须指定添加的字段名称!!!!!
sql = "INSERT INTO news(title,summary,visits,accountName,grabTime) VALUES(%s,%s,%s,%s,%s)"

try:
     cursor.executemany(sql,newsObj_tupeList)
except Exception as e:
  conn.rollback()
      print("执行MySQL: %s 时出错:%s" % (sql, e))     

conn.commit()
cursor.close()
conn.close()

 

#批量添加数据,!!!!!数据格式必须list[tuple(),tuple(),tuple()]  或者tuple(tuple(),tuple(),tuple())

list = [(‘666‘,‘666‘,‘666‘,‘666‘,‘666‘),(‘777‘,‘222‘,‘333‘,‘444‘,‘555‘)]
weixinDb = Operation_WeiXinDb()
weixinDb.Insert_News(list)
       

python mysql curros.executemany 批量添加