【问题标题】:Replacing Data in Table for SQLite为 SQLite 替换表中的数据
【发布时间】:2019-02-07 09:02:27
【问题描述】:

我查看了一些不同的建议,但由于某种原因,以下解决方案都无法工作/仍然在表中重复数据。

"Category" 将是此 SQLite 表的唯一值。要更改的值为“spending_limit”。

到目前为止我已经尝试过:

public void writeToSpendingLimits(int sequence, double amount){
    String sql = "INSERT OR REPLACE INTO spending_limits(category, spending_limit) "
    + "VALUES(" + sequence + ", " +  amount + ");";           
    connection.executeSQL(sql);
}

.

public void writeToSpendingLimits(int sequence, double amount){           
        String sql = "INSERT INTO spending_limits (category, spending_limit) "
            + "VALUES(" + sequence + ", " + amount + ") "
            + "ON CONFLICT(category)"
            + "DO UPDATE SET spending_limit = " + amount + ";";
    connection.executeSQL(sql);

.

public void writeToSpendingLimits(int sequence, double amount){            
    String sql = "INSERT INTO spending_limits (category, spending_limit) "
            + "VALUES (coalesce((select category from spending_limits where category = " + amount + "),"
            + "(select max(category) from spending_limits) + 1), " + amount + ");";
    connection.executeSQL(sql);

.

public void writeToSpendingLimits(int sequence, double amount){              
    String sql = "UPDATE spending_limits " 
            + "SET category = " + sequence + ", " + "spending_limit = " + amount + " "
            + "WHERE spending_limit = " + sequence + ";\n"
            + "INSERT INTO spending_limits(category, spending_limit) "
            + "SELECT " + sequence + ", " + amount + " "
            + "WHERE (Select Changes() = 0)";
    connection.executeSQL(sql);

【问题讨论】:

  • 你的表架构是什么?不工作是什么意思?

标签: sql database sqlite replace


【解决方案1】:

假设您希望每个类别有一个单一的支出限制,那么您可以将该类别设为 UNIQUE 列。那么重复的类别会导致冲突,并且会执行冲突操作,例如 OR REPLACE

例如考虑以下几点:-

DROP TABLE IF EXISTS spending_limits;
CREATE TABLE IF NOT EXISTS spending_limits (category INTEGER UNIQUE NOT NULL, spending_limit REAL);
INSERT OR REPLACE INTO spending_limits (category, spending_limit) VALUES(1,10.23);
INSERT OR REPLACE INTO spending_limits (category, spending_limit) VALUES(2,10.23);
INSERT OR REPLACE INTO spending_limits (category, spending_limit) VALUES(3,10.23);
INSERT OR REPLACE INTO spending_limits (category, spending_limit) VALUES(4,10.23);
INSERT OR REPLACE INTO spending_limits (category, spending_limit) VALUES(5,10.23);
INSERT OR REPLACE INTO spending_limits (category, spending_limit) VALUES('Animal',10.23);
INSERT OR REPLACE INTO spending_limits (category, spending_limit) VALUES('Vegetable',10.23);
INSERT OR REPLACE INTO spending_limits (category, spending_limit) VALUES('Mineral',10.23);
SELECT *,rowid AS rid FROM spending_limits;

INSERT OR REPLACE INTO spending_limits (category, spending_limit) VALUES(1,11.23);

INSERT OR REPLACE INTO spending_limits (category, spending_limit) VALUES('Animal',11.23);

SELECT *,rowid AS rid FROM spending_limits;

这将:-

  • 删除 spending_limits 表(如果存在)(简化测试)
  • 创建 spending_limits 表,注意类别列具有 UNIQUE 属性。
  • spending_limits 表中插入 8 行(用于演示的混合类别类型)
  • 选择所有行并输出
  • 根据类别插入 2 个重复行,但数量有所调整。
    • 即该语句根据类别是否已存在来插入新行或替换现有行。
  • 选择所有行并输出它们。

结果是:-

在初始插入 8 行之后:-

在插入(替换)具有现有类别的 2 行之后:-

  • 注意 rowid 列也会更新(因此这可能是 WITHOUT ROWID 表的候选)

【讨论】:

    猜你喜欢
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 2020-04-01
    相关资源
    最近更新 更多