我使用脚本Github中的mysql2sqlite.sh将我的mysql数据库更改为sqlite。但我得到的问题是,在我的表中,数据'E-001'变为'E?001'。
我不知道如何修改脚本以获得所需的结果。请帮帮我。
脚本是
#!/bin/sh
# Converts a mysqldump file into a Sqlite 3 compatible file. It also extracts the MySQL `KEY xxxxx` from the
# CREATE block and create them in separate commands _after_ all the INSERTs.
# Awk is choosen because it's fast and portable. You can use gawk, original awk or even the lightning fast mawk.
# The mysqldump file is traversed only once.
# Usage: $ ./mysql2sqlite mysqldump-opts db-name | sqlite3 database.sqlite
# Example: $ ./mysql2sqlite --no-data -u root -pMySecretPassWord myDbase | sqlite3 database.sqlite
# Thanks to and @artemyk and @gkuenning for their nice tweaks.
mysqldump --compatible=ansi --skip-extended-insert --compact "$@" | \
awk '
BEGIN {
FS=",$"
print "PRAGMA synchronous = OFF;"
print "PRAGMA journal_mode = MEMORY;"
print "BEGIN TRANSACTION;"
}
# CREATE TRIGGER statements have funny commenting. Remember we are in trigger.
/^\/\*.*CREATE.*TRIGGER/ {
gsub( /^.*TRIGGER/, "CREATE TRIGGER" )
print
inTrigger = 1
next
}
# The end of CREATE TRIGGER has a stray comment terminator
/END \*\/;;/ { gsub( /\*\//, "" ); print; inTrigger = 0; next }
# The rest of triggers just get passed through
inTrigger != 0 { print; next }
# Skip other comments
/^\/\*/ { next }
# Print all `INSERT` lines. The single quotes are protected by another single quote.
/INSERT/ {
gsub( /\\\047/, "\047\047" )
gsub(/\\n/, "\n")
gsub(/\\r/, "\r")
gsub(/\\"/, "\"")
gsub(/\\\\/, "\\")
gsub(/\\\032/, "\032")
print
next
}
# Print the `CREATE` line as is and capture the table name.
/^CREATE/ {
print
if ( match( $0, /\"[^\"]+/ ) ) tableName = substr( $0, RSTART+1, RLENGTH-1 )
}
# Replace `FULLTEXT KEY` or any other `XXXXX KEY` except PRIMARY by `KEY`
/^ [^"]+KEY/ && !/^ PRIMARY KEY/ { gsub( /.+KEY/, " KEY" ) }
# Get rid of field lengths in KEY lines
/ KEY/ { gsub(/\([0-9]+\)/, "") }
# Print all fields definition lines except the `KEY` lines.
/^ / && !/^( KEY|\);)/ {
gsub( /AUTO_INCREMENT|auto_increment/, "" )
gsub( /(CHARACTER SET|character set) [^ ]+ /, "" )
gsub( /DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP|default current_timestamp on update current_timestamp/, "" )
gsub( /(COLLATE|collate) [^ ]+ /, "" )
gsub(/(ENUM|enum)[^)]+\)/, "text ")
gsub(/(SET|set)\([^)]+\)/, "text ")
gsub(/UNSIGNED|unsigned/, "")
if (prev) print prev ","
prev = $1
}
# `KEY` lines are extracted from the `CREATE` block and stored in array for later print
# in a separate `CREATE KEY` command. The index name is prefixed by the table name to
# avoid a sqlite error for duplicate index name.
/^( KEY|\);)/ {
if (prev) print prev
prev=""
if ($0 == ");"){
print
} else {
if ( match( $0, /\"[^"]+/ ) ) indexName = substr( $0, RSTART+1, RLENGTH-1 )
if ( match( $0, /\([^()]+/ ) ) indexKey = substr( $0, RSTART+1, RLENGTH-1 )
key[tableName]=key[tableName] "CREATE INDEX \"" tableName "_" indexName "\" ON \"" tableName "\" (" indexKey ");\n"
}
}
# Print all `KEY` creation lines.
END {
for (table in key) printf key[table]
print "END TRANSACTION;"
}
'
exit 0
答案 0 :(得分:1)
我无法提供有保障的解决方案,但这是一种我成功使用的简单技术来处理类似的问题(参见下面的“注释”)。最近几天我一直在和这个剧本搏斗,并认为这是值得分享的,以防有其他人需要调整它但被awk学习曲线阻碍。
基本思路是将脚本输出到文本文件,编辑文件,然后导入到sqlite(下面更详细的说明)。
你可能需要进行一些实验,但至少你不需要学习awk(虽然我一直在努力,这很有趣......)。
如何
运行脚本导出到文件(而不是直接传递) 到sqlite3):
./mysql2sqlite -u root -pMySecretPassWord myDbase > sqliteimport.sql
使用您首选的文本编辑技术来清理任何混乱 你遇到过。例如,在sublimetext中搜索/替换。 (有关提示,请参阅下面的最后一个注释。)
将已清理的脚本导入sqlite:
sqlite3 database.sqlite < sqliteimport.sql
注意:
我怀疑你正在处理的是一个编码问题 - ' - '表示一个字符,它不能被你的shell,脚本(awk)识别,或者意味着什么不同,或者你的sqlite数据库。根据您的具体情况,您可能无法解决问题(请参阅下一个注释)。
预先警告,如果违规字符嵌入 text 数据(不仅仅是作为文本,而是实际,这很可能只会起作用> text 内容存储在文本字段中)。如果它们是机器名(外键字段,实体id,例如),存储为文本的二进制数据,或存储在二进制字段(例如blob)中的文本数据,则要小心。你可以尝试一下,但不要满怀希望,即使似乎工作也一定要测试它。
如果事实上“ - ”表示某些不寻常的字符,您可能无法在搜索/替换工具的“搜索”字段中键入连字符。从源数据中复制它(例如,打开文件,突出显示并复制到剪贴板),然后粘贴到工具中。
希望这有帮助!
答案 1 :(得分:-1)