ProgrammingError: (1064, "You have an error in your SQL syntax

新手上路,请多包涵

1.使用mysqldb库1.2.5版本。在scrapy的pipeline文件中进行插入数据库操作时,报错。
clipboard.png

2.这是报错的完整信息

clipboard.png

这是对应的pipeline文件
import MySQLdb
import os.path

class ProjectPipeline(object):

def process_item(self, item, spider):
    name = item['name'].encode('utf8')
    location = item['location'].encode('utf8')
    describe = item['describe'].encode('utf8')    
    picture = item['picture'].encode('utf8')
    comment = item['comment'].encode('utf8')

    conn = MySQLdb.connect(
        host = 'localhost',
        port = 3306,
        user = 'root',
        passwd = '3285109',
        db = 'django',
        charset = 'utf8')
    cur = conn.cursor()
    cur.execute("INSERT INTO django_tmp(name,location,describe,picture,comment) values(%s,%s,%s,%s,%s)", (name,location,describe,picture,comment))
    cur.close()
    conn.commit()

    return item

再送上我的数据库的表
clipboard.png

3.最可气的就是我在其他的scrapy项目中成功的对数据库成功地进行了插入!!!
这是代码

import MySQLdb
import os.path

class WeatherPipeline(object):

def process_item(self, item, spider):
    cityDate = item['cityDate'].encode('utf8')
    week = item['week'].encode('utf8')
    img = os.path.basename(item['img'])
    temperature = item['temperature'].encode('utf8')
    weather = item['weather'].encode('utf8')
    wind = item['wind'].encode('utf8')
    conn = MySQLdb.connect(
        host = 'localhost',
        port = 3306,
        user = 'root',
        passwd = '3285109',
        db = 'scrapyDB',
        charset = 'utf8')
    cur = conn.cursor()
    cur.execute("INSERT INTO weather(cityDate,week,img,temperature,weather,wind) values(%s,%s,%s,%s,%s,%s)", (cityDate,week,img,temperature,weather,wind))
    cur.close()
    conn.commit()
    conn.close()

    return item

这是表的设计

clipboard.png

再来就是运行成功的样子

clipboard.png

clipboard.png

再到数据库中查看一下!果然是有数据的

clipboard.png

4.那么问题来了!!!为什么我检查了很多遍也没有发现到底两者之间有什么不同,导致我的第一个文件语法错误!!!我是新手,在做毕业设计,真心求大神不要嫌弃,给我指点一下!谢谢!

阅读 2.8k
1 个回答
新手上路,请多包涵

。。。。。时隔这么长的时间再上来看这个问题!!我搞懂了!!!
python所谓的格式字符串!!!
a = 'ztm'
print 'ab%scd'%a
则会输出abztmcd
a = 'ztm'
b = 123
print 'ab%scd%de'%(a,b)
则会输出abztmcd123de

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题