"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Convert a MySQL Dump SQL File to a SQLite3 Database?

How to Convert a MySQL Dump SQL File to a SQLite3 Database?

Published on 2024-11-08
Browse:432

How to Convert a MySQL Dump SQL File to a SQLite3 Database?

Converting MySQL Dump SQL to Sqlite3 DB

Importing a MySQL dump SQL file into a SQLite3 database requires conversion to ensure compatibility.

The provided shell script offers an automated way to convert the dump file:

#!/bin/sh
# Usage: $0 

if [ "x$1" == "x" ]; then
   echo "Usage: $0 "
   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' |
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
    rm tmp
else
   echo "There were errors during conversion.  Please review $1.err and $1.sql for details."
fi
; then echo "Usage: $0 " 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' | 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 rm tmp else echo "There were errors during conversion. Please review $1.err and $1.sql for details." fi

The script performs the following transformations:
  • Removes redundant key definitions.
  • Sets fields to unsigned if applicable.
  • Sets auto_increment fields to primary key autoincrement.
  • Converts integer types to their equivalents in SQLite (smallint, tinyint, and int become integer).
  • Removes character sets.
  • Converts enum types to varchar(255).
  • Removes set references to update triggers.
  • Converts multiple row inserts to individual inserts for SQLite compatibility.
  • Wraps the converted SQL in a transaction for safety.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3