How to migrate sqlite to mysql script

How to migrate sqlite to mysql script

Without further ado, I will post the code for you directly. The specific code is as follows:

#!/usr/bin/perl
#
# based on https://stackoverflow.com/a/87531/5742651
# usage: sqlite3 .dump database_name.sqlite3 | perl sqlite2mysql.pl | mysql -u root -p $import_database_name
#
# ignore follow lines:
# BEGIN TRANSACTION 
# COMMIT 
#sqlite_sequence 
# CREATE UNIQUE INDEX
# PRAGMA foreign_keys=OFF
# "tablename/field" => `tablename/field`
# booleans 't' and 'f' => 1 and 0
# AUTOINCREMENT => AUTO_INCREMENT
# varchar => varchar(255)
# CREATE TABLE table... => DROP TABLE table; CREATE TABLE table...
# Merge insert sqls into multiple insert to speed up
# INSERT INTO table VALUES('val1');
# INSERT INTO table VALUES('val2'); => INSERT INTO table VALUES('val1'), ('val2'), ('val3');
# INSERT INTO table VALUES('val3');
my $open=0;
my $line_cache = '';
# For speed up
print "SET GLOBAL max_allowed_packet=209715200;\n";
#print "SET AUTOCOMMIT=0;\n";
while ($line = <>){
  if (($line !~ /PRAGMA foreign_keys=OFF/) && ($line !~ /BEGIN TRANSACTION/) && ($line !~ /COMMIT/) && ($line !~ /sqlite_sequence/) && ($line !~ /CREATE UNIQUE INDEX/)){
   if ($line =~ /CREATE TABLE \"([a-z_0-9]*)\"(.*)/){
   $name = "\`$1\`";
   $sub = $2;
   $sub =~ s/varchar([^(])/varchar(255)$1/g;
   $line = "DROP TABLE IF EXISTS $name;\nCREATE TABLE $name$sub\n"; 
   }
   elsif ($line =~ /CREATE VIEW ([a-z_0-9]*)(.*)/){
   $name = "\`$1\`";
   $sub = $2;
   $line = "DROP VIEW IF EXISTS $name;\nCREATE VIEW $name$sub\n";
   }
   elsif ($line =~ /INSERT INTO \"([a-z_]*)\" VALUES(.*);/){
        if ($open == 0) {
          $open = 1;
       $line_cache .= "INSERT INTO \`$1\` VALUES $2";
        } else {
          $line_cache .= ", $2";
        }
        next;
   }else{
   $line =~ s/\'\'/\\\'/g;
   }
    if ($open == 1) {
       $open = 0;
       $line = $line_cache.";\n".$line;
       $line_cache = '';
    }
   $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;
  }
}
#print "SET AUTOCOMMIT=1;\n";

Summarize

The above is the method of migrating sqlite to mysql script introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Detailed explanation of how to migrate a MySQL database to another machine
  • MySQL database migration quickly exports and imports large amounts of data
  • MySQL data migration using MySQLdump command
  • The correct way to migrate MySQL data to Oracle
  • MySQL database migration example by copying data files
  • Migrate mysql database to Oracle database
  • Detailed explanation of mysql5.5 database data directory migration method
  • Detailed steps for migrating the data folder of the MySQL database
  • Analysis of Mysql data migration methods and tools

<<:  How to implement Svelte's Defer Transition in Vue

>>:  Steps to create your own YUM repository

Recommend

How to implement vertical text alignment with CSS (Summary)

The default arrangement of text in HTML is horizo...

Nginx rewrite regular matching rewriting method example

Nginx's rewrite function supports regular mat...

Analysis of pitfalls in rounding operation of ROUND function in MySQL

This article uses examples to illustrate the pitf...

React encapsulates the global bullet box method

This article example shares the specific code of ...

Detailed explanation of nginx's default_server definition and matching rules

The default_server directive of nginx can define ...

How to configure MySQL master-slave replication under Windows

MySQL master-slave replication allows data from o...

Detailed explanation of how to use the vue3 Teleport instant movement function

The use of vue3 Teleport instant movement functio...

JavaScript using Ckeditor + Ckfinder file upload case detailed explanation

Table of contents 1. Preparation 2. Decompression...

The phenomenon of margin-top collapse and the specific solution

What is margin-top collapse Margin-top collapse i...

Four ways to modify the default CSS style of element-ui components in Vue

Table of contents Preface 1. Use global unified o...

The difference between distinct and group by in MySQL

Simply put, distinct is used to remove duplicates...

HTML+VUE paging to achieve cool IoT large screen function

Effect demo.html <html> <head> <me...

How to use Vue-router routing

Table of contents 1. Description 2. Installation ...

The latest Linux installation process of tomcat8

Download https://tomcat.apache.org/download-80.cg...