我试图从数据库中提取记录,对其中一个字段进行一些处理,然后将数据插入到数据库中。但是,我注意到,脚本在一次成功插入后停止,即使在数据库中插入的记录之外还有更多的记录。你知道吗
cur = db.cursor()
# Pull records from the database, process each one, and store back into the database
SQLQuery = """SELECT * FROM cybersecurity4.hackhoundposts"""
cur.execute(SQLQuery)
for row in cur:
postID = row[0]
threadID = row[1]
authorID = row[2]
url = row[3]
subforum = row[4]
threadTitle = row[5]
authorName = row[6]
postDateTime = row[7]
postSequence = row[8]
flatcontent = row[9]
userTitle = row[11]
hasAttachment = row[12]
print (postID)
# #process the flatcontent
flatcontent = rmvSpecialCharsandCamelCase(flatcontent)
# insert into the database
try:
string = """INSERT INTO cybersecurity4.cleanedhackhoundposts
(postid, threadid, authorid, URL, subforum, threadTitle, authorName, postdatetime, postSequence, flatcontent, usertitle, hasattachment)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s');""" % \
(postID ,threadID,authorID,url,subforum,threadTitle,authorName,postDateTime,postSequence,flatcontent,userTitle,hasAttachment)
print (string)
cur.execute(string)
print ("Successfully inserted post " + str(postID))
except:
int("Failed to insert post" + str(postID))
我理解您的代码,您没有重复插入的内容,因此您正在检索N条记录,但只插入1条记录。你需要实现的一些sudo代码是。你知道吗
您已接近结果,但错过了最后一步。你知道吗
编辑: 不能在选择的同一个id上插入id,必须让db处理该id或只是更新行。因此,从insert查询中删除id可能会解决这个问题。下次还要检查数据库日志,它告诉很多。你知道吗
相关问题 更多 >
编程相关推荐