sqlite遷移到mysql腳本的方法
更新時間:2017年08月07日 16:47:27 投稿:mrr
這篇文章主要介紹了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/\'\'/\\\'/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";
總結(jié)
以上所述是小編給大家介紹的sqlite遷移到mysql腳本的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
mysql 實現(xiàn)遷移數(shù)據(jù)庫到另一臺服務(wù)器
這篇文章主要介紹了mysql 實現(xiàn)遷移數(shù)據(jù)庫到另一臺服務(wù)器中,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
mysql?子查詢的概述和分類及單行子查詢功能實現(xiàn)
本文詳細介紹了MySQL的子查詢概念和應(yīng)用,解釋了子查詢是在主查詢中嵌套另一個查詢,包括外查詢和內(nèi)查詢,并從多個角度進行分類,文章還深入探討了子查詢的編寫技巧和使用場景,對于學(xué)習(xí)和應(yīng)用MySQL的人來說,這是一篇非常有價值的指南2024-10-10
MySQL數(shù)據(jù)遷移相關(guān)總結(jié)
這篇文章主要介紹了MySQL數(shù)據(jù)遷移的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用MySQL數(shù)據(jù)庫,感興趣的朋友可以了解下2021-04-04

