Sqlite China  
首页 | 各种语言的sqlite编程 | sqlite研究 |sqlite应用实例与杂谈 | sqlite相关下载 | SQlite论坛
当前位置 : 主页>sqlite研究>列表
MySQL 到 SQLite 的转换
来源:工友 作者:工友 时间:2007-12-21

下面是Perl的程序代码:

#!/usr/bin/perl -w
use strict;

use DBI;
use Getopt::Std;

use vars qw(%opts);
getopts('u:s:d:h',\%opts);
my ($user,$host,$db,$help) = parse_args(\%opts);

USAGE() and exit unless $user and $host and $db and not $help;

my $table = join(' ',@ARGV);
open(DUMP, "mysqldump -u $user -p -h $host $db $table |");
my $sql = do {local $/; };

$sql =~ s/^#.*$//mg; # chokes on comments
$sql =~ s/auto_increment//g; # on 'auto_increment'
$sql =~ s/TYPE=\w+;/;/g; # and on 'TYPE=____'
$sql =~ s/\\'/''/g; # and on escaped '

my @table = $sql =~ /CREATE\s+TABLE\s+(\w+)/g;
print "creating tables: ",join(' ',@table),"\n";

my $dbh = DBI->connect(
("DBI:SQLite:dbname=$db.dbm"),
{RaiseError=>1}
);

$dbh->do($sql);

sub parse_args {
my %opt = %{+shift};
return @opt{qw(u s d h)};
}

sub USAGE {print "USAGE: $0 -u user -s server(host) -d database\n"}

=pod

=head1 NAME

mysql2sqlite.pl - MySQL database migration script

=head1 DESCRIPTION

This is a simple Perl DBI script for use with the MySQL
and SQLite database drivers. The script opens a pipe to
the mysqldump program to retrieve CREATE and INSERT
statements for the specified tables. This data is then
munged to conform with SQLite, and then fed to a dbm
file named the same as the database.

=head1 SYNOPSIS

./mysql2sqlite.pl -u user -s host -d dbase table1 table2 table3

This will create a dbm named 'dbase.dbm' with three tables
(table1, table2, table3) provided that they all exist in
the MySQL database. If tables are not supplied, then ALL
TABLES in the database will be migrated. If a table already
exists in the dbm file, then the script will stop execution
before that table's data is migrated (simplicity vs.
robustness, i chose simplicity).

=head1 LEGAL STUFF

Mi casa su casa, but if you get hurt or someone gets hurt
from this casa, then it's your casa, not mine.

=cut
 
对于如何处理Mysql中的ENUM/SET类型,则可以参考下面的代码:
sub JeffasMysql2SQLite {
my @Ssql = split /\n/,shift(@_);
my %index;
my @tablename;

for my $sql( @Ssql ){
push @tablename, $1 if $sql =~ m/create \s+ table \s+ (\w+) /i
+x;

$sql =~ s/^#.*$//mg; # chokes on comments
$sql =~ s{^\s*?(KEY(?:\s*\w+\s*)?\(.*)}{
push @{$index{$tablename[-1]}},$1;
"";
}giemx;
$sql =~ s/auto_increment//ig; # on 'auto_increment'
$sql =~ s/UNSIGNED//ig;
$sql =~ s/TYPE=\w+;/;/gi; # and on 'TYPE=____'
$sql =~ s/\\'/''/g; # and on escaped '

$sql =~ s{^\s*?(\w+)\s+(?:SET|ENUM)\((.*)$}{ SetOrEnumToVarcha
+r($1,$2); }exig;
}

my $ret = join "\n",@Ssql;

for my $tablename( @tablename ){

for my $ix( @{ $index{$tablename} } ){
$ix=~ s/[,\s]+$//;

if( $ix =~ /\bKEY\((\w+)\)/i ) {
$ret.=" CREATE INDEX $1 on $tablename ($1);\n";
} elsif( $ix =~ /\bKEY\s+(\w+)\s+\((.*?)\z/i ) {
$ret.=" CREATE INDEX $1 on $tablename ($2;\n";
}
}
}

return $ret;
}

sub SetOrEnumToVarchar {
my( $name, $val ) = @_;
my $end = substr $val, rindex($val,')') + 1;
$val = substr $val, 0, rindex($val,')') - 1;
my $q = substr $val, 0, 1, "";
( $val ) = sort {
$b <=> $a
} map {
s/^\"//;
s/\"\$//;
length $_;
} split /$q,$q/, $val;

#warn "\n\t\tname $name\n\t\t val $val\n\t\t end $end\n\t\t q $q\n\t
+\t";
return "$name VARCHAR($val) $end";
}
(阅读次数:
上一篇:Sqlite虚拟机VDBE原理 下一篇:SQLite 与 PHP 结合开发(PPT)
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
§最新评论
热点文章
·SQLite数据库的体系结构
·SQLite与其他数据库的速度
·SQL 语言参考资料
·SQLite语法备忘录
·sqlite 的相关调查1
·嵌入式数据库SQLite的一份
·SQLite在嵌入式Web服务器
·点评主流开源数据库的技术
·基于ARM-Linux的SQLite嵌
·SQLite与其他数据库的速度
·SQLite数据库编程--创建数
·SQL 语法手册
·SQLite Mode 数据库交互的
·SQLITE3 使用总结(3)
·XXTEA加密算法为SQLite 3.
·SQLite 第三版总览(简介)
·SQLite 第三版中的数据类
·用sqlite 执行标准 sql 语
·System.Data.Sqlite 上手
·SQLite编译安装步骤
相关文章
·SQLite Mode 数据库交互的
·SQL 语言参考资料
·SQLite在嵌入式Web服务器
·SQL 语法手册
·System.Data.Sqlite 上手
·SQLite数据库编程--创建数
·SQLite数据库编程--数据库
·SQLite在TorqueScript中的
·关于sqlite_exec回调函数
·用sqlite 执行标准 sql 语

版权Power by DedeCms   后台登陆
Copyright @ 2007