First load of a txt file using the sql command line - mysql

I work locally with WAMPSERVER2.5
I built my dadabase on OpenOfficeCalc: db.txt
- Character set: Unicode (UTF-8)
- Field separator: ,
- Text separator: "
I want to import "db.txt" in a table named "db_pma" which is in the database named "hct" (in PhpMyAdmin)
"db.txt" contains 2 fields:
- string_field (character string, max:30)
- numeric_field (sample: 5862314523685.256325632)
Questions:
1) Do I need to first create in PhpMyAdmin the structure of the table ("db_pma") which will contain the data?
2) I tried this code to import "db.txt"
mysql -h localhost -u root
LOAD DATA LOCAL INFILE 'db.txt'
INTO TABLE dp_pma
FIELDS TERMINATED BY ','
LINES TERMINATED BY 'AUTO';
It doesn't work.
Could you help me?

Use LINES TERMINATED BY '\r\n' (for Windows) or LINES TERMINATED BY '\n' (for Linux) instead.

Related

inserting csv files into mysql db

I have csv file with delimiter ';'. It looks following (third column should be empty):
id;theme_name;description
5cbde2fe-b70a-5245-bbde-c2504a4bced1;DevTools;allow web developers to test and debug their code. They are different from website builders and integrated development environments (IDEs) in that they do not assist in the direct creation of a webpage, rather they are tools used for testing the user interface of a website or web application.
c8bfc406-aaa6-5cf9-94c3-09fc54b934e7;AI;
Here is my script for inserting data from csv into db:
mysql -u ${MYSQL_USER} --password=${MYSQL_PASSWORD} ${MYSQL_DATABASE} --local-infile=1 -h ${MYSQL_HOST} -e"LOAD DATA LOCAL INFILE '/tmp/init_data/$file' INTO TABLE $table_name FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '\"' IGNORE 1 LINES";
When I'm making SELECT statement, Im getting carriage return (\r) in last column in response:
Here is response from mysql
{
"themeName": "DevTools",
"description": "allow web developers to test and debug their code. They are different from website builders and integrated development environments (IDEs) in that they do not assist in the direct creation of a webpage, rather they are tools used for testing the user interface of a website or web application.\r"
}, {
"themeName": "AI",
"description": "\r"
}
When I add delimiter ';' after last column in csv file, carriage return disappeared from response.
for example: c8bfc406-aaa6-5cf9-94c3-09fc54b934e7;AI;;
Why mysql add \r into third column ?
Is there any possible way how to solve it ? (except replace in select statement)
Thanks
I bet your CSV file comes from Windows. Those files have \r\n at the end of every line.
Add this to your command ...
LINES TERMINATED BY '\\r\\n'
and things should work. If they don't try this
LINES TERMINATED BY '\r\n'
On UNIX-derived systems (like Linux) the default LINES TERMINATED BY '\n' works.
On Mac systems you need LINES TERMINATED BY '\r'.
If you add a trailing ; column separator you create a fourth column in your CSV. But the table you're loading only has three columns, so LOAD DATA INFILE ignores that fourth column, which has a \r in it.
Why this difference, you ask? Old-timey teletype devices (the original terminals for MS-DOS and UNIX) needed a Return code to move the print head back to the first column, and a Newline code to move the paper up one line. The UNIX Bell Labs team decided their tty driver would add the extra Return code so lines ended with a particular single character. MS-DOS's team (umm, Bill Gates) left them both in.
Why Mac with just Return? Maybe somebody knows.
Answer:
According to #O.Jones answer I needed to add LINES TERMINATED BY '\r' but alsoI need to add \n
mysql -u ${MYSQL_USER} --password=${MYSQL_PASSWORD} ${MYSQL_DATABASE} --local-infile=1 -h ${MYSQL_HOST} -e"LOAD DATA LOCAL INFILE '/tmp/init_data/$file' INTO TABLE $table_name FIELDS TERMINATED BY ';' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES";

Importing CSV file to HeidiSQL, error 1148

