Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql在python中显示类型错误_Mysql_Python 2.7 - Fatal编程技术网

Mysql在python中显示类型错误

Mysql在python中显示类型错误,mysql,python-2.7,Mysql,Python 2.7,我编写了以下代码来写入mysql数据库。它显示%d格式:需要数字,而不是str 而当我打印数字时,它显示为6。为什么会这样 for i in range(len(text1)): try1="INSERT INTO `crawl_url_attribute_css_sel_value`(`crawl_item_id`,`crawl_url_attribute_css_sel_id`,`value`,`reconciled`)\ VALUE

我编写了以下代码来写入mysql数据库。它显示%d格式:需要数字,而不是str

而当我打印数字时,它显示为6。为什么会这样

for i in range(len(text1)):
            try1="INSERT INTO `crawl_url_attribute_css_sel_value`(`crawl_item_id`,`crawl_url_attribute_css_sel_id`,`value`,`reconciled`)\
                 VALUES(%s,%d,%s,FALSE) "

            values=(prodlist[i].get_attribute(result1[4]), int(number), db.escape_string(text1[i].text))
            print number
            cursor.execute(try1,values)
            db.commit()

您必须使用%s占位符

for i in range(len(text1)):
        try1="INSERT INTO `crawl_url_attribute_css_sel_value` 
        (`crawl_item_id`,  `crawl_url_attribute_css_sel_id`, `value`, `reconciled`)\
        VALUES(%s,%s,%s,FALSE)"

        values=(prodlist[i].get_attribute(result1[4]), int(number), db.escape_string(text1[i].text))
        print number
        cursor.execute(try1,values)
        db.commit()
你可以阅读解释

要执行查询,首先需要一个游标,然后才能执行 查询:

c=db.cursor max_price=5 c.executes从中选择垃圾邮件、鸡蛋、香肠 早餐 其中价格<%s,最大价格

在本例中,max_price=5为什么在字符串中使用%s?因为 MySQLdb将其转换为SQL文本值,即字符串 '5'. 当它完成时,查询实际上会说,…WHERE price <5