[英]Data insertion not reflecting in mysql database
I was trying this post: How to speed up Python CSV Read to MySQL Write to speed up my data insertion script.我正在尝试这篇文章: 如何加快 Python CSV 读取到 MySQL 写入以加快我的数据插入脚本。 my table format: '''CREATE TABLE pattern ( pattern_id CHAR(32) PRIMARY KEY, block CHAR(4), pattern_source ENUM('LABEL','BURST'), pattern VARCHAR(150), pattern_type ENUM('PROD','EVAL','RMA','QMFAT','BURN_IN','DEBUG','UNKNOWN','REPAIR','VALIDATION') )''' I modified based on my database config.
我的表格格式:'''CREATE TABLE 模式(pattern_id CHAR(32) PRIMARY KEY, block CHAR(4), pattern_source ENUM('LABEL','BURST'), pattern VARCHAR(150), pattern_type ENUM('PROD', 'EVAL','RMA','QMFAT','BURN_IN','DEBUG','UNKNOWN','REPAIR','VALIDATION') )''' 我根据我的数据库配置进行了修改。 but after running the script, it runs without errors and data not updating in table.
但是在运行脚本后,它运行没有错误,并且表中的数据没有更新。 please refer to tick mark code snippet in that post above.
请参阅上面该帖子中的刻度线代码片段。 find my code here.
在这里找到我的代码。
import mysql.connector import time import concurrent.futures import csv import itertools CSVFILE='C:\\Users\\Downloads\\pattern.csv' CHUNK=10_000 def doBulkInsert(rows): with mysql.connector.connect(user='root', password='zxcasdQWE123', host='localhost', database='testdatabase') as connection: connection.cursor().executemany(f'INSERT INTO table_name (pattern_id, block, pattern_source, pattern, pattern_type) VALUES (%s, %s, %s, %s, %s)',rows) connection.commit() def main(): _s = time.perf_counter() with open(CSVFILE) as csvfile: csvdata = csv.reader(csvfile) _s = time.perf_counter() with concurrent.futures.ThreadPoolExecutor() as executor: while (data:= list(itertools.islice(csvdata, CHUNK))): executor.submit(doBulkInsert, data) executor.shutdown(wait=True) print(f'Duration = {time.perf_counter()-_s}') if __name__ == '__main__': main()
One thing you should always do with strings, is to use '' around their names.您应该始终对字符串做的一件事是在它们的名称周围使用 ''。 When there is a space in the name, the query will fail.
当名称中有空格时,查询将失败。 So instead of:
所以而不是:
VALUES (%s, %s, %s, %s, %s)值(%s、%s、%s、%s、%s)
You could try:你可以试试:
VALUES ('%s', '%s', %s, '%s', %s)值(“%s”、“%s”、%s、“%s”、%s)
Next issue, the pattern_id should be unique for each and every insert, else mysql will complain and only keep the first.下一个问题,每个插入的 pattern_id 应该是唯一的,否则 mysql 会抱怨并且只保留第一个。 You must be absolutely sure that pattern_id's are unique, If not, create a new column for the key.
您必须绝对确定 pattern_id 是唯一的,如果不是,请为键创建一个新列。 even when you never use that number.
即使您从不使用该号码。
Also, can you print the Sql statements before you run them.此外,您能否在运行 Sql 语句之前打印它们。 I'm not sure whether your Python code is correct.
我不确定您的 Python 代码是否正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.