138

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

1

15 Answers 15

97

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

7
  • 13
    Works as charm! Here is a bit of documentation: $ ./mysql2sqlite.sh -h host -u MyUserName -pMySecretPassWord myDbase | sqlite3 database.sqlite gist.github.com/943776
    – kachar
    Oct 30, 2012 at 12:58
  • 1
    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 Oct 27, 2015 at 4:47
  • 3
    It's worth noting that this script has been superseded by a successor maintained here: github.com/dumblob/mysql2sqlite
    – drzax
    Jul 20, 2016 at 8:10
  • Even better to play with non-ascii characters: $./mysql2sqlite.sh --default-character-set=utf8 -hHost -uUser -pPass db | sqlite3 db.sqlite gist.github.com/esperlu/943776#gistcomment-984448
    – Sun Junwen
    Aug 6, 2019 at 4:43
  • 1
    Worked for me using $./mysql2sqlite.sh -u MyUserName -p MyPassword --column-statistics=0 --default-character-set=utf8 MyDatabase | sqlite3 database.sqlite
    – Imjohsep
    Jan 28, 2021 at 17:52
57

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.
1
  • 1
    That would be extremely slow compared to piping through a script. Sep 30, 2021 at 17:39
52

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
15
  • 3
    Don't forget gem install mysql and gem install sqlite3
    – Nick
    Feb 18, 2014 at 3:54
  • 2
    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, 2014 at 19:16
  • 4
    replace mysql with mysql2 if you are using mysql2
    – Carlosin
    Jun 9, 2014 at 7:58
  • 1
    Note: You'll need the ruby-dev package to build these native gem packages.
    – Panda
    Jul 16, 2014 at 7: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, 2014 at 18:24
23

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.
7
  • 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, 2019 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. Apr 15, 2019 at 11:02
  • @arkadianriver I suggest you always use virtual environments with Python 😎 May 9, 2019 at 7: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 1, 2020 at 17:40
  • Follow up to my above comment: it also appears that the defaulted values for columns are not exported.
    – Josh Green
    Oct 1, 2020 at 21:02
18

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.

1
14

Sqlite has officially list of converting tools.

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

12

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

0
9

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
6

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.

2
  • 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, 2017 at 9:53
  • 2
    But what "extra lines" though ? You could have incompatible data types all over the file.
    – elig
    Mar 22, 2021 at 2:56
3

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.

1
  • 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, 2018 at 14:39
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.

3
  • How can I copy only one table from the DB not entire mysql database ? Apr 10, 2016 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, 2016 at 23:18
  • It seems to be working. Nonetheless, I cannot find where is the output file. Can you help me?
    – Pontios
    Nov 2, 2018 at 16:51
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.

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.)

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

1
  • 2023 : this is the best solution, just install sudo apt install kexi then install the driver sudo apt install kexi-mysql-driver (and even postgresql), and open it, and bring it from the server, open it as a file in SQLite (and remove the extra tables created by Kexi) Jan 29 at 10:55
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.