为什么这个 SQLite 脚本会影响多行?
Why is this SQLite Script Affecting Multiple Rows?
我有以下简单的 SQLite 脚本来创建一个带有版本控制的新数据库 table:
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS `db_versions` (
`version` integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`name` varchar ( 50 ) DEFAULT NULL UNIQUE,
`date_defined` datetime DEFAULT NULL,
`comments` text
);
INSERT INTO `db_versions` VALUES (0,'initial-create','2017-12-02 14:41:56',NULL);
COMMIT;
运行 SQLite 数据库浏览器中的这个脚本正确地记录了只有 1 行受到影响(插入)。但是,当我尝试使用 Mono (Mono.Data.Sqlite) 在代码中执行此脚本时,该脚本显然会影响 2 行。这是代码:
using (var conn = new SqliteConnection(_connStr)) {
await conn.OpenAsync(cancellationToken);
using (SqliteCommand comm = conn.CreateCommand()) {
comm.CommandType = CommandType.Text;
comm.CommandText = @"
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS `db_versions` (
`version` integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`name` varchar ( 50 ) DEFAULT NULL UNIQUE,
`date_defined` datetime DEFAULT NULL,
`comments` text
);
INSERT INTO `db_versions` VALUES (0,'initial-create','2017-12-02 14:41:56',NULL);
COMMIT;
";
int rowsAffected = await comm.ExecuteNonQueryAsync(cancellationToken);
if (rowsAffected > 1) {
// Why is this code running??
}
}
}
有谁知道为什么我会得到这些不同的结果?
啊,我明白了。正在定义整数 version
字段 AUTOINCREMENT
,其中 also adds 内部 sqlite_sequence
table 的最大 ROWID。所以 添加了第二行。
我有以下简单的 SQLite 脚本来创建一个带有版本控制的新数据库 table:
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS `db_versions` (
`version` integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`name` varchar ( 50 ) DEFAULT NULL UNIQUE,
`date_defined` datetime DEFAULT NULL,
`comments` text
);
INSERT INTO `db_versions` VALUES (0,'initial-create','2017-12-02 14:41:56',NULL);
COMMIT;
运行 SQLite 数据库浏览器中的这个脚本正确地记录了只有 1 行受到影响(插入)。但是,当我尝试使用 Mono (Mono.Data.Sqlite) 在代码中执行此脚本时,该脚本显然会影响 2 行。这是代码:
using (var conn = new SqliteConnection(_connStr)) {
await conn.OpenAsync(cancellationToken);
using (SqliteCommand comm = conn.CreateCommand()) {
comm.CommandType = CommandType.Text;
comm.CommandText = @"
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS `db_versions` (
`version` integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`name` varchar ( 50 ) DEFAULT NULL UNIQUE,
`date_defined` datetime DEFAULT NULL,
`comments` text
);
INSERT INTO `db_versions` VALUES (0,'initial-create','2017-12-02 14:41:56',NULL);
COMMIT;
";
int rowsAffected = await comm.ExecuteNonQueryAsync(cancellationToken);
if (rowsAffected > 1) {
// Why is this code running??
}
}
}
有谁知道为什么我会得到这些不同的结果?
啊,我明白了。正在定义整数 version
字段 AUTOINCREMENT
,其中 also adds 内部 sqlite_sequence
table 的最大 ROWID。所以 添加了第二行。