我正在尝试使用数组值列表执行MySQL的插入语句。但它不起作用..
我的“数组”列表包含所有必需的值
insert_statement = "insert into db (Hostname,Serial_Number,cpu,memory,Eth0_IP,Eth1_IP,hardware_type,datacenter,Operating_System) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s') % (array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8])"
cur.execute(insert_statement)
我也试过
insert_statement = "insert into db (Hostname,Serial_Number,cpu,memory,Eth0_IP,Eth1_IP,hardware_type,datacenter,Operating_System) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s') , array"
cur.executemany(insert_statement)
两者都出现“sql语句错误”错误。
答案 0 :(得分:1)
使用MySQLdb这应该有用(如果你的数组包含9个元素):
insert_statement = "insert into db (Hostname,Serial_Number,cpu,memory,Eth0_IP,Eth1_IP,hardware_type,datacenter,Operating_System) values (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
cur.execute(insert_statement, array)