【发布时间】:2016-11-16 11:55:33
【问题描述】:
在 mysql 数据库中插入大量数据。 遵循的步骤是: https://affiliate.itunes.apple.com/resources/documentation/epfimporter/
问题是除了我看不到数据之外,一切似乎都很顺利。我也在桌子上尝试了select count(*),它返回 0 行。
我在命令行上也试过。
数据库的大小正在增加。
我正在使用 Sequel Pro,当我按下刷新时,它会显示行数。随着我的脚本不断插入更多数据,行数和大小也在增加。但是加载器完成后,行数显示为0,但大小仍然是一个很大的数字。
摄取器代码 sn-p。完整的代码可以在上面给出的链接中找到。
def _populateTable(self, tableName, resumeNum=0, isIncremental=False, skipKeyViolators=False):
"""
Populate tableName with data fetched by the parser, first advancing to resumePos.
For Full imports, if skipKeyViolators is True, any insertions which would violate the primary key constraint
will be skipped and won't log errors.
"""
#REPLACE is a MySQL extension which inserts if the key is new, or deletes and inserts if the key is a duplicate
commandString = ("REPLACE" if isIncremental else "INSERT")
ignoreString = ("IGNORE" if (skipKeyViolators and not isIncremental) else "")
exStrTemplate = """%s %s INTO %s %s VALUES %s"""
colNamesStr = "(%s)" % (", ".join(self.parser.columnNames))
self.parser.seekToRecord(resumeNum) #advance to resumeNum
conn = self.connect()
while (True):
#By default, we concatenate 200 inserts into a single INSERT statement.
#a large batch size per insert improves performance, until you start hitting max_packet_size issues.
#If you increase MySQL server's max_packet_size, you may get increased performance by increasing maxNum
records = self.parser.nextRecords(maxNum=300)
if (not records):
break
escapedRecords = self._escapeRecords(records) #This will sanitize the records
stringList = ["(%s)" % (", ".join(aRecord)) for aRecord in escapedRecords]
cur = conn.cursor()
colVals = unicode(", ".join(stringList), 'utf-8')
exStr = exStrTemplate % (commandString, ignoreString, tableName, colNamesStr, colVals)
#unquote NULLs
exStr = exStr.replace("'NULL'", "NULL")
exStr = exStr.replace("'null'", "NULL")
try:
cur.execute(exStr)
except MySQLdb.Warning, e:
LOGGER.warning(str(e))
except MySQLdb.IntegrityError, e:
#This is likely a primary key constraint violation; should only be hit if skipKeyViolators is False
LOGGER.error("Error %d: %s", e.args[0], e.args[1])
self.lastRecordIngested = self.parser.latestRecordNum
recCheck = self._checkProgress()
if recCheck:
LOGGER.info("...at record %i...", recCheck)
conn.close()
我什至等待脚本完成,但没有结果。
MySQl 版本 5.6.23
非常感谢任何帮助。
【问题讨论】:
-
显示实际的插入命令。
-
@johnelemans 我已经给出了一个链接。它是 iTunes 提供的代码,您可以找到正在插入的文件 EPFIgester.py
-
重现您面临的问题所需的所有信息都应直接显示在 Stackoverflow 上。
-
@Jocelyn 添加了代码 sn-p。
标签: mysql innodb itunes mysql-python