I'm completely novice to MySQL, and I'm trying to load some CSV sheets to the server using HeidiSQL -> tools -> Import CSV. But it keeps giving me this error:
Error 1148: The used command is not allowed with this MySQL version".
Is there a way to fix that, or maybe another way to load a CSV?
For query based csv import you can use load data ... to load csv files.
For more info refer here
Example:
To skip the first line which is header in csv you can use ignore 1 lines
load data local infile 'D:std_table.csv' into table local.student
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n'
ignore 1 lines;
For windows based system use \r\n for line termination and for Linux use \n
Side Note:
You could try using MySQL Workbench for MySQL from official site here.
try this one:
LOAD DATA INFILE 'c:/tmp/discounts.csv'
INTO TABLE discounts
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

How to import xls file in to MySQL table

I'm using ubuntu 14.04 operating system and I want to import excel sheet(.xls) in to database.
I am using phpmyadmin mysql administration tool. If any one can give me the commands to import xls to mysql I would be grateful.
Import CSV file in to MySQL Table
Change file type in to CSV
Create table according to excel column names
Place the csv file in /var/lib/mysql
Open phpmyadmin sql command pane:
Execute this sql command
LOAD DATA INFILE 'testlist.csv'
INTO TABLE TEST
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Use the load data capability. See
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Export CSV file in to MySQL Table
SELECT ... FROM ... WHERE ...
INTO OUTFILE 'testlist.csv'
FIELDS TERMINATED BY ','
Here is an example that produces a file in the comma-separated values (CSV) format used by many programs:
SELECT * INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;

Importing into mysql with symbols

I have some data (in csv) per country taken from a third-party source, and I am having some issues importing them into mySQL.
For example, one column in my table is Country with value 'Côte d'Ivoire' - the import in mysql appears to divide this one row of data into two with Country value of 'C'. It is unable to import the text value of 'Côte d'Ivoire'.
This is what I used for the import:
TRUNCATE TABLE source_DATA_TABLE;
LOAD DATA INFILE 'H://TESTDATA/2015/source_DATA.csv'
INTO TABLE source_proc_pqr
FIELDS TERMINATED BY '\#'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
I changed the delimited through my regional settings on my PC to # but the same problem exists.
Anyone have a fix to this problem? I'm using mySQL workbench (xampp/phpmyadmin).
I tried something and it works. first of all the file should be in utf8. and then you can load it like this:
LOAD DATA INFILE 'H://TESTDATA/2015/source_DATA.csv'
INTO TABLE source_proc_pqr
CHARACTER SET UTF8
FIELDS TERMINATED BY '\#'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;

Importing data from Sybase to MySQL

I'm migrating an old Sybase database to a new MySQL db.
Since the moment that Sybase can export data to .dat files (something similar to new csv), I decided to use it.
The problem is that Sybase uses commas as column separator and commas in strings are ignored because are enclosed in '', but not in MySQL.
Is there a way to solve the problem? Here's my query:
LOAD DATA LOCAL INFILE 'C:\\UNLOAD\\166.dat'
INTO TABLE linea_col
COLUMNS TERMINATED BY ','
LINES TERMINATED BY '\r\n';
Thank you in advance.
If they are enclosed by single quotes try this:
LOAD DATA LOCAL INFILE 'C:\\UNLOAD\\166.dat'
INTO TABLE linea_col
COLUMNS TERMINATED BY ','
ENCLOSED BY '\''
LINES TERMINATED BY '\r\n';
If that doesn't work then you should do ENCLOSED BY '''' but I'm 99.99% certain the first is correct.
I'm not familiar with how DAT files may differ to CSV, but you can use CSV as the common format.
You can export from Sybase using BCP and forcing the output to use CSV format
bcp database.dbo.tbl_name out c:\temp\output.csv -c -t, -U user_name -P password -S server_name
Then import into MySQL with
load data local infile 'uniq.csv' into table tblUniq fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(uniqName, uniqCity, uniqComments)