Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.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唯一ID_Mysql_Sql_Rss - Fatal编程技术网

在数据库中插入列表元素时创建MySQL唯一ID

在数据库中插入列表元素时创建MySQL唯一ID,mysql,sql,rss,Mysql,Sql,Rss,我实际上是从主页下载提要,并尝试将它们写入MySQL数据库。这些提要以RSS的形式发布。没有创建唯一ID,一切正常。因此Insert命令一定是错误的 这是我的密码: import feedparser import urllib2 import cookielib import MySQLdb import time import datetime from cookielib import CookieJar from urllib2 import urlopen db = MySQLdb.

我实际上是从主页下载提要,并尝试将它们写入MySQL数据库。这些提要以RSS的形式发布。没有创建唯一ID,一切正常。因此Insert命令一定是错误的

这是我的密码:

import feedparser
import urllib2
import cookielib
import MySQLdb
import time
import datetime
from cookielib import CookieJar
from urllib2 import urlopen

db = MySQLdb.connect(host="localhost", # your host, usually localhost
                 user="root", # your username - SELECT * FROM mysql.user
                 passwd="****", # your password
                 db="sentiment_analysis") # name of the data base

cur = db.cursor()
cur.execute("DROP TABLE IF EXISTS feeddata_lse")

sql = """CREATE TABLE feeddata_lse (LSE_ID INT NOT NULL AUTO_INCREMENT,PRIMARY KEY(LSE_ID),Unix_Timesstamp integer, III_Timestamp varchar(255), Source varchar(255), Title varchar(255), Text TEXT,  Link varchar(255), Epic varchar(255), CommentNr integer, Author varchar(255))"""
cur.execute(sql)

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(CookieJar()))
opener.addheaders = [{'User-agent','Mozilla/5.0'}]

def feed_load(feed):
return [(time.time(),
         entry.published,
         'lse',
         entry.title,
         entry.summary,
         entry.link,
         (entry.link.split('?ShareTicker=')[1]).split('&post=')[0],
         entry.link.split('&post=')[1],
         entry.author)
        for entry
        in feedparser.parse(feed).entries]

def main():
FEED_URL = "http://www.lse.co.uk/chat/recent/"
feed = feed_load(FEED_URL)

print feed[1][1]

for item in feed:
    cur.execute("""INSERT INTO feeddata_lse VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)""",item)
db.commit()
如果我删除Insert语句中的
LSE\u ID INT NOT NULL AUTO\u INCREMENT,主键(LSE\u ID)
out,一切正常。我更改插入部分的主要问题是RSS提要包含一个提要列表,而每个提要都是另一个列表

谢谢您的帮助。请用代码示例解释您的论点,因为我没有经验。如果您有任何问题,请随时提问

以下是错误报告:

Traceback (most recent call last):
File "C:/Python27/MySQL_finalversion/rss_db_connection_mysql_v1.py", line 54, in <module>
main()
File "C:/Python27/MySQL_finalversion/rss_db_connection_mysql_v1.py", line 48, in main
cur.execute("""INSERT INTO feeddata_lse VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)""",item)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (1136, "Column count doesn't match value count at row 1")
回溯(最近一次呼叫最后一次):
文件“C:/Python27/MySQL\u finalversion/rss\u db\u connection\u MySQL\u v1.py”,第54行,在
main()
文件“C:/Python27/MySQL\u finalversion/rss\u db\u connection\u MySQL\u v1.py”,第48行,在main中
当前执行(““插入到feeddata中的值(%s,%s,%s,%s,%s,%s)”,项)
文件“C:\Python27\lib\site packages\MySQLdb\cursors.py”,第205行,在execute中
errorhandler(self、exc、value)
defaulterrorhandler中第36行的文件“C:\Python27\lib\site packages\MySQLdb\connections.py”
提高errorclass,errorvalue
操作错误:(1136,“列计数与第1行的值计数不匹配”)

表创建语句中出现
LSE\u ID INT NOT NULL AUTO\u INCREMENT,PRIMARY KEY(LSE\u ID)
行,而不是insert语句。如果从表创建语句中删除一列,但保持INSERT语句不变,则确实会出现“column count Not match value(列计数与值不匹配)”错误,因为您尝试插入的列比表中定义的多一列

我想你会发现你真的想保留主键。与其从表创建语句中删除
LSE_ID INT NOT NULL AUTO_INCREMENT,PRIMARY KEY(LSE_ID)
,不如尝试像这样显式命名INSERT语句中的列

将值(%s,%s,
插入feeddata(Unix时间戳、III时间戳、源、标题、文本、链接、Epic、注释、作者)中