Please help me with exporting a MySQL database into a SQLite database.
-
3The SQLite project also has a page on conversion utilities: sqlite.org/cvstrac/wiki?p=ConverterToolsClinton– Clinton2011-03-02 06:15:46 +00:00Commented Mar 2, 2011 at 6:15
-
@Clinton there is a warning on the page you've linked to that the information is obsolete.kjones– kjones2017-07-29 07:42:05 +00:00Commented Jul 29, 2017 at 7:42
-
1@Clinton link returns "Not Found". It looks like those converter tools are no longer available/supportedEAmez– EAmez2021-03-24 11:47:58 +00:00Commented Mar 24, 2021 at 11:47
-
2@EAmez - www2.sqlite.org/cvstrac/wiki?p=ConverterToolsGreenROBO– GreenROBO2021-11-08 07:10:53 +00:00Commented Nov 8, 2021 at 7:10
-
Use DBeaver: dbeaver.com/docs/wiki/Data-migrationKike Lebowski– Kike Lebowski2023-08-15 12:41:01 +00:00Commented Aug 15, 2023 at 12:41
9 Answers
There's a fantastic Linux shell script on Github that converts Mysql to an Sqlite3 file. You need both mysqldump and sqlite3 installed on your server. Works great.
6 Comments
2016-05-11 17:32 GMT+2 @esperlu declared MIT as a fitting license (also retrospectively) and the original gist as deprecated.The answer by @user2111698 edited by @quassy works as promised. Since I do this frequently I put their instructions into a bash script:
#!/bin/bash
mysql_host=localhost
mysql_user=george
mysql_dbname=database
sqlite3_dbname=database.sqlite3
# dump the mysql database to a txt file
mysqldump \
--skip-create-options \
--compatible=ansi \
--skip-extended-insert \
--compact \
--single-transaction \
-h$mysql_host \
-u$mysql_user \
-p $mysql_dbname \
> /tmp/localdb.txt
# remove lines mentioning "PRIMARY KEY" or "KEY"
cat /tmp/localdb.txt \
| grep -v "PRIMARY KEY" \
| grep -v KEY \
> /tmp/localdb.txt.1
# mysqldump leaves trailing commas before closing parentheses
perl -0pe 's/,\n\)/\)/g' /tmp/localdb.txt.1 > /tmp/localdb.txt.2
# change all \' to ''
sed -e 's/\\'\''/'\'''\''/g' /tmp/localdb.txt.2 > /tmp/localdb.txt.3
if [ -e $sqlite3_dbname ]; then
mv $sqlite3_dbname $sqlite3_dbname.bak
fi
sqlite3 $sqlite3_dbname < /tmp/localdb.txt.3
A gist with detailed comments can be found at https://gist.github.com/grfiv/b79ace3656113bcfbd9b7c7da8e9ae8d
4 Comments
mysql2sqlite.sh mentioned in the top post doesn't cope well with PRIMARY KEY lines, it doesn't write the trailing ) to complete the CREATE statement.
This is what I did. I ran the mysql dump as:
mysqldump --skip-create-options --compatible=ansi --skip-extended-insert --compact --single-transaction -h<host> -u<user> -p<passwd> <database name> > localdb.txt
I then used grep to remove PRIMARY KEY and KEY:
cat localdb.txt | grep -v "PRIMARY KEY' | grep -v KEY > localdb.txt.1
I then used an editor to fix the file. When the keys are removed you end up with a CREATE statement that looks like:
CREATE ...
...,
)
That trailing , has to be removed. In vi this expression matches them, ,$\n)
Then you need to change all \' to ''
Then you can do the import:
sqlite3 local.sqlite3 < localdb.txt.1
And that's it. I haven't found a single program that worked for me. I hope this helps someone.
Comments
I manualy created the table structure in sqlite database.
Than I uploaded the data with teh following command:
mysqldump -u root {database} {table} --no-create-info --skip-extended-insert --complete-insert --skip-add-locks --compatible=ansi | sed "s/\\\'/''/g" |sqlite3 flora.db
I had to use sed to fix a different apex encoding in the two databases
1 Comment
Personally I like the simple usage of mysqldump, yet some adjustments are need (depending on your art with Unix and what you want to do).
Ex. for just one table (prods) with PK:
$ mysqldump mysql prods -u ME -pPASS --compatible ansi --compact |grep -v "^\/\*" |sqlite3 testme2.db
$ mysqldump mysql prods -u ME -pPASS --compatible ansi --compact |grep -v "^\/\*" |sqlite3 testme2.db
Error: near line 1: table "prods" already exists
Error: near line 7: UNIQUE constraint failed: prods.id, prods.ts
$ sqlite3 testme2.db '.schema'
CREATE TABLE "prods" (
"id" varchar(30) NOT NULL DEFAULT '',
"ts" int(11) NOT NULL DEFAULT '0',
"val" double DEFAULT NULL,
PRIMARY KEY ("id","ts")
);
For more complex things, probably better to write a wrapper, or then, use the already mentioned fantastic awk Linux shell script on Gist .
Comments
The following Website has an online tool to convert Convert MySQL to SQLite online https://www.rebasedata.com/convert-mysql-to-sqlite-online
Comments
There is an alternative method: use the python script in the link convert the mysql dump to csv, and then use tools like DB Browser for SQLite or your own script to import the csv to a SQLite database.
Comments
There is a fantastic, lightweight tool called SQLite Database Browser that allows you to create and edit sqlite databases. I used it to craete databases for Android apps. You can run SQL statements against it to load up the data so if you export the data from a mySQL database you can just import it using this tool. Here's a link: http://sqlitebrowser.sourceforge.net/
7 Comments
export the data with
mysqldump database > database.sql
and import the data with
sqlite3 database < database.sql
you may need -u (user) and -p (password) options