mysql2sqlite.sh自动增量
原始MySQl Tbl_驱动程序mysql2sqlite.sh自动增量,mysql,sqlite,Mysql,Sqlite,原始MySQl Tbl_驱动程序 delimiter $$ CREATE TABLE `tbl_driver` ( `_id` int(11) NOT NULL AUTO_INCREMENT, `Driver_Code` varchar(45) NOT NULL, `Driver_Name` varchar(45) NOT NULL, `AddBy_ID` int(11) NOT NULL, PRIMARY KEY (`_id`) ) ENGINE=InnoDB AUTO_
delimiter $$
CREATE TABLE `tbl_driver` (
`_id` int(11) NOT NULL AUTO_INCREMENT,
`Driver_Code` varchar(45) NOT NULL,
`Driver_Name` varchar(45) NOT NULL,
`AddBy_ID` int(11) NOT NULL,
PRIMARY KEY (`_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1$$
mysql2sqlite.sh
#!/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
当执行此脚本时,我的sqlite数据库如下
Sqlite Tbl_驱动程序
CREATE TABLE "tbl_driver" (
"_id" int(11) NOT NULL ,
"Driver_Code" varchar(45) NOT NULL,
"Driver_Name" varchar(45) NOT NULL,
"AddBy_ID" int(11) NOT NULL,
PRIMARY KEY ("_id")
)
我想更改“\u id”int(11)不为空,
变成这样的
“\u id”int(11)NOT NULL主键自动递增,
或
变成这样的
“\u id”int(11)非空自动增量,
没有主键也可以
有没有修改这个脚本的想法?自动增量关键字是MySQL特有的 SQLite有一个关键字
AUTOINCREMENT
(不带下划线),这意味着该列自动生成以前从未在表中使用过的单调递增值
如果省略了AUTOINCREMENT
关键字(正如您当前显示的脚本所做的那样),SQLite会将ROWID分配给新行,这意味着它的值将比表中当前最大的ROWID大1。如果从表的高端删除行,然后插入新行,则可能会重复使用值
有关更多详细信息,请参阅
如果要修改此脚本以添加AUTOINCREMENT
关键字,则可以修改此行:
gsub( /AUTO_INCREMENT|auto_increment/, "" )
为此:
gsub( /AUTO_INCREMENT|auto_increment/, "AUTOINCREMENT" )
您的评论如下: 好吧,我用sqlite3在一个虚拟表上试过了
sqlite> create table foo (
i int autoincrement,
primary key (i)
);
Error: near "autoincrement": syntax error
显然,SQLite要求autoincrement
遵循列级主键约束。它不喜欢MySQL将pk约束作为表级约束放在末尾的约定。SQLite中的语法图支持这一点
让我们尝试将主键
放在自动递增
之前
sqlite> create table foo (
i int primary key autoincrement
);
Error: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
显然SQLite不喜欢“INT”,它更喜欢“INTEGER”:
成功
因此,您的awk脚本无法像您想象的那样轻松地将MySQL表DDL转换为SQLite
您的评论如下: 您正在尝试复制一个名为的Perl模块的工作,这是一个很大的工作。我不会为你写一个完整的工作脚本 要真正解决这个问题,并生成一个脚本,使所有语法更改自动化,使DDL与SQLite兼容,您需要为SQLDDL实现一个完整的解析器。这在awk中是不实际的 我建议您在一些关键字替换的情况下使用脚本,如果需要进一步的更改,请在文本编辑器中手动修复它们
也要考虑妥协。如果在SQLite中重新格式化DDL以使用<代码>自动增量< /COD>特性,请考虑默认ROWID功能是否足够接近。阅读我上面发布的链接,了解其中的区别。我发现了一个奇怪的解决方案,但它适用于PHP原则 创建一个Mysql数据库。 从数据库中创建条令2实体,构成所有一致性 条令2具有将实体与数据库进行比较并修复数据库以验证实体的功能 通过mysql2sqlite.sh导出数据库完全符合您的描述 因此,您可以将条令驱动程序配置为使用sqlite db,并: 作曲人:
vendor/bin/doctrine-module orm:schema-tool:update --force
它修复了自动增量,无需手动操作。不起作用,当我将数据库中的表更改为
gsub(/auto\u increment | auto\u increment/,“AUTOINCREMENT”)
,只创建了一个表,并且该表没有任何自动增量。如何查看.sh文件错误日志?我认为此脚本编辑的自动增量有些语法不正确。必须弄清楚,是不是脚本我应该把所有的int(11)都改成整数,gsub(/int(11)/,“INTEGER”)
也不能工作
vendor/bin/doctrine-module orm:schema-tool:update --force