mysql2sqlite.sh Auto_Increment

Joh*_*ker 7 mysql sqlite

原始的MySQl Tbl_driver

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$$
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

执行这个脚本时,我的sqlite数据库就像这样

Sqlite Tbl_Driver

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")
)
Run Code Online (Sandbox Code Playgroud)

我想改变"_id" int(11) NOT NULL ,
成这样"_id" int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
或者
变成这样的"_id" int(11) NOT NULL AUTO_INCREMENT,
主键也可以

有什么想法来修改这个脚本吗?

Bil*_*win 4

AUTO_INCREMENT关键字特定于 MySQL。

SQLite 有一个关键字AUTOINCREMENT(不带下划线),这意味着该列自动生成表中以前从未使用过的单调递增值。

如果您省略AUTOINCREMENT关键字(正如您当前显示的脚本所做的那样),SQLite 会将 ROWID 分配给新行,这意味着它将是比表中当前最大 ROWID 大 1 的值。如果您从表的高端删除行然后插入新行,则可以重复使用值。

有关更多详细信息,请参阅http://www.sqlite.org/autoinc.html 。

如果你想修改这个脚本来添加AUTOINCREMENT关键字,看起来你可以修改这一行:

gsub( /AUTO_INCREMENT|auto_increment/, "" )
Run Code Online (Sandbox Code Playgroud)

对此:

gsub( /AUTO_INCREMENT|auto_increment/, "AUTOINCREMENT" )
Run Code Online (Sandbox Code Playgroud)

回复您的评论:

好吧,我使用 sqlite3 在虚拟表上尝试了它。

sqlite> create table foo ( 
  i int autoincrement, 
  primary key (i)
);
Error: near "autoincrement": syntax error
Run Code Online (Sandbox Code Playgroud)

显然 SQLite 要求autoincrement遵循列级主键约束。它对将 pk 约束作为表级约束放在最后的 MySQL 约定不满意。SQLite文档中 CREATE TABLE 的语法图支持这一点。

让我们尝试放在primary key之前autoincrement

sqlite> create table foo ( 
  i int primary key autoincrement
);
Error: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
Run Code Online (Sandbox Code Playgroud)

显然 SQLite 不喜欢“INT”,它更喜欢“INTEGER”:

sqlite> create table foo (
  i integer primary key autoincrement
);
sqlite>
Run Code Online (Sandbox Code Playgroud)

成功!

因此,您的 awk 脚本无法像您想象的那样轻松地将 MySQL 表 DDL 转换为 SQLite。


回复您的评论:

您正在尝试复制名为SQL::Translator的 Perl 模块的工作,这是一项艰巨的工作。我不会为你写一个完整的工作脚本。

要真正解决这个问题,并制作一个可以自动执行所有语法更改的脚本以使 DDL 与 SQLite 兼容,您需要为 SQL DDL 实现完整的解析器。这在 awk 中是不实际的。

我建议您将脚本用于某些关键字替换的情况,然后如果需要进一步更改,请在文本编辑器中手动修复它们。

还要考虑做出妥协。如果重新格式化 DDL 来使用AUTOINCREMENTSQLite 中的功能太困难,请考虑默认的 ROWID 功能是否足够接近。阅读我上面发布的链接以了解差异。