python2.7 mysql execute()问题
怪我咯
怪我咯 2017-05-18 10:52:05
0
1
770

插入数据的部分代码:

            try:
                sql = "INSERT INTO {}({}) VALUES(%s)".format(table,cols) % q[0]
                print sql           
                result = self.cur.executemany(sql,q)
                #result = self.cur.execute(sql)
                insert_id = self.db.insert_id()
                self.db.commit()
                #判断是否执行成功
                if result:
                    return insert_id
                else:
                    return 0
            except MySQLdb.Error,e:
                #发生错误时回滚
                print "数据库错误,原因%d: %s" % (e.args[0], e.args[1])               
                self.db.rollback()

在执行完程序后得到

[(u'""," http://sz.centanet.com/ershoufang/szlg13772053.html"," 345\u4e07|3\u5ba42\u5385|74.89\u5e73"," \u5357|2007\u5e74|\u4f4e\u5c42(\u517134\u5c42)|\u8c6a\u88c5|\u5eb7\u8fbe\u5c14\u82b1\u56ed\u4e94\u671f(\u8774\u8776\u5821)|"',)]
INSERT INTO zydc(follow, house_url, price_area, houseinfo) VALUES(%s)
数据库错误,原因1136: Column count doesn't match value count at row 1
None

那条insert语句我在数据库中手动输入时并没有问题,但是不知道为什么execute出错。
当我向一个只存链接的表插入数据时,是完全没问题的。
刚刚我想输出错误原因时,发现数据竟然迷之插入成功了,然后我试图用executemany()后就又出现错误了。

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
PHPzhong

最后我放弃使用executemany()了。

INSERT INTO employees (first_name, hire_date)
VALUES ('Jane', '2005-02-12'), ('Joe', '2006-05-23'), ('John', '2010-10-03')

直接用上面这种形式,execute()就好。
https://dev.mysql.com/doc/con...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!