126

Is it possible to convert from MySQL to SQLite with a free tool on windows?

mullek
  • 1,941
  • 4
  • 16
  • 12

15 Answers15

86

There is a mysql2sqlite.sh script on GitHub

As described in the header, the script can be used like this:

./mysql2sqlite.sh myDbase | sqlite3 database.sqlite

alternatives

David LeBauer
  • 28,793
  • 27
  • 106
  • 180
  • 10
    Works as charm! Here is a bit of documentation: `$ ./mysql2sqlite.sh -h host -u MyUserName -pMySecretPassWord myDbase | sqlite3 database.sqlite` https://gist.github.com/943776 – kachar Oct 30 '12 at 12:58
  • The script just pulls arguments from the command line and passes them to mysqldump, so you can find out about configuring custom ports, etc with `man mysql` or `man mysqldump` – Devin Howard Oct 27 '15 at 04:47
  • 2
    It's worth noting that this script has been superseded by a successor maintained here: https://github.com/dumblob/mysql2sqlite – drzax Jul 20 '16 at 08:10
  • Even better to play with non-ascii characters: $./mysql2sqlite.sh --default-character-set=utf8 -hHost -uUser -pPass db | sqlite3 db.sqlite https://gist.github.com/esperlu/943776#gistcomment-984448 – Sun Junwen Aug 06 '19 at 04:43
  • Worked for me using `$./mysql2sqlite.sh -u MyUserName -p MyPassword --column-statistics=0 --default-character-set=utf8 MyDatabase | sqlite3 database.sqlite` – Imjohsep Jan 28 '21 at 17:52
  • When I try to execute the script it says "Permission denied". `sudo`doesn't work either. – theerrormagnet Apr 25 '21 at 08:06
54

Here is a list of converters. (snapshot at archive.today)


