这篇文章主要介绍了sqlite迁移到mysql脚本的方法,需要的朋友可以参考下
废话不多说了,直接给大家贴代码了,具体代码如下所示:
#! /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;
";
#print "SET AUTOCOMMIT=0;
";
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;
CREATE TABLE $name$sub
"; 
   }
   elsif ($line =~ /CREATE VIEW ([a-z_0-9]*)(.*)/){
   $name = "`$1`";
   $sub = $2;
   $line = "DROP VIEW IF EXISTS $name;
CREATE VIEW $name$sub
";
   }
   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.";
".$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;
";总结
以上就是sqlite如何迁移到MySQL脚本的实例介绍的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号