Skip to main content

Posts

Showing posts from April, 2010

How to Export data in a file from MySQL?

There are scenarios like when changing a web server when you have to export data in file that can be imported in some other server. Here is an example to export data of a query in file from MySQL . SELECT * INTO OUTFILE 'filePath' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' FROM tablename; Where filepath is path of the file you want  to export data to.

Backing Up and Restoring MySQL Database

Creating a backup is very important and common task  In this article, I will outline how to back up and restore database in MySQL. A Simple Database Backup: You can use mysqldump to create a backup . Here is general syntax to do that mysqldump -u [username] -p [password] [databasename] > [backupfile.sql] Restore using mysql If you want to restore mysqldump file you can use mysql command.  Here is general structure of mysql command: mysql -u [username] -p [password] [database_to_restore] < [backupfile] Note: Use common command prompt for these commands instead of MySQL shell.

Loading data in MySQL Table from a Text File

There are many situation when we need to load some text data into a table. In this post I will tell you some simple steps to load Data into a MySQL table from a text File . First of all data should be in proper format Fields/ Columns should be separated by some character like space, tab or comma etc. Rows /Records/ Lines should be separated by some character like "\r\n" if there is one record per line. If data is Enclosed using some character it should be same for all columns. Create a Table in MySQL to hold data. Use LOAD DATA command to load data into the Table. Example: I have following data saved in a file c:\sample.txt "http://dbpedia.org/resource/AfghanistanHistory" "http://www.w3.org/2000/01/rdf-schema#label" "AfghanistanHistory" "http://dbpedia.org/resource/AfghanistanGeography" "http://www.w3.org/2000/01/rdf-schema#label" "AfghanistanGeography" "http://dbpedia.org/resource/Access