An alternative method that would work even on windows but is rarely mentioned is: use an ORM class that abstracts specific database differences away for you. e.g. you get these in PHP (RedBean), Python (Django's ORM layer, Storm, SqlAlchemy), Ruby on Rails (ActiveRecord), Cocoa (CoreData) etc.

i.e. you could do this:

  1. Load data from source database using the ORM class.
  2. Store data in memory or serialize to disk.
  3. Store data into destination database using the ORM class.
David d C e Freitas
  • 7,146
  • 4
  • 54
  • 66
47

Sequel (Ruby ORM) has a command line tool for dealing with databases, you must have ruby installed, then:

  $ gem install sequel mysql2 sqlite3 
  $ sequel mysql2://user:password@host/database -C sqlite://db.sqlite
Macario
  • 2,097
  • 2
  • 20
  • 39
  • 2
    Don't forget `gem install mysql` and `gem install sqlite3` – Nick Feb 18 '14 at 03:54
  • 1
    and `gem install pg` if you want to convert from or to a postgres db, sequel should have adapters for major db's – Macario Feb 27 '14 at 19:16
  • 2
    replace `mysql` with `mysql2` if you are using `mysql2` – Carlosin Jun 09 '14 at 07:58
  • 1
    Note: You'll need the `ruby-dev` package to build these native gem packages. – Panda Jul 16 '14 at 07:49
  • 1
    Had to use "gem install sequel mysql2 sqlite3" and then "sequel mysql2://user:password@host/database -C sqlite://db.sqlite" PERFECT – unom Aug 11 '14 at 18:24
  • I then dug up my ruby skills and tried the answer below this one, but had to use mysql2 instead of mysql and this, "gem install sequel mysql2 sqlite3" then "sequel mysql2://user:password@host/database -C sqlite://db.sqlite" finally produced some meaningful errors. Seems I had some illegal dates in some fields, like 0004-00-00 and I had to correct those. Installed Firefox with the SQLite Manager add-on and went in and edited about 10 fields that had this problem. Reran the command and DONE! Finally. – unom Aug 12 '14 at 09:42
  • how do I know if I should be using mysql or mysql2? – Jürgen Paul Oct 23 '14 at 03:03
  • In ubuntu 14.04 works fine, using mysql2 and ruby-dev – e-info128 Dec 22 '14 at 17:26
  • You may need to quote the arguments if your password has shell-unsafe characters, such as a semi-colon. – kainjow Apr 09 '15 at 17:08
  • mysql - `Error: Sequel::DatabaseError: SQLite3::SQLException: unrecognized token: "'"` mysql2 - `Error: Sequel::DatabaseError: SQLite3::SQLException: unrecognized token: "'����"` – stephen Jun 15 '15 at 08:36
  • Is there an attirbute to control the letter encoding? – teawithfruit Sep 16 '15 at 11:03
  • @teawithfruit I am not sure, you can ask Jeremy Evans (https://github.com/jeremyevans), the maintainer of sequel ORM. I've heard he is very committed to the project and to help others with any issues related to it. There is an IRC channel too. http://sequel.jeremyevans.net/development.html – Macario Sep 18 '15 at 01:47
  • I've built a docker container for the sequel one liner database conversion https://blog.chib.me/how-to-convert-databases-with-one-line-of-code/ – web-chib Feb 07 '16 at 13:06
  • Note: You will need `libsqlite3-dev` package to install `sqlite3` gem – Tushar Goswami Feb 13 '16 at 07:35
19

Not every DB schema can be converted. MySQL is more complex and feature-rich than SQLite. However, if your schema is simple enough, you could dump it into an SQL file and try to import it / load it into an SQLite DB.

Assaf Lavie
  • 63,560
  • 33
  • 139
  • 197
  • After dumping your MySQL database to a file, you can use this script to convert it to SQLite https://github.com/dumblob/mysql2sqlite (as mentioned in @David_LeBauer 's answer). – Paul Rougieux Jan 22 '20 at 13:34
13

I faced the same problem about 2 days ago when I had to convert a 20GB+ MySQL database to SQLite. It was by no means an easy task and I ended up writing this Python package that does the job.

The upside of it being written in Python is that it's cross platform (unlike a shell/bash script) and can all be easily installed using pip install (even on Windows). It uses generators and chunking of the data being processed and is therefore very memory efficient.

I also put in some effort to correctly translate most of the datatypes from MySQL to SQLite.

The tool is also thoroughly tested and works on Python 2.7 and 3.5+.

It is invokable via command line but can also be used as a standard Python class which you can include in some larger Python orchestration.

Here's how you use it:

Usage: mysql2sqlite [OPTIONS]

Options:
  -f, --sqlite-file PATH     SQLite3 database file  [required]
  -d, --mysql-database TEXT  MySQL database name  [required]
  -u, --mysql-user TEXT      MySQL user  [required]
  -p, --mysql-password TEXT  MySQL password
  -h, --mysql-host TEXT      MySQL host. Defaults to localhost.
  -P, --mysql-port INTEGER   MySQL port. Defaults to 3306.
  -c, --chunk INTEGER        Chunk reading/writing SQL records
  -l, --log-file PATH        Log file
  -V, --vacuum               Use the VACUUM command to rebuild the SQLite
                             database file, repacking it into a minimal amount
                             of disk space
  --use-buffered-cursors     Use MySQLCursorBuffered for reading the MySQL
                             database. This can be useful in situations where
                             multiple queries, with small result sets, need to
                             be combined or computed with each other.
  --help                     Show this message and exit.
Klemen Tusar
  • 7,916
  • 4
  • 26
  • 26
  • 2
    Hey @techouse, this looks great! Is there any way it could work with a `.sql` dump file or does it require connection to an installed MySQL database? – vortek Apr 14 '19 at 22:44
  • Hey! Ummm, no this *requires* a working MySQL database. If you have a dump without an actual server your best bet is to change the file manually *if* you know what you're doing. – Klemen Tusar Apr 15 '19 at 11:02
  • @arkadianriver I suggest you always use virtual environments with Python – Klemen Tusar May 09 '19 at 07:13
  • This tool was pretty awesome; great job. I exported this to a .sql dump file by running `sqlite3 db.sql`, then executed the following commands to have sqlite generate the dump file: `.output init.sql` `.dump` `.exit` -- I will note the only unfortunate thing so far is that views weren't exported correctly, but that's not a huge deal. – Josh Green Oct 01 '20 at 17:40
  • Follow up to my above comment: it also appears that the defaulted values for columns are not exported. – Josh Green Oct 01 '20 at 21:02
9

Sqlite has officially list of converting tools.

http://www2.sqlite.org/cvstrac/wiki?p=ConverterTools

maytham-ɯɐɥʇʎɐɯ
  • 21,551
  • 10
  • 85
  • 103
7

I found the perfect solution

First, you need this script (put it into a file called 'mysql-to-sqlite.sh'):

#!/bin/bash
if [ "x$1" == "x" ]; then
  echo "Usage: $0 <dumpname>"
  exit
fi

cat $1 |
grep -v ' KEY "' |
grep -v ' UNIQUE KEY "' |
grep -v ' PRIMARY KEY ' |
sed '/^SET/d' |
sed 's/ unsigned / /g' |
sed 's/ auto_increment/ primary key autoincrement/g' |
sed 's/ smallint([0-9]*) / integer /g' |
sed 's/ tinyint([0-9]*) / integer /g' |
sed 's/ int([0-9]*) / integer /g' |
sed 's/ character set [^ ]* / /g' |
sed 's/ enum([^)]*) / varchar(255) /g' |
sed 's/ on update [^,]*//g' |
sed 's/\\r\\n/\\n/g' |
sed 's/\\"/"/g' |
perl -e 'local $/;$_=<>;s/,\n\)/\n\)/gs;print "begin;\n";print;print "commit;\n"' |
perl -pe '
if (/^(INSERT.+?)\(/) {
  $a=$1;
  s/\\'\''/'\'\''/g;
  s/\\n/\n/g;
  s/\),\(/\);\n$a\(/g;
}
' > $1.sql
cat $1.sql | sqlite3 $1.db > $1.err
ERRORS=`cat $1.err | wc -l`
if [ $ERRORS == 0 ]; then
  echo "Conversion completed without error. Output file: $1.db"
  rm $1.sql
  rm $1.err
else
  echo "There were errors during conversion.  Please review $1.err and $1.sql for details."
fi

Then, dump a copy of your database:

you@prompt:~$ mysqldump -u root -p --compatible=ansi --skip-opt generator > dumpfile

And now, run the conversion:

you@prompt:~$ mysql-to-sqlite.sh dumpfile

And if all goes well, you should now have a dumpfile.db which can be used via sqlite3.

you@prompt:~$ sqlite3 dumpfile.db 
SQLite version 3.6.10
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
dg_cities                 dg_forms                  dg_surnames             
dg_counties               dg_provinces              dg_user_accounts        
dg_countries              dg_provinces_netherlands
dg_first_names            dg_states
Máté
  • 2,195
  • 3
  • 16
  • 25
Paulo Luan
  • 181
  • 2
  • 8
6

Simplest way to Convert MySql DB to Sqlite:

1) Generate sql dump file for you MySql database.

2) Upload the file to RebaseData online converter here

3) A download button will appear on page to download database in Sqlite format

