将mysql转储sql文件转换成可导入sqlite3 db格式的脚本

script to convert mysql dump sql file into format that can be imported into sqlite3 db(将mysql转储sql文件转换成可导入sqlite3 db格式的脚本)

本文介绍了将mysql转储sql文件转换成可导入sqlite3 db格式的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导出 SQL 文件,其中包含来自 MySQL 的表和数据,我想将其导入 Sqlite 3 DB.

I have an export SQL file containing tables and data from MySQL and I want to import it into a Sqlite 3 DB.

最好的方法是什么?

仅通过 sqlite3 工具导入文件不起作用.

Just importing the file via the sqlite3 tool doesn't work.

推荐答案

这个 shell 脚本帮助你

This shell script help you

#!/bin/sh
if [ "x$1" == "x" ]; then
   echo "Usage: $0 <dumpname>"
   exit
fi
cat $1 |
grep -v ' KEY "' |
grep -v ' UNIQUE KEY "' |
grep -v ' PRIMARY KEY ' |
sed '/^SET/d' |
sed 's/ unsigned / /g' |
sed 's/ auto_increment/ primary key autoincrement/g' |
sed 's/ smallint([0-9]*) / integer /g' |
sed 's/ tinyint([0-9]*) / integer /g' |
sed 's/ int([0-9]*) / integer /g' |
sed 's/ character set [^ ]* / /g' |
sed 's/ enum([^)]*) / varchar(255) /g' |
sed 's/ on update [^,]*//g' |
perl -e 'local $/;$_=<>;s/,\n\)/\n\)/gs;print "begin;\n";print;print "commit;\n"' |
perl -pe '
  if (/^(INSERT.+?)\(/) {
     $a=$1;
     s/\\'\''/'\'\''/g;
     s/\\n/\n/g;
     s/\),\(/\);\n$a\(/g;
  }
  ' > $1.sql
cat $1.sql | sqlite3 $1.db > $1.err
ERRORS=`cat $1.err | wc -l`
if [ $ERRORS == 0 ]; then
  echo "Conversion completed without error. Output file: $1.db"
  rm $1.sql
  rm $1.err
    rm tmp
else
   echo "There were errors during conversion.  Please review $1.err and $1.sql for details."
fi

这篇关于将mysql转储sql文件转换成可导入sqlite3 db格式的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将mysql转储sql文件转换成可导入sqlite3 db格式的脚本

基础教程推荐