简体   繁体   English

如何在插入之前使用CSV(平面文件)更新MySQL数据库并验证数据

[英]How to Update MySQL Database with CSV(flat-file) and validate data before insertion

I have about 40K records that I need to update into my php/mysql application on a daily basis. 我每天大约有40K条记录需要更新到我的php / mysql应用程序中。 What is the recommended best approach? 推荐的最佳方法是什么?

I did some research on load data in file to load the flat-file in a temp MySQL table and run validation on the whole column instead of doing one by one row at a time. 我对文件中的数据加载进行了一些研究,以将平面文件加载到临时MySQL表中,并在整个列上运行验证,而不是一次一行地进行验证。 Later use the primary key and insert/update that in the database. 以后使用主键并将其插入/更新到数据库中。

However for the data which has MultiSelect options or multiple valid values? 但是对于具有MultiSelect选项或多个有效值的数据? How do i validate those before updating them in the database. 我如何在更新数据库之前验证它们。

Options are delimited using piped. 选项使用管道分隔。

So for example a multi select data is Color type Valid Values are RED GREEN BLUE BROWN BLACK WHITE 因此,例如,多选数据为颜色类型,有效值为红色,绿色,蓝色,棕色,黑色,白色

Raw value example case scenarios are 原始值示例案例方案是

Case 1 Raw Data that needs to be validated RED|GREEN|YELLOW 情况1需要验证的原始数据RED | GREEN | YELLOW

How can is validate this data in the temp table so RED & GREEN pass thru validation and YELLOW gets stripped out and goes in error log? 如何才能在临时表中验证此数据,以使红色和绿色通过验证,并且黄色被剥夺并进入错误日志?

How can is do the above validation in Bulk on a csv/tabdelimited flat-file with 40K plus records ? 如何对具有40K加记录的csv / tabdelimited平面文件进行批量验证?

I like your approach of evaluating columns instead of row-by-row, this seems to me that it would give you a performance boost. 我喜欢您评估列而不是逐行评估的方法,在我看来,这可以提高性能。

To add to your idea, why not create a function to validate a specific column. 为了增加您的想法,为什么不创建一个函数来验证特定的列。 For the columns with multiple correct values, you just put your check statement into a switch statement or something inside of that function. 对于具有多个正确值的列,只需将您的check语句放入switch语句或该函数内部的内容中即可。

Example: 例:

//Example function validates a field that contains an integer value
function validateField1($x)
{

   //if necessary you could parse apart the value before "switching" it.

   if(isset($x))
   {
      switch($x)
      {
         case 1: //integer 1 is a valid value
            return true;
         case 3: //integer 3 is a valid value
            return true;
         default://for all other values return false
            return false;
      }
   }
}

This way you can evaluate the fields on a case by case basis. 这样,您可以根据情况评估字段。 In addition to that, if your validate function returns a false value you could programmatically edit the .csv file before uploading it to the database. 除此之外,如果您的validate函数返回false值,则可以在将.csv文件上传到数据库之前以编程方式对其进行编辑。

I have no idea if this will work for you, but do let us know how it turns out! 我不知道这是否对您有用,但是请告诉我们结果如何!

Regards, 问候,

H H

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM