- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Exporting and importing data in MySQL
In this part of the MySQL tutorial, we will be exporting data from MySQL database and importing data back.
Simple data export
In our first example, we will save data in a text file.
mysql> SELECT * FROM Cars INTO OUTFILE '/tmp/cars'; Query OK, 8 rows affected (0.00 sec)
We select all rows (8) from the Cars
table into the cars file located in the /tmp
directory. We need to have permissions to write to that directory.
$ cat /tmp/cars 1 Audi 52642 2 Mercedes 57127 3 Skoda 9000 4 Volvo 29000 5 Bentley 350000 6 Citroen 21000 7 Hummer 41400 8 Volkswagen 21600
We show the contents of the file.
mysql> DELETE FROM Cars; mysql> LOAD DATA INFILE '/tmp/cars' INTO TABLE Cars;
In the first statement we delete all rows from the table. In the second statement we load all data from the text file into the Cars table.
mysql> SELECT * FROM Cars INTO OUTFILE '/tmp/cars.csv' -> FIELDS TERMINATED BY ',';
In the above SQL statement, we dump all data from the Cars table into a cars.csv file. The FIELDS TERMINATED BY
clause controls, how the data will be terminated in the text file. We have chosen a comma character. The CSV stands for Comma Separated Values and it is a very common and very portable file format. It can be imported by numerous other applications like OpenOffice, other databases etc.
$ cat /tmp/cars.csv 1,Audi,52642 2,Mercedes,57127 3,Skoda,9000 4,Volvo,29000 5,Bentley,350000 6,Citroen,21000 7,Hummer,41400 8,Volkswagen,21600
This is the contents of the cars.csv file.
mysql> DELETE FROM Cars; mysql> LOAD DATA INFILE '/tmp/cars.csv' INTO TABLE Cars -> FIELDS TERMINATED BY ','; mysql> SELECT * FROM Cars; +----+------------+--------+ | Id | Name | Cost | +----+------------+--------+ | 1 | Audi | 52642 | | 2 | Mercedes | 57127 | | 3 | Skoda | 9000 | | 4 | Volvo | 29000 | | 5 | Bentley | 350000 | | 6 | Citroen | 21000 | | 7 | Hummer | 41400 | | 8 | Volkswagen | 21600 | +----+------------+--------+
We delete all the data and restore it from the cars.csv
file.
Exporting to XML files
It is possible to export and import XML data using the mysql
monitor.
$ mysql -uroot -p --xml -e 'SELECT * FROM mydb.Cars' > /tmp/cars.xml
The mysql
monitor has an --xml
option, which enables us to dump data in XML format. The -e
option executes a statement and quits the monitor.
$ cat /tmp/cars.xml <?xml version="1.0"?> <resultset statement="SELECT * FROM mydb.Cars " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <row> <field name="Id">1</field> <field name="Name">Audi</field> <field name="Cost">52642</field> </row> <row> <field name="Id">2</field> <field name="Name">Mercedes</field> <field name="Cost">57127</field> </row> <row> <field name="Id">3</field> <field name="Name">Skoda</field> <field name="Cost">9000</field> </row> <row> <field name="Id">4</field> <field name="Name">Volvo</field> <field name="Cost">29000</field> </row> <row> <field name="Id">5</field> <field name="Name">Bentley</field> <field name="Cost">350000</field> </row> <row> <field name="Id">6</field> <field name="Name">Citroen</field> <field name="Cost">21000</field> </row> <row> <field name="Id">7</field> <field name="Name">Hummer</field> <field name="Cost">41400</field> </row> <row> <field name="Id">8</field> <field name="Name">Volkswagen</field> <field name="Cost">21600</field> </row> </resultset>
This is the XML file generated by the mysql
monitor.
mysql> TRUNCATE Cars; mysql> LOAD XML /tmp/cars.xml INTO TABLE Cars;
We truncate the Cars table. We load data from the XML file. Note that LOAD XML
statement is available for MySQL 5.5 and newer.
Using mysqldump tool
The mysqldump
is a command tool to create backups for MySQL. The word dump is used when we transfer data from one place to another. From a database file to a text file. From a memory to a file. And similar.
Dumping table structures
mysqldump -u root -p --no-data mydb > bkp1.sql
The above command dumps table structures of all tables in the mydb
database to the bkq1.sql
file. The --no-data
option causes that the data is not saved, only the table structures.
-- -- Table structure for table `Cars` -- DROP TABLE IF EXISTS `Cars`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Cars` ( `Id` int(11) NOT NULL, `Name` varchar(50) DEFAULT NULL, `Cost` int(11) DEFAULT NULL, PRIMARY KEY (`Id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */;
Here we see a portion of the bkp1.sql
file. This is the SQL for the creation of the Cars
table.
Dumping data only
$ mysqldump -uroot -p --no-create-info mydb > bkp2.sql
This command dumps all data from all tables of the mydb
databases. It omits the table structures. The omission of the table structures is caused by the --no-create-info
option.
-- -- Dumping data for table `Cars` -- LOCK TABLES `Cars` WRITE; /*!40000 ALTER TABLE `Cars` DISABLE KEYS */; INSERT INTO `Cars` VALUES (1,'Audi',52642),(2,'Mercedes',57127),(3,'Skoda',9000), (4,'Volvo',29000),(5,'Bentley',350000),(6,'Citroen',21000), (7,'Hummer',41400),(8,'Volkswagen',21600); /*!40000 ALTER TABLE `Cars` ENABLE KEYS */; UNLOCK TABLES;
Here we can see the data for the Cars
table.
Dumping the whole database
$ mysqldump -uroot -p mydb > bkp3.sql
This command dumps all tables from the mydb
database to the bkp3.sql
file.
Restoring data
We show, how to restore the database from the backup SQL files.
mysql> DROP DATABASE mydb; ERROR 1010 (HY000): Error dropping database (can't rmdir './mydb/', errno: 17) mysql> SHOW TABLES; Empty set (0.00 sec)
We drop the mydb
database. An error is shown. The tables were dropped but not the database.
$ sudo ls /var/lib/mysql/mydb cars cars.txt $ sudo rm /var/lib/mysql/mydb/cars $ sudo rm /var/lib/mysql/mydb/cars.txt
The reason is that (in my case) while doing backups, some of the data were written in the mydb
directory, in which MySQL stores the mydb
database. These two alien files could not be removed, hence the above error. By removing the files the error is fixed.
mysql> DROP DATABASE mydb; Query OK, 0 rows affected (0.04 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | testdb | | world | +--------------------+ 4 rows in set (0.00 sec)
The mydb database was fully removed.
mysql> CREATE DATABASE mydb; mysql> USE mydb; mysql> source bkp3.sql
We create the mydb
database. Change to the database. And use the source command to execute the bkp3.sql
script. The database is recreated.
mysql> SHOW TABLES; +----------------+ | Tables_in_mydb | +----------------+ | AA | | Ages | | Animals | | Authors | | BB | | Books | | Books2 | | Brands | | Cars | ... mysql> SELECT * FROM Cars; +----+------------+--------+ | Id | Name | Cost | +----+------------+--------+ | 1 | Audi | 52642 | | 2 | Mercedes | 57127 | | 3 | Skoda | 9000 | | 4 | Volvo | 29000 | | 5 | Bentley | 350000 | | 6 | Citroen | 21000 | | 7 | Hummer | 41400 | | 8 | Volkswagen | 21600 | +----+------------+--------+
The data is verified.
In this part of the MySQL tutorial, we have shown several ways how we can export and import data in MySQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论