gpt4 book ai didi

mysql2sqlite.sh 自动增量

转载 作者:IT王子 更新时间:2023-10-29 06:24:12 25 4
gpt4 key购买 nike

原始的 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$$

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_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")
)

我想更改 "_id"int(11) NOT NULL ,
变成这样 "_id"int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,

变成这样 "_id"int(11) NOT NULL AUTO_INCREMENT,
没有主键也可以

想修改这个脚本吗?

最佳答案

AUTO_INCREMENT 关键字特定于 MySQL。

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

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

参见 http://www.sqlite.org/autoinc.html了解更多详情。

如果您想修改此脚本以添加 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 要求自动增量 遵循列级 主键约束。它不满意将 pk 约束放在末尾的 MySQL 约定,作为表级约束。 SQLite documentation for CREATE TABLE 中的语法图支持这一点.

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

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

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

sqlite> create table foo (
i integer primary key autoincrement
);
sqlite>

成功!

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


回复你的评论:

您正在尝试复制名为 SQL::Translator 的 Perl 模块的工作,这是很多工作。我不会为您编写完整的工作脚本。

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

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

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

关于mysql2sqlite.sh 自动增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20744711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com