实例讲解sqlite迁移到mysql脚本的方法_

小云云
Lepaskan: 2017-12-25 11:03:34
asal
1665 orang telah melayarinya

本文主要介绍了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;\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/\&#39;\&#39;/\\\&#39;/g;
   }
    if ($open == 1) {
       $open = 0;
       $line = $line_cache.";\n".$line;
       $line_cache = &#39;&#39;;
    }
   $line =~ s/\"/`/g;
   $line =~ s/([^\\&#39;])\&#39;t\&#39;(.)/$1THIS_IS_TRUE$2/g;
   $line =~ s/THIS_IS_TRUE/1/g;
   $line =~ s/([^\\&#39;])\&#39;f\&#39;(.)/$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";
Salin selepas log masuk

相关推荐:

PHP之SQLite数据库类操作与用法实例

Python如何读取sqlite数据库的文件?

php 简单操作SQLite数据库类与用法实例详解

Atas ialah kandungan terperinci 实例讲解sqlite迁移到mysql脚本的方法_. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!