SQLite to MySQL: Database migration process

SQLite and MySQL are the two giants of the database management world. Both are Relational Database Management Systems and both use Structured Query Language or SQL to set up databases. SQLite, as the name indicates, is a lighter platform that can handle simple and smaller projects. On the other hand, MySQL is meant for businesses that handle long-term projects with huge amounts of data.

Both platforms are being used widely in the world and both have their own use cases. Depending on the use case, a business might need to migrate to MySQL from SQLite because the latter doesn’t prove useful when your business is constantly growing. SQLite’s inability to handle larger projects makes it necessary to migrate databases to MySQL which is built for this purpose.

SQLite is a simple database management platform that doesn’t utilize servers and runs on its own. The light nature of SQLite makes it very easy to migrate the database to MySQL. An SQLite database is composed of the simplest objects and only requires sufficient time and expertise to be migrated. The task is not hard but it is not short of technicalities. 

SQLite and MySQL are different from each other in the following ways.

  • The INSERT INTO method for escaping strings clauses in the two platforms
  • For the Boolean data type, MySQL utilizes 1 and 0 while SQLite utilizes t and f.

SQLite is a serverless self-sustained platform while MySQL runs on servers. The migration from SQLite to MySQL can be done using different methods. Let’s start with the easiest one.

So, here it goes.

  • First of all, install SQLite3 
  • Dump the SQLite database using the following statement or command

$ echo “.dump archive” | sqlite3 dbtest.sdb > dbtest.sql

  • Create a database in the MySQL platform using the following statement

$ echo “CREATE DATABASE dbtest ” | mysql -u root -p

  • Now, import the dump file created above to the MySQL database

$ sed -e ‘/PRAGMA/d’ -e’s/BEGIN/START/’ -e ‘s/”archive”/archive/’ < dbtest.sql | mysql -u root -p –database=dbtest

The above-given method is only for the SQLite archive consisting of a single table. Because the DDL and INSERT statements are different in SQLite and MySQL, this method will not work for the migration of large or complex SQLite databases.

If the dump method discussed above is not working for you, you can use Python or Perl scripts for easy and automated conversion. Both of these scripts are perfectly capable of converting SQLite databases that have considerable differences with the MySQL standards.

Here is an example of Perl scripts for database conversion.

#! /usr/bin/perl

while ($line = <>){

    if (($line !~ /BEGIN TRANSACTION/) && ($line !~ /COMMIT/) && ($line !~ /sqlite_sequence/) && ($line !~ /CREATE UNIQUE INDEX/)){

        if ($line =~ /CREATE TABLE \”([a-z_]*)\”(.*)/){

            $name = $1;

            $sub = $2;

            $sub =~ s/\”//g;

            $line = “DROP TABLE IF EXISTS $name;\nCREATE TABLE IF NOT EXISTS $name$sub\n”;

        }

        elsif ($line =~ /INSERT INTO \”([a-z_]*)\”(.*)/){

            $line = “INSERT INTO $1$2\n”;

            $line =~ s/\”/\\\”/g;

            $line =~ s/\”/\’/g;

        }else{

            $line =~ s/\’\’/\\\’/g;

        }

        $line =~ s/([^\\’])\’t\'(.)/$1THIS_IS_TRUE$2/g;

        $line =~ s/THIS_IS_TRUE/1/g;

        $line =~ s/([^\\’])\’f\'(.)/$1THIS_IS_FALSE$2/g;

        $line =~ s/THIS_IS_FALSE/0/g;

        $line =~ s/AUTOINCREMENT/AUTO_INCREMENT/g;

        print $line;

    }

}

Considering all the factors, the process of SQLite to MySQL conversion is not as easy as it may look. The above methods are prone to failures and a minor mistake in processing can lead to database losses. Here is where the SQLite to MySQL converter developed by Intelligent Converters comes in handy. The software automates the process of conversion and eradicates any risk of losing values, columns, tables, or the whole database. At the time of conversion, it runs a deep relational scan and finds common factors and updates the databases accordingly. You can even transform the data according to your preferences at the time of migration. 

Richard

Richard