python mysql 插入数据报错 Failed processing format-parameters; 'int' object is not iterable

搞了半天才找到问题。一直报错 Failed processing format-parameters; 'int' object is not iterable

data = (0, 'this is title', 'this is abstract', 'image-url', 'views', 'time', 'http:\\www.baidu.com', '这里是评论')
sql = "insert into table_name(id ,title,abstract,image,views,time,href,comments) values(%s,%s,%s,%s,%s,%s,%s,%s)"
try:
    cursor.executemany(sql, data)
except Exception as e:
    print(str(e) + 'insert exception')
conn.commit()
原因是用这种方式插入数据必须是数组形式才可以,改为如下:

data = [(0, 'this is title', 'this is abstract', 'image-url', 'views', 'time', 'http:\\www.baidu.com', '这里是评论')]

就可以了。


更新数据报错:1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s where href = %s' at line 1 update  exception

此问题也是val需要是数组,改为数组就正常了如下:

cursor = conn.cursor()
sql = "update ecs_news_qczj set article=%s where href = %s"
val = ('123467',hrefu)
try:
    cursor.executemany(sql,val)
except Exception as e:
    print(str(e) + ' update  exception')
conn.commit()

改为:

val = [('123467',hrefu)]

希望能帮助大家少走弯路。


你可能感兴趣的:(python mysql 插入数据报错 Failed processing format-parameters; 'int' object is not iterable)