0

我尝试使用以下脚本将 mysqldump 文件转换为 Sqlite 3 兼容文件。

#!/bin/bash
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' |
sed 's/\\r\\n/\\n/g' |
sed 's/\\"/"/g' |
sed 's/ "id" bigint(20) NOT NULL/ "id" integer primary key autoincrement/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"
else
echo "There were errors during conversion.  Please review $1.err and $1.sql for   details."
fi

尝试转换以下转储文件时出现以下错误:

DROP TABLE IF EXISTS "acos";
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE "acos" (
  "id" int(10) NOT NULL,
 "parent_id" int(10) DEFAULT NULL,
  "model" varchar(255) DEFAULT NULL,
  "foreign_key" int(10) DEFAULT NULL,
  "alias" varchar(255) DEFAULT NULL,
  "lft" int(10) DEFAULT NULL,
  "rght" int(10) DEFAULT NULL,
  PRIMARY KEY ("id"),
  FULLTEXT KEY "alias" ("alias")
 );

错误:

'/home/anees/TestProjects/mysqltosqlite/sqltests/mysql-to-sqlite.sh'     '/home/anees/TestProjects/mysqltosqlite/sqltests/mydb.sql'
Error: near line 26: near ")": syntax error
Error: near line 41: near "LOCK": syntax error
Error: near line 43: no such table: acos
Error: near line 44: no such table: acos
Error: near line 45: no such table: acos
Error: near line 46: no such table: acos
Error: near line 48: near "UNLOCK": syntax error
Conversion completed without error. Output file:    /home/anees/TestProjects/mysqltosqlite/sqltests/mydb.sql.db

anees@anees-desktop:~/TestProjects/mysqltosqlite/sqltests$

4

2 回答 2

1

删除表名中的双引号以及字段声明,例如:

DROP TABLE IF EXISTS acos;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE acos (
于 2012-12-04T11:47:39.250 回答
1

删除FULLTEXT KEY; 在 SQLite 中,全文搜索的工作方式不同

转储中还有其他错误,但您没有显示代码。在任何情况下,您都可以删除LOCK/UNLOCK命令。

于 2012-12-04T14:39:43.680 回答