[SQL] Insert content file to mysql


 
Thread Tools Search this Thread
Top Forums Programming [SQL] Insert content file to mysql
# 1  
Old 07-13-2011
[SQL] Insert content file to mysql

dear all,
i want to insert string in file to mysql i just want how to do that cause i am poor in sql languages ...
so this file like this
DATA.txt
Code:
doni|student|westjava|123412|lombok|
iwan|student|westjava|1234412|utankayu|
rio|student|westjava|12342|cempedak|

so i want insert DATA.txt to table in mysql like this

table in mysql
Code:
|id| name | status | kota | phone | address |
|1|doni|student|westjava|123412|lombok|
|2|iwan|student|westjava|1234412|utankayu|
|3|rio|student|eastjava|12342|cempedak|

how i can do that if i want content in DATA.txt will insert in table like above
thx for advice
# 2  
Old 07-13-2011
You may try something like this:
Code:
# cat /tmp/data.txt
doni|student|westjava|123412|lombok|
iwan|student|westjava|1234412|utankayu|
rio|student|westjava|12342|cempedak|
# mysql -uroot -p t
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.13-55-log Percona Server (GPL), Release rel20.4, Revision 138

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create table test_t (
    -> id int(10) primary key auto_increment,
    -> name char(20),
    -> status char(20),
    -> kota char(20),
    -> phone int(20),
    -> address char(60)
    ->  );
Query OK, 0 rows affected (0.01 sec)

mysql> load data local infile '/tmp/data.txt'
    -> into table test_t
    -> fields terminated by '|'
    -> lines terminated by '\n'
    -> ( name, status, kota, phone, address );
Query OK, 3 rows affected (0.00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select  * from test_t;
+----+------+---------+----------+---------+----------+
| id | name | status  | kota     | phone   | address  |
+----+------+---------+----------+---------+----------+
|  1 | doni | student | westjava |  123412 | lombok   |
|  2 | iwan | student | westjava | 1234412 | utankayu |
|  3 | rio  | student | westjava |   12342 | cempedak |
+----+------+---------+----------+---------+----------+
3 rows in set (0.00 sec)

mysql>

These 2 Users Gave Thanks to radoulov For This Post:
# 3  
Old 07-14-2011
Quote:
Originally Posted by radoulov
You may try something like this:
Code:
# cat /tmp/data.txt
doni|student|westjava|123412|lombok|
iwan|student|westjava|1234412|utankayu|
rio|student|westjava|12342|cempedak|
# mysql -uroot -p t
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.13-55-log Percona Server (GPL), Release rel20.4, Revision 138

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create table test_t (
    -> id int(10) primary key auto_increment,
    -> name char(20),
    -> status char(20),
    -> kota char(20),
    -> phone int(20),
    -> address char(60)
    ->  );
Query OK, 0 rows affected (0.01 sec)

mysql> load data local infile '/tmp/data.txt'
    -> into table test_t
    -> fields terminated by '|'
    -> lines terminated by '\n'
    -> ( name, status, kota, phone, address );
Query OK, 3 rows affected (0.00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select  * from test_t;
+----+------+---------+----------+---------+----------+
| id | name | status  | kota     | phone   | address  |
+----+------+---------+----------+---------+----------+
|  1 | doni | student | westjava |  123412 | lombok   |
|  2 | iwan | student | westjava | 1234412 | utankayu |
|  3 | rio  | student | westjava |   12342 | cempedak |
+----+------+---------+----------+---------+----------+
3 rows in set (0.00 sec)

mysql>

@radoulov: thx for your help
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert content from file 1 to file 2 in specific criteria meet

Hi , I'm looking for some code that can copy and paste form file1 to file2 with 2 criterial meet. file1: test "sp-j1" test "sp-j2" test "sp-j3" test "sp-j4" file2: sub Pre_Shorts1 (Status_Code, Message$) global Status !if Message$ <> "" then print... (3 Replies)
Discussion started by: kttan
3 Replies

2. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

3. Shell Programming and Scripting

Insert content of a file to another file at a line number which is given by third file

Hi friends, here is my problem. I have three files like this.. cat file1.txt ======= unix is best unix is best linux is best unix is best linux is best linux is best unix is best unix is best cat file2.txt ======== Windows performs better Mac OS performs better Windows... (4 Replies)
Discussion started by: Jagadeesh Kumar
4 Replies

4. UNIX for Dummies Questions & Answers

sed insert content of file.txt to multi files

Ive this sed & find command find /home/www/ -name footer.php -exec sed -i 's/<\/body>/file.txt\n<\/body>/' what I need to place content of file.txt before </body> in all footer.php files file.txt content is google analytic script which is like 7 lines any help to adjust my command to... (2 Replies)
Discussion started by: xmoe
2 Replies

5. Shell Programming and Scripting

convert file into sql insert stmt

My file is now cleaned, sanitized & prepped: 07/07/2008 21:18:51 Installation 52016 complete *BUT NOTHING CHANGED* 07/21/2008 15:28:15 Removal 52016 complete 07/21/2008 15:34:15 Removal 55856 complete 12/08/2009 19:30:40 Installation 62323 complete 12/08/2009 19:39:06 Installation ... (6 Replies)
Discussion started by: dba_frog
6 Replies

6. Emergency UNIX and Linux Support

Insert data into sql queries from a file

Hello friends, I need to insert data from a file to another. I need this to form an sql query file which will consist of 50.000 INSERT INTO sentences. my sql query file will consist of 50.000 times the below line consecutively: insert into subscriber... (6 Replies)
Discussion started by: EAGL€
6 Replies

7. Shell Programming and Scripting

how to break mysql dump sql file

Hi folks I have mysql dump which having insert queries, i want to break that file when 10 complete "INSERTS" lines so extract that line and store in 1.sql and 2.sql and for next 10 insert lines. pls guide me how can i do that. Regards, Bash (2 Replies)
Discussion started by: learnbash
2 Replies

8. Shell Programming and Scripting

How to insert data into MYSql database from a text file

Hi, Need to get help from you guys about this issue. I need to insert data into MySql database from a text file which is located in other server. The text file is something look like below: Date | SubscriberNo | Call Duration 20/7/07 | 123456788 | 20 20/7/07 | 123412344 | 30 The... (4 Replies)
Discussion started by: shirleyeow
4 Replies

9. Shell Programming and Scripting

Insert file content via sed after two searchings

Hi folks, The file webcache.xml contains a lot sections which begins and ends with the string </CACHEABILITYRULE>. The section In need to deel with is: </CACHEABILITYRULE> <CACHEABILITYRULE NAME="cache swf" CACHE="YES" COMMENT="This rule caches all .swf files. This... (2 Replies)
Discussion started by: nir_s
2 Replies
Login or Register to Ask a Question