完成从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();

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • CREATE TABLE IF NOT EXISTS ecs_order_info (order_id mediu...
    cookie口阅读 15,778评论 0 16
  • 沈威读书打卡第60天,今天我读了抹不去的生日会的作文,文章讲述了小主人学轮滑-轮滑的发展史-开始总摔跤-克服苦难,...
    四一沈威阅读 169评论 0 0
  • 咳嗽本身是孩子身体应对致病菌的一种自我保护机制。咳嗽了十几天,说明孩子体质不足够的强,没有很快的结束战争。发展成支...
    桔子花_1ded阅读 223评论 0 1
  • 前天晚上,三哥打来电话说他去四舅家了。在舅家说起话来,四舅说他前一段在城里住了二十多天院。 我心里纳闷,三哥这时节...
    清水一滴阅读 2,167评论 5 19
  • 等到快要失去,才懂分外珍惜。 最后的一段时间,通常也是一个高效的时间。 在大学考试的前一天晚上,在工...
    大卫飞思阅读 159评论 2 0