Sandeep Yohans
  • 726
  • 11
  • 27
4

There are different ways to do this. I also had this problem and I searched a lot and then I got a simple way to convert MySQL to SQLite.

Follow these steps:

  1. First You Need to Install SQLite DB Browser (very small and fast to view Tables and Data)

  2. Open your MySQL File in Notepad or it would be great if you open in Notepad++

  3. Remove First extra Lines Contains Information or Queries and Save it.

  4. Open SQLite DB Browser, Create Database, then Tables, and Same Types as it is in MySQL Database.

  5. In Menu Bar of SQLite DB Browser Select File-> then Import data MySQL File which you saved.

It will easily Convert into SQLite After Warning Dialog.

If error then remove more extra lines if your MySQL file have.

You can also Install MySQL to SQLite Converter Software on trial Basis, but the information I am giving for conversion is life time.

Nimantha
  • 4,731
  • 5
  • 15
  • 38
Uzair Qaiser
  • 136
  • 9
  • This worked fine if your data have no character like apostrophe. For example "This is Peter's book" in sql dumb will be 'This is Peter\'s book'. This will make DB Browser throw error, for a large data it is difficult to remove. But if you import the data from phpAdmin as CSV to DB Browser, that import will be successful. I hope this help – Seunope Dec 19 '17 at 09:53
  • But what "extra lines" though ? You could have incompatible data types all over the file. – elig Mar 22 '21 at 02:56
