完成从Mysql到SQLite数据库的整体迁移

1.服务器端创建Mysql数据库的时候要讲究 1, 不要加注记(因为我的java程序没有去处理,注记也叫注视) 2、编码为UTF-8

2.利用工具生成.sql文件.需要更改的地方有:

                     

1.      SET FOREIGN_KEY_CHECKS=0;  该句需要删除

2.      .sql文件定义主键是:

CREATE TABLE `dishes` (

  `id` int(11) NOT NULL AUTO_INCREMENT,….. ,PRIMARY KEY (`id`));

在SQLite中只要写int(11) PRIMARY KEY NOT NULL 即可实现autoincrement

3.      ENGINE=InnoDB AUTO_INCREMENT=111 DEFAULT CHARSET=utf8 这句话不能要

4.      COMMENT (注记) 之后的内容,包括它都不能要

举个例子吧

/*

MySQL Data Transfer

Source Host: 192.168.1.151

Source Database: test

Target Host: 192.168.1.151

Target Database: test

Date: 2011-8-31 18:00:18

*/

SET FOREIGN_KEY_CHECKS=0;

— —————————-

— Table structure for dishes

— —————————-

DROP TABLE IF EXISTS `dishes`;

CREATE TABLE `dishes` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `d_enname` varchar(255) NOT NULL,

  `d_name` varchar(255) NOT NULL,

  `d_price` float NOT NULL,

  `d_image` varchar(255) DEFAULT NULL,

  `d_taste` varchar(255) NOT NULL,

  `d_cook` varchar(255) NOT NULL,

  `d_spiciness` int(11) NOT NULL DEFAULT ‘0’,

  `d_calories` int(11) NOT NULL DEFAULT ‘0’,

  `d_time` int(11) NOT NULL DEFAULT ‘0’,

  `mc_id` int(11) NOT NULL,

  `mt_id` int(11) NOT NULL,

  `mit_id` int(11) NOT NULL,

  `d_ingredients` varchar(255) NOT NULL,

  `r_id` int(11) DEFAULT ‘0’,

  `version_id` int(11) DEFAULT ‘0’,

  `d_content` varchar(255) DEFAULT NULL,

  `discount_price` float DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=111 DEFAULT CHARSET=utf8;

红色的部分,包括倒数第三行那个”,” 都需要删掉或者修改的。改动以后的结果是:

/*

MySQL Data Transfer

Source Host: 192.168.1.151

Source Database: test

Target Host: 192.168.1.151

Target Database: test

Date: 2011-8-31 18:00:18

*/

— —————————-

— Table structure for dishes

— —————————-

DROP TABLE IF EXISTS `dishes`;

CREATE TABLE `dishes` (

  `id` int(11) PRIMARY KEY NOT NULL,

  `d_enname` varchar(255) NOT NULL,

  `d_name` varchar(255) NOT NULL,

  `d_price` float NOT NULL,

  `d_image` varchar(255) DEFAULT NULL,

  `d_taste` varchar(255) NOT NULL,

  `d_cook` varchar(255) NOT NULL,

  `d_spiciness` int(11) NOT NULL DEFAULT ‘0’,

  `d_calories` int(11) NOT NULL DEFAULT ‘0’,

  `d_time` int(11) NOT NULL DEFAULT ‘0’,

  `mc_id` int(11) NOT NULL,

  `mt_id` int(11) NOT NULL,

  `mit_id` int(11) NOT NULL,

  `d_ingredients` varchar(255) NOT NULL,

  `r_id` int(11) DEFAULT ‘0’,

  `version_id` int(11) DEFAULT ‘0’,

  `d_content` varchar(255) DEFAULT NULL,

  `discount_price` float DEFAULT NULL);

这样的.sql文件可以使用SQLite Expert 直接导入sql文件(前提是你已经自己创建了一个数据库).

OK,那些表已经完全迁移过来了。

另外,附着我写的关于自动修改该.sql文件代码如下,(注意源文件要改名为test.sql,目标文件为result.sql,放在E盘下,当然自己可以手动去改)

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.InputStreamReader;

public class ConvertScript {

/**

* @param args

*/

public static void main(String[] args) {

try {

read(“E:/test.sql”,”E:/result.sql”);

} catch (Exception e) {

e.printStackTrace();

}

}

public static void read(String sourcePath,String destionPath) throws Exception {

InputStreamReader read = new InputStreamReader (new FileInputStream(new File(sourcePath)), “UTF-8”);

FileWriter fw = new FileWriter(new File(destionPath));

BufferedReader reader = new BufferedReader(read);

BufferedWriter writer = new BufferedWriter(fw);

String s = null;

StringBuilder sb = new StringBuilder();

while((s = reader.readLine()) != null) {

String replace = null;

String result = null;

if(s.contains(“SET FOREIGN_KEY_CHECKS=0;”)) {

replace = “SET FOREIGN_KEY_CHECKS=0;”;

result = “\r\n”;

} else if(s.contains(“NOT NULL AUTO_INCREMENT”)) {

replace = “NOT NULL AUTO_INCREMENT”;

result = “PRIMARY KEY NOT NULL”;

} else if(s.contains(“PRIMARY KEY (`id`)”)) {

replace = “”;

} else if(s.contains(“ENGINE=InnoDB AUTO_INCREMENT”) || s.contains(“) ENGINE=InnoDB DEFAULT CHARSET=utf8”)) {

s = “);”;

}

if(s.equals(“);”)) {

String resultStr = sb.toString();

sb = new StringBuilder();

sb.append(resultStr.substring(0, resultStr.lastIndexOf(“,”)));

}

if(replace != null && result != null)

s = s.replace(replace, result);

if(!””.equals(replace)) {

sb.append(s).append(“\r\n”);

}

}

System.out.println(sb.toString());

writer.write(sb.toString());

reader.close();

writer.close();

}

}

    原文作者:littlewoodman
    原文地址: https://www.jianshu.com/p/8d79f986efa8
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