【解决问题】完成从Mysql到SQLite数据库的整体迁移
来源:互联网 发布:校园招聘网络营销策划 编辑:程序博客网 时间:2024/06/11 10:33
我们公司的CTO要求我维护客户端的数据库,并且下达了一个命令。android客户端的DB从最初到最后是与ServerDB保持一致的。
最初的想法是,我遇到的问题网络上应该有的,我就不断的查找,包括英文的网站上只找到一篇。(时间我没记错是2007年) 他也没有什么好的办法。
他的办法经过我加工修改得出,若要实现成功迁移要保证:
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();}}
- 【解决问题】完成从Mysql到SQLite数据库的整体迁移
- seafile从sqlite迁移到mysql
- JIRA数据库的迁移,从HSQL到MYSQL/Oracle
- 从MySQL到Redis的简单数据库迁移方法
- 把数据库从MYSQL迁移到POSTGRESQL
- 数据库迁移之从oracle 到 MySQL
- 数据库迁移之从oracle 到 MySQL
- 数据库迁移之从oracle 到 MySQL
- MySQL数据库从windows迁移到linux
- 【数据库】从mysql迁移到sqlserver
- 数据库迁移之从oracle 到 MySQL
- 数据库迁移-从Oracle到MySQL
- 数据库迁移之从oracle 到 MySQL
- 数据库迁移之从oracle 到 MySQL
- 数据库迁移之从oracle 到 MySQL
- MySQL数据库从windows迁移到linux
- sqlite迁移到mysql脚本
- 通过Ruby完成从MoveableType到.Text的迁移
- Apache 虚拟机简单配置
- SecureCRT软件介绍及linux常用命令
- java中Date与String的相互转化
- MySQL5.5源代码安装
- 各种加密算法的实例
- 【解决问题】完成从Mysql到SQLite数据库的整体迁移
- 中英翻译好句子
- today Bug
- Android上HDMI介绍(基于高通平台)
- 微软面试题 寻找数组中出现的唯一重复的一个数
- label标签
- 收藏一些有用的地址
- Trie树(字典树)
- Ext的TabPanel页面加载总结