2

My solution to this issue running a Mac was to

  1. Install Ruby and sequel similar to Macario's answer. I followed this link to help setup Ruby, mysql and sqlite3 Ruby on Rails development setup for Mac OSX
  2. Install sequel

    $ gem install sequel
    

    If still required

    % gem install mysql sqlite3
    

    then used the following based of the Sequel doc bin_sequel.rdoc (see Copy Database)

    sequel -C mysql://myUserName:myPassword@host/databaseName sqlite://myConvertedDatabaseName.sqlite
    

A windows user could install Ruby and Sequel for a windows solution.

Grant Luck
  • 61
  • 3
  • How can I copy only one table from the DB not entire mysql database ? – vaibhavatul47 Apr 10 '16 at 19:21
  • For a single table in the past I have done this, but there may be a better way. Export the table as csv with the column names in the first row option ticked. Then using DB Browser for SQLite, import the table into my existing sqlite database using the import table from csv option. You will then likely have to go in and change the fields to the type you require as I believe all the fields will be set to TEXT. In the project I was working on though we developed a process were we made an api call to return all the data in a table and then inserted/replaced the data in the apps database. – Grant Luck Apr 11 '16 at 23:18
  • It seems to be working. Nonetheless, I cannot find where is the output file. Can you help me? – Pontios Nov 02 '18 at 16:51
2

I tried a number of methods on this thread, but nothing worked for me. So here is a new solution, which I also found to be very simple:

  1. Install RazorSQL. Works for Mac, Windows and Linux.
  2. In RazorSQL connect to your database, on localhost for example. Conversion doesn't work with sql dump files.
  3. Right click on your database -> Database Conversion -> select SQLite. This will save a txt file with all the sqlite queries necessary to create this database.
  4. Install a SQLite database manager, like DB Browser for SQLite. It works on any OS.
  5. Create an empty database, go to Execute SQL tab and paste the content from step 3.

That's it, now you have your SQLite database.

Vali Munteanu
  • 369
  • 1
  • 14
  • RazorSQL seems to just build a "common" *.sql file with table definitions and data insert intos but nothing particularly SQLite specific. It did not export Views which is a big minus. The mysqldump client which comes with MySql exports views too but it also produces lot of conditional stuff etc. what SQLite will not digest. – dalilander Feb 13 '18 at 14:39
1

I like the SQLite2009 Pro Enterprise Manager suggested by Jfly. However:

  • The MySQL datatype INT is not converted to SQlite datatype INTEGER (works with DBeaver )

  • It does not import foreign key constaints from MySQL (I could not find any tool that supports the transfer of foreign key constraints from MySQL to SQlite.)

Stefan
  • 6,227
  • 1
  • 34
  • 75
1

From list of converter tools I found Kexi. It is a UI tool to import from various DB servers (including MySQL) into SQLite. When importing some database (say from MySQL) it stores it in Kexi format. The Kexi format is the 'native' SQLite format. So simply copy the kexi file and have your data in sqlite format

Tushar Goswami
  • 697
  • 1
  • 7
  • 19
1

If you have experience write simple scripts by Perl\Python\etc, and convert MySQL to SQLite. Read data from Mysql and write it on SQLite.

Buhake Sindi
  • 82,658
  • 26
  • 157
  • 220
Eugene
  • 1,541
  • 3
  • 13
  • 29
1

If you have been given a database file and have not installed the correct server (either SQLite or MySQL), try this tool: https://dbconvert.com/sqlite/mysql/ The trial version allows converting the first 50 records of each table, the rest of the data is watermarked. This is a Windows program, and can either dump into a running database server, or can dump output to a .sql file

kurdtpage
  • 2,734
  • 1
  • 21
  • 22