我尝试使用以下脚本将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 $
答案 0 :(得分:1)
删除表名中的双重倒置逗号以及字段声明,如:
DROP TABLE IF EXISTS acos;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE acos (
答案 1 :(得分:1)
删除FULLTEXT KEY
;在SQLite中,全文搜索工作differently。
转储中还有其他错误,但您没有显示代码。
无论如何,您只需删除LOCK
/ UNLOCK
命令。