问题
I have the following code. I am trying to import a text file in to sql table using php as suggested by one of the users on this site.
Unfortunately my import got errors half way due to my text file having "Max and Min" words in the file.
I tried to find out what i can do to avoid it. Most of the stuff i found was about using reserved words in the column name. But mine is not a column name it is inserted in the columns as data.
Can this be avoided as I don't know how many other reserved words are present in the text file and I need to make my code run automatically everyday. I can't have it aborting every time. It is a huge text file so I can't manually replace keywords everytime either.
mysqli_query("CREATE TABLE IF NOT EXISTS `add_feature_id` (
`id_f` INT(10) unsigned NOT NULL AUTO_INCREMENT,
`id_product` INT(10) unsigned NOT NULL,
`id_feature` INT(10) unsigned NOT NULL,
`value` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL
DEFAULT NULL,PRIMARY KEY ( `id_f` ) )",$conn);
$fd = fopen('trial.txt', 'r');
$fheader = fgets($fd);
while (($data = fgetcsv($fd,0, "~")) !== FALSE) {
$id_product = $data[0];
$id_feature = $data[1];
$unitval = $data[2];
$value = mysql_real_escape_string($unitval);
mysqli_query("INSERT INTO `add_feature_id`(`id_product`,`id_feature`,`value`)
VALUES ($id_product,$id_feature,'$value')",$conn) or die(mysql_error());
}
fclose($fd);
$result = mysqli_query("SELECT * FROM `add_feature_id`",$conn);
//I print my result here but i get error while insert is executed
The error i am getting is
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' Max and Min Requirements
Low entry level requirement Minimu' at line 2
This is the part of my text file where the error occurs Max and Min Requirements as can be seen in the text file below
IMSKU~AttributeID~Value~Unit~StoredValue~StoredUnit(header row)
1006854 ~ 16257 ~Licensing Program: Max and Min Requirements<ul><li>Low entry level requirement</li><li>Minimum 1 server (Band S) OR 5 desktop (Band A)</li></ul> ~ ~ 0.00 ~
回答1:
You've got invalid data on line 32325:
1062708~16257~Express Licensing Program:<ul><li>Targeted at small - medium companies (1-500 units)</li><li>Minimum purchase requirements for licenses</li><li>Includes all Symantec software products</li><li> Certificated-based program - requires no legal review</li><li>Band identified via number of units per transaction</li></ul>
<br />Max and Min Requirements<ul><li>Low entry level requirement</li><li>Minimum 1 server (S-Band) OR 5 desktop (A-H Band)</li></ul>~~0.00~
1062708~16260~2~~0.00~
That middle line.
The reason it fails is because $id_product
and $id_feature
aren't in single quotes/or escaped.
What you should do is prepare/execute this (or get rid of that invalid data).
回答2:
How about quote encapsulated strings with:
LOAD DATA INFILE '/tmp/trial.txt'
INTO TABLE add_feature_id
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n' -- or \r\n
https://dev.mysql.com/doc/refman/5.1/en/load-data.html
Edit:
create table fred
(
fullName varchar(255) not null,
age int not null
);
fred2.txt:
"jason"~50
"fred max min max smith"~87
"jason avg abs sum smith"~12
load data infile 'c:\\dev\\fred2.txt'
into table fred
fields terminated by '~'
optionally enclosed by '"'
lines terminated by '\r\n'
select * from fred
yep they came in :)
来源:https://stackoverflow.com/questions/31373944/sql-reserved-keywords-causing-errors-while-importing-data-from-text-file