This tutorial describes how to transfer a MySQL/MariaDB schema (both structure and data) to an SQLite database file.
- UNIX-like operating system
- accessible MySQL/MariaDB server, with read permission on the schema to be converted (details at mysqldump page)
- MySQL/MariaDB command line client
- SQLite
- one of the many implementations of AWK, e.g. GNU Awk
-
if not already available, create the MySQL/MariaDB data source:
-
create the schema to convert:
$ mysql -h[host] -u[username] -p Enter password: [password] Welcome to the MySQL [... and the rest of MySQL welcome message]
mysql> CREATE SCHEMA my_database; Query OK, 1 row affected (0.00 sec) mysql> USE my_database; Database changed
-
together with a table:
mysql> CREATE TABLE users ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, username VARCHAR(100) UNIQUE, created TIMESTAMP DEFAULT NOW() ) DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.12 sec)
-
and populate it with a few records:
mysql> INSERT INTO users (username) VALUES ('user_1'); Query OK, 1 row affected (0.05 sec) mysql> INSERT INTO users (username) VALUES ('user_2'); Query OK, 1 row affected (0.05 sec) mysql> INSERT INTO users (username) VALUES ('user_3'); Query OK, 1 row affected (0.05 sec)
mysql> EXIT;
-
-
retrieve mysql2sqlite script:
-
download mysql2sqlite script from its GitHub repository
-
make the script executable:
$ chmod +x mysql2sqlite.sh
-
Export data via MySQL/MariaDB client command line interface (or any MySQL/MariaDB client you like, of course):
$ mysqldump -h[host] -u[username] -p my_database > mysql_dump.sql --compact
Enter password: [password]
Output file mysql_dump.sql content will look like:
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) DEFAULT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO `users` VALUES (1,'user_1','2017-03-12 09:18:04'),
(2,'user_2','2017-03-12 09:18:06'),
(3,'user_3','2017-03-12 09:18:08');
$ ./mysql2sqlite.sh mysql_dump.sql > sqlite_dump.sql
Output file sqlite_dump.sql content will look like:
PRAGMA synchronous = OFF;
PRAGMA journal_mode = MEMORY;
BEGIN TRANSACTION;
CREATE TABLE `users` (
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
`username` varchar(100) DEFAULT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (`username`)
);
INSERT INTO `users` VALUES (1,'user_1','2017-03-12 09:18:04'),
(2,'user_2','2017-03-12 09:18:04'),
(3,'user_3','2017-03-12 09:18:04');
END TRANSACTION;
Further details about how SQLite handles data types other than TEXT, INTEGER, REAL, NUMERIC and BLOB are available on SQLite documentation
Import data via SQLite command line interface:
$ sqlite3 my_database.sqlite
SQLite version 3.8.2 [... and the rest of SQLite welcome message]
sqlite> .read sqlite_dump.sql
memory
sqlite> .exit
$ sqlite3 my_database.sqlite
SQLite version 3.8.2 [... and the rest of SQLite welcome message]
sqlite> .tables
users
sqlite> .schema users
CREATE TABLE `users` (
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
`username` varchar(100) DEFAULT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (`username`)
);
sqlite> SELECT * FROM users;
1|user_1|2017-03-12 09:18:04
2|user_2|2017-03-12 09:18:04
3|user_3|2017-03-12 09:18:04
sqlite> .exit