Backslash throws unexpected beginning of statement error - mysql

So I have this SQL Script for bulk loading my data:
INSERT INTO DeliveryMethod (deliveryMethod)
VALUES ('Bicycle');
INSERT INTO DeliveryMethod (deliveryMethod)
VALUES ('Car');
INSERT INTO DeliveryMethod (deliveryMethod)
VALUES ('Van');
INSERT INTO DeliveryMethod (deliveryMethod)
VALUES ('None');
SET foreign_key_checks = 0;
# Load data into categories
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/CategoryData.txt'
REPLACE INTO TABLE CourierDB.Category
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(name)
SET categoryID = NULL; # Should trigger auto increment (or \r)
# Load data into package
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/PackageData.txt'
REPLACE INTO TABLE CourierDB.Package
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(price, itemName, category)
SET packageID = NULL; # Should trigger auto increment (or \r)
# Load data into address
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/AddressData.txt'
REPLACE INTO TABLE CourierDB.Address
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(buildingName, streetName, county, postcode)
SET addressID = NULL;
# Load data into packages
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/PackagesData.txt'
REPLACE INTO TABLE CourierDB.Packages
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(packagesID, package);
# Load data into branch
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/BranchData.txt'
REPLACE INTO TABLE CourierDB.Branch
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(branchName, address, headOfficeID, managerID, deliveryMethods)
SET branchID = NULL;
# Load data into consignment
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/ConsignmentData.txt'
REPLACE INTO TABLE CourierDB.Consignment
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(dispatchDate, consignmentType, branch, deliveryAddressID, returnAddressID, packages)
SET trackingID = NULL;
# Load data into DeliveryMethods
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/DeliveryMethodsData.txt'
REPLACE INTO TABLE CourierDB.DeliveryMethods
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(deliveryMethodID, deliveryMethod);
# Load data into Employee
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/EmployeeData.txt'
REPLACE INTO TABLE CourierDB.Employee
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(NIN, firstName, lastName, dateOfBirth, emailAddress, mobileNo, salary, branchID, supervisorID, address)
SET staffNo = NULL;
# Load data into CustomerConsignments
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/CustomerConsignmentsData.txt'
REPLACE INTO TABLE CourierDB.CustomerConsignments
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(consignmentsID, consignment);
# Load data into Customer
LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/CustomerData.txt'
REPLACE INTO TABLE CourierDB.Customer
FIELDS TERMINATED BY ','
LINES STARTING BY '(' TERMINATED BY ')\r'
(firstName, lastName, dateOfBirth, emailAddress, mobileNo, customerBranchID, address, consignments)
SET customerID = NULL;
SET foreign_key_checks = 1;
However with this said... I'm getting this error here for some reason when i put it into my relational schema:
Static analysis:
3 errors were found during analysis.
Unexpected character. (near "\" at position 0) Unexpected beginning of
statement. (near "\" at position 0) Unexpected beginning of statement.
(near "r" at position 1) SQL query:
\r LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4/PackageData.txt'
REPLACE INTO TABLE CourierDB.Package FIELDS TERMINATED BY ',' LINES
STARTING BY '(' TERMINATED BY ')\r' (price, itemName, category) SET
packageID = NULL
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near '\r LOAD DATA LOCAL INFILE
'C:/Users/ryank/OneDrive/Documents/GitHub/DBMS-CW/R4' at line 1
Any help would be greatly appreciated! Thanks

Related

Load data infile ignore 1 lines skip 2 lines

I want to import a CSV file to my MySQL table using Load data infile, but sometimes it skips 2 lines.
My SQL:
LOAD DATA LOW_PRIORITY LOCAL INFILE 'C:\\csv\\member.csv'
REPLACE INTO TABLE `member`
CHARACTER SET utf8mb4 FIELDS
TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES (`UserName`, `RegDate`, `Address`, `Remark`);
Data:
"UserName","RegDate","Address","Remark"
"Peter",07-AUG-2001 18:17,"10th Floor, xxx's PlazaI","Remark xxx" xxx"
"Mary",07-NOV-2001 15:17,,"The ""BIG"" boss"
"Paul",07-DEC-2001 15:17,"10th Floor, xxx's PlazaI",""

How do I find and replace in a CSV I'm importing using mysql

I'm importing a CSV file into Heidi SQL. I've created the table using this code:
create table all_data
(
Keyword varchar(1000),
Position int,
Previous_Position int,
Search_Volume int,
KW_Difficulty float,
URL varchar(1000),
Post_title varchar(1000),
Post_URL varchar(1000),
Genre varchar(1000),
Location varchar(1000),
Avg_Daily_Visitors float,
pageviews int
)
;
but in the Avg_Daily_visitors column it has "\N" where there is no value. I've been importing the data with this code:
load data local infile 'C:/filepath.../All_Data.csv'
replace into table all_data
fields terminated by ','
enclosed by '"'
escaped by '"'
lines terminated by "\r\n"
ignore 1 rows
set
Avg_Daily_Visitors = replace(Avg_Daily_Visitors,"\N",0),
pageviews = replace(pageviews,"\N", 0)
;
but it's not replacing the values with 0, which is what I want to achieve. How do I make Heidi SQL replace "\N" with "0" on import?
Thanks.
First assign the value you read to a variable, then work on that variable. For this you specify the columns of your destination table, but a variable instead of the column where you want to replace.
load data local infile 'C:/filepath.../All_Data.csv'
replace into table all_data
fields terminated by ','
enclosed by '"'
escaped by '"'
lines terminated by "\r\n"
ignore 1 rows
(column_1, column_2, #variable1, #variable2, column_5)
set
Avg_Daily_Visitors = replace(#variable1,"\N",0),
pageviews = replace(#variable2,"\N", 0)
;

How to convert string to date time format Mysql

I have this file csv:
20170117-00:00:6 3087A6B282A46C 124.191.134.139 2746 256 63628
20170117-00:00:53 330391C9F58B5A 120.18.97.172 2746 257 43077
20170117-00:02:27 3303CD14BD40C8 110.147.131.78 2746 256 63628
20170117-00:02:00 3304B3CD448E4F 189.35.218.62 2746 255 43074
and I am trying to upload it using load data local infile but I obtain only Null value in the time column. Here there is my code (Time is the name of the table column)
LOAD DATA LOCAL INFILE 'path.log'
INTO TABLE Clicks
CHARACTER SET latin1
FIELDS TERMINATED BY '\t'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
SET Time= STR_TO_DATE(#Time,'%Y%m%d-%H:%i:%s')
;
This is the syntax you should be using:
LOAD DATA LOCAL INFILE 'path.log'
INTO TABLE Clicks
CHARACTER SET latin1
FIELDS TERMINATED BY '\t'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(#Time, column2, column3, column4, column5) -- read time data into #Time
SET column1 = STR_TO_DATE(#Time, '%Y%m%d-%H:%i:%s') -- then set column1
I think the #Time variable was never being set to anything for each record being read, hence you were getting NULL in the first column.
Try This
LOAD DATA LOCAL INFILE 'path.log'
INTO TABLE Clicks
CHARACTER SET latin1
FIELDS TERMINATED BY '\t'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
SET TIME= CONCAT(SUBSTRING(#Time,1,4),'-',SUBSTRING(#Time,5,2),'-',SUBSTRING(#Time,7,2),' ',REPLACE(#Time,CONCAT(SUBSTRING_INDEX(#Time, '-',1),'-'),''))

Can you define fields and values with a "LOAD DATA LOCAL INFILE" query in MySQL?

I'm trying to import data into a MySQL table with values not defined in the input file. I have this php string concatenated query:
"LOAD DATA LOCAL INFILE '".#mysql_escape_string($this->file_name).
"' IGNORE INTO TABLE `".$this->table_name.
"` FIELDS TERMINATED BY '".#mysql_escape_string($this->field_separate_char).
"' OPTIONALLY ENCLOSED BY '".#mysql_escape_string($this->field_enclose_char).
"' ESCAPED BY '".#mysql_escape_string($this->field_escape_char).
"' LINES TERMINATED BY '". $this->line_separate_char .
"' ".
($this->use_csv_header ? " IGNORE 1 LINES " : "")
Is there a way I can set the account id in this load statement?
As documented under LOAD DATA INFILE Syntax:
The SET clause can be used to supply values not derived from the input file. The following statement sets column3 to the current date and time:
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, column2)
SET column3 = CURRENT_TIMESTAMP;
You can call functions and SQL statements within load data like this:
load data local infile '/tmp/foo.txt' into table foo
fields terminated by '\t' lines terminated by '\n'
(#col1, #col2)
set myid=#col1,
username=CONCAT(#col2, 'abc', 'xyz');

load data infile with unknown system variable error

I´ve been trying to use load data infile procedure to load data from a csv file to mysql, althougt I'ven been unable to due it because of a unknown variable error in the set statement.
LOAD DATA LOCAL INFILE 'D:\\Bets\\BD\\tables\\match.csv'
INTO TABLE `bets`.`match`
FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"'
LINES TERMINATED BY '\r\n'
(`idMatch`, `idChampionship`, #date, `homeTeam`, `awayTeam`, `homeTeamGoals`, `awayTeamGoals`, `matchType` );
SET matchDate = STR_TO_DATE(#date, 'YYYY-MM-DD')
The date field to import is in format 2011-08-07.
when running the query the error given is: SQL Error (1193): Unknown system variable 'matchDate'.
any help would be pappreciated... thanks!
When SET matchDate it should be like this:
SET matchDate = STR_TO_DATE(#date, '%Y-%m-%d');
instead of SET matchDate = STR_TO_DATE(#date, 'YYYY-mm-dd');
Example of MySQL STR_TO_DATE function you can read here.
I've stumbled upon the same issue. The SET line must be part of the LOAD DATA INFILE ... statement. In your code, the column expression is terminated by a semicolon, which executes the SET command after the LOAD DATA INFILE finishes. The user variables created in the statement are not existing any more after the statement completes. Correct statement would be as follows,
LOAD DATA LOCAL INFILE 'D:\\Bets\\BD\\tables\\match.csv'
INTO TABLE `bets`.`match`
FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"'
LINES TERMINATED BY '\r\n'
(`idMatch`, `idChampionship`, #date, `homeTeam`,
`awayTeam`,`homeTeamGoals`, `awayTeamGoals`, `matchType`)
SET matchDate = STR_TO_DATE(#date, 'YYYY-MM-DD');