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


本文摘自PHP中文网,作者小云云,侵删。

本文主要介绍了sqlite迁移到mysql脚本的方法,需要的朋友可以参考下,希望能帮助到大家。

废话不多说了,直接给大家贴代码了,具体代码如下所示:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

#! /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";

相关推荐:

阅读剩余部分

相关阅读 >>

mysql如何设置默认值

详解mysql数据库中文乱码问题

mysql中锁的必要性及分类介绍

图文详解mysql5.7安装教程

详解分析mysql8.0的内存消耗

navicat for mysql过期怎么破解

mysql如何改表的字符集

mysql常见索引类型有哪些

怎么删mysql的注册表?

mysql中关于alter命令的使用详解

更多相关阅读请进入《Sqlite》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...