简体   繁体   English

使用ContentValues将多个记录插入Sqlite数据库中的两个列

[英]Inserting Multiples Records into Two Columns in Sqlite Database using ContentValues


Whenever I use the following code to insert multiple records into a table it only inserts the last initialized value. 每当我使用以下代码将多个记录插入表中时,它只会插入最后一个初始化值。

Here's the method for inserting data into a table: 这是将数据插入表中的方法:

public long insertQuote() {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_PHID, 100);
        initialValues.put(KEY_LAT, 28.5700);
        initialValues.put(KEY_LAT, 28.4700);
        initialValues.put(KEY_LAT, 27.1833);
        initialValues.put(KEY_LAT, 26.4583);

        initialValues.put(KEY_LON, 77.3200);
        initialValues.put(KEY_LON, 77.0300);
        initialValues.put(KEY_LON, 78.0167);
        initialValues.put(KEY_LON, 80.3173);

        return db.insert(DATABASE_TABLE, null, initialValues);
    }

In this case for the column key "KEY_LAT" I am only able to see the last initialized value "26.4583" on a SOP (Sys.out.println) output on logcat. 在这种情况下,对于列密钥“KEY_LAT”,我只能在logcat上的SOP(Sys.out.println)输出上看到最后一个初始化值“26.4583”。

Same follows for the other column key "KEY_LON". 对于其他列键“KEY_LON”也是如此。 I can see just these two records. 我只能看到这两个记录。

I suppose they're not inserted into the table as put() Method skips the previously "to-be-inserted" values and accepts the last for a particular column. 我想它们没有插入到表中,因为put()方法跳过以前的"to-be-inserted"值并接受特定列的最后一个值。

Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

This is happening because you are using same key LAT and LON for all. 发生这种情况是因为您正在为所有人使用相同的密钥LAT和LON。 That's being overwritten. 那被覆盖了。

You should use different keys such as 你应该使用不同的键,如

public long insertQuote() {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_PHID, 100);
        initialValues.put(KEY_LAT1, 28.5700);
        initialValues.put(KEY_LAT2, 28.4700);
        initialValues.put(KEY_LAT3, 27.1833);
        initialValues.put(KEY_LAT4, 26.4583);

        initialValues.put(KEY_LON1, 77.3200);
        initialValues.put(KEY_LON2, 77.0300);
        initialValues.put(KEY_LON3, 78.0167);
        initialValues.put(KEY_LON4, 80.3173);

        return db.insert(DATABASE_TABLE, null, initialValues);
    }

Edit: 编辑:

So you should use this approach. 所以你应该使用这种方法。

public long insertQuote() {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_PHID, 100);
            initialValues.put(KEY_LAT, 28.5700);
            initialValues.put(KEY_LON, 77.3200);
            db.insert(DATABASE_TABLE, null, initialValues);
            initialValues.put(KEY_LAT, 28.4700);
            initialValues.put(KEY_LON, 77.0300);
            db.insert(DATABASE_TABLE, null, initialValues);
            initialValues.put(KEY_LAT, 27.1833);
            initialValues.put(KEY_LON, 78.0167);
            db.insert(DATABASE_TABLE, null, initialValues);
            initialValues.put(KEY_LAT, 26.4583);
            initialValues.put(KEY_LON, 80.3173);
            return db.insert(DATABASE_TABLE, null, initialValues);  
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM