简体   繁体   English

数据插入未反映在 mysql 数据库中

[英]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()
can you tell or an updated code why it is not updating. 你能告诉或更新代码为什么它没有更新。

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.

相关问题 在mysql表中插入数据 - Insertion of data in mysql table 使用python在postgresql数据库中插入json数据问题 - json data insertion problem in postgresql database with python 使用python插入后未显示数据库中的数据 - Not showing data in database after insertion using python 当我在 Django 中按下 forms 的下一步或保存按钮时,如何避免在 Mysql 数据库中插入新的相同数据行 - How to avoid insertion of new row of same data in Mysql database when i press next or save button of forms in Django 在 Flask SQLAlchemy 中反映数据库 - Reflecting a database in Flask SQLAlchemy 使用 innondb 引擎在 mysql 数据库中从 python 脚本插入问题 - Problem in insertion from python script in mysql database with innondb engine 加载CSV值,然后在mysql数据库中搜索匹配项,然后应用插入? - Load CSV values then search for matches in mysql database and then apply insertion? 加速从pandas数据帧到mysql的数据插入 - Speeding up data insertion from pandas dataframe to mysql 提交表单时,django 中的数据库中没有数据插入 - No data insertion in the database in django when form is been submited 在 Azure SQL 数据库中插入新数据后接收确认 - Recieving Ackowledgment after new data insertion in Azure SQL Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM