convert sqlite to mysql

前端之家收集整理的这篇文章主要介绍了convert sqlite to mysql前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

尼玛,开始数据库不选好后面改太操蛋了,关键运行中得数据还不能丢,让人崩溃阿

1. schema先

sqlite3 xxxx.db

>.output schema.sql

>.schema

MysqLsqlite差距老大了,写一个sed脚本改schema,sql,一边写一边测,不具有可复制性

  1. #!/bin/sed -f
  2. #filename : schema.sed
  3. #author: authur
  4. #date : 2012-7-24
  5.  
  6. s/\(CREATE TABLE \)"\(\S*\)"/\1\2/
  7. /\bid\b[^,]*PRIMARY KEY/s/PRIMARY KEY/AUTO_INCREMENT PRIMARY KEY/g
  8. /[CREATE TABLE|integer|varchar]/s/"/`/g
  9. /CREATE TABLE/s/\(text[^,]*\)DEFAULT[^,]*,/\1,/g
  10. /CREATE TABLE.*workshop_blog/s/DEFAULT 'interview'//g
  11. /CREATE TABLE.*workshop_topic/s/DEFAULT 'faq'//g
  12. s/djangoratings_vote_content_type_id__object_id__cookie__user_id__key__ip_address/djratings_vote_ctype_id_object_id_cookie_user_id_key_ip_address/
  13. s/djangoratings_vote_content_type_id__object_id__key__user_id__ip_address/djratings_vote_ctype_id_object_id_key_user_id_ip_address/

./schema.sed schema.sql > schema-MysqL.sql


2. 数据

数据就更让人崩溃了,MysqL utf 字符集不支持大小写敏感,还有数据格式,一堆错误

sqlite3 xxxx.db

>.output dump.sql

>.dump

首先修改schema.sed为如下

  1. #!/bin/sed -f
  2. #filename : schema.sed
  3. #author: authur
  4. #date : 2012-7-24
  5.  
  6.  
  7. s/\(CREATE TABLE \)"\(\S*\)"/\1\2/
  8. /\bid\b[^,/g
  9. /CREATE TABLE.*workshop_blog/s/DEFAULT 'interview'//g
  10. /CREATE TABLE.*workshop_topic/s/DEFAULT 'faq'//g
  11. s/djangoratings_vote_content_type_id__object_id__cookie__user_id__key__ip_address/djratings_vote_ctype_id_object_id_cookie_user
  12. _id_key_ip_address/
  13. s/djangoratings_vote_content_type_id__object_id__key__user_id__ip_address/djratings_vote_ctype_id_object_id_key_user_id_ip_addr
  14. ess/
  15.  
  16. s/PRAGMA foreign_keys=OFF;/SET foreign_key_checks = 0;/
  17. s/BEGIN TRANSACTION;/START TRANSACTION;/
  18. s/\(INSERT INTO \)"\(\S*\)"/\1\2/


然后./schema.sed dump.sql > dump-MysqL.sql

到这里如果你认为大功已经告成你就错了,数据能不能导还说不定呢


首先尝试把dump-MysqL.sql 导入MysqL,如果发现因为大小写不敏感导致duplicate key或其他错误你就试试下面方法

设置MysqL collation, 在my.cnf里添加

[MysqLd]

character-set-server=latin1
collation-server=latin1_general_cs #_cs结尾得都行, cs就是case sensitive,ci 就是 case insensitive,utf8 没有 _cs结尾得


再重启MysqL试试。


如果有用中文做unique key,导入基本也有问题, 做unique key好像只能区别出前两个字,尼玛。这怎么解决呢,看业务得吧,我比较粗暴点,把每个key前面加个随机字符,自己看着办吧。

  1. update workshop_tag set name = substr(abs(random()),1,10) || name;

然后重新dump, 用schema.sed 处理一次

处理了这两个问题,我现在数据已经可以导入到MysqL了,中间有没有隐藏问题谁也说不定阿,先这样用着呗。

ps。 一切做好备份,别转不成也回不去了就麻烦了。

猜你在找的Sqlite相关文章