如何优雅的将数据从sqlite3迁移到mysql
2013-09-20 18:29
645 查看
网上找了很多资料。发现都是有问题的。
比较接近要求有的。http://stackoverflow.com/questions/18671/quick-easy-way-to-migrate-sqlite3-to-mysql
在他基础上进行改造。
python代码
执行
之后联接数据库过程中需要设置联接以后编码为utf-8
然后导入dump.sql执行。ok了。
其中可能会出现错误
varchar(64,0)
手工修改为varchar(64)
=_=
比较接近要求有的。http://stackoverflow.com/questions/18671/quick-easy-way-to-migrate-sqlite3-to-mysql
在他基础上进行改造。
python代码
import re import fileinput def this_line_is_useless(line): useless_es = [ 'BEGIN TRANSACTION', 'COMMIT', 'sqlite_sequence', 'CREATE UNIQUE INDEX', ] for useless in useless_es: if re.search(useless, line): return True def has_primary_key(line): return bool(re.search(r'PRIMARY KEY', line)) searching_for_end = False for line in fileinput.input(): if this_line_is_useless(line): continue if re.match(r".*, ''\);", line): line = re.sub(r"''\);", r'``);', line) if re.match(r'^CREATE TABLE.*', line): searching_for_end = True m = re.search('CREATE TABLE "?([a-z0-9_]*)"?(.*)', line) if m: name, sub = m.groups() line = "DROP TABLE IF EXISTS %(name)s;\nCREATE TABLE IF NOT EXISTS `%(name)s`%(sub)s\n" line = line % dict(name=name, sub=sub) else: m = re.search('INSERT INTO "([a-z0-9_]*)"(.*)', line) if m: line = 'INSERT INTO %s%s\n' % m.groups() line = line.replace('"', r'\"') line = line.replace('"', "'") line = re.sub(r"([^'])'t'(.)", "\1THIS_IS_TRUE\2", line) line = line.replace('THIS_IS_TRUE', '1') line = re.sub(r"([^'])'f'(.)", "\1THIS_IS_FALSE\2", line) line = line.replace('THIS_IS_FALSE', '0') if searching_for_end: if re.search(r"integer(?:\s+\w+)*\s*PRIMARY KEY(?:\s+\w+)*\s*,", line): line = line.replace("PRIMARY KEY", "PRIMARY KEY AUTO_INCREMENT") line = line.replace('"', '`').replace("'", '`') if re.match(r".*, ``\);", line): line = re.sub(r'``\);', r"'');", line) if searching_for_end and re.match(r'.*\);', line): searching_for_end = False if re.match(r"CREATE INDEX", line): line = re.sub('"', '`', line) if re.match(r'ANALYZE sqlite_master', line): line = '' if re.match(r'INSERT INTO sqlite_stat1', line): line = '' if line is not None: print line,
执行
sqlite3 your.db .dump | python dump_for_mysql.py > dump.sql
之后联接数据库过程中需要设置联接以后编码为utf-8
然后导入dump.sql执行。ok了。
其中可能会出现错误
varchar(64,0)
手工修改为varchar(64)
=_=
相关文章推荐
- [MySQL FAQ]系列 -- 如何跨时区迁移数据
- mysql数据迁移到mongoDB中,如何操作?
- [MySQL FAQ]系列 -- 如何跨时区迁移数据
- 转义字符导致sqlite迁移至mysql少了15w数据
- MySQL------如何将SQLServer文件数据迁移到MySQL
- MySQL如何跨机器迁移数据?
- 如何从把mysql的数据迁移到oracle上来---------Oracle SQL Developer技术
- MySQL如何跨时区迁移数据
- [原创]如何实现MySQL表数据随机读取
- mssql数据迁移到mysql
- 在MYSQL中如何查看第N条数据
- node.js如何读取MySQL数据(转)
- 五个小技巧告诉你如何保护MySQL数据仓库
- MySQL的binlog数据如何查看
- 如何用Navicat导出MySQL的数据字典
- SQLServer(MSSQL)、MySQL、SQLite、Access相互迁移转换工具 DB2DB v1.0
- mysql语句中如何用正则表达式匹配数据
- 如何解决Php(adodb)输出Mysql中数据乱码问题!
- 如何利用MySQL加密函数保护网站敏感数据
- MySQL的binlog数据如何查看