【问题标题】:Android SQLite database contains only null valuesAndroid SQLite 数据库仅包含空值
【发布时间】:2015-07-19 07:28:58
【问题描述】:

我正在尝试将数组中的值插入 SQLite 数据库。 问题是该函数只能插入 null 值,即使数组不包含此类值。

插入函数:

public void addArrayEntry(String [] response){
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        //System.out.println(response.length);
        for (int i=1; i<response.length; i++){
            values.put(temperature,response[i]);
            db.insert(tableStatistics,null,values);
        }

        db.close();
    }

    catch (Exception e){
        Log.e("Error in insert", e.toString());
    }

}

String createTable = "CREATE TABLE statistics ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "temperature REAL, "+
            "light INTEGER, "+
            "humidity INTEGER)";

数组值:

05-08 14:24:21.405 10720-10720/org.security.andreiism.iotsecurity I/System.out﹕ 23.798828125
05-08 14:24:21.405 10720-10720/org.security.andreiism.iotsecurity I/System.out﹕ 744
05-08 14:24:21.405 10720-10720/org.security.andreiism.iotsecurity I/System.out﹕ 424

【问题讨论】:

  • 这里看起来不错 - 也许问题出在其他地方?或尝试调试以查找在values 中给db.insert 的确切内容
  • 我将for序列改为从0开始,“温度”字段的类型为“TEXT”,输出相同。
  • 尝试将ContentValues values = new ContentValues(); 移动到for 循环中

标签: android arrays database sqlite null


【解决方案1】:

在 for 循环之外写下您的 db.insert。所以所有values 只插入一次。您所做的是在第一次插入值,假设值为 1。第二次插入时插入 1 和 2。第三次插入 1、2 和 3。现在您的表将如下所示:

id| temperature
1 |1
2 |1
3 |2
4 |1
5 |2
6 |3

这是因为您在 for 循环之外定义了 values 并向其添加值,例如添加到 ArrayList。但这在下一次循环运行之前不会被删除。所以新的值只会添加到之前的values,所有的值都会一起添加到列表中。

所以你的方法应该是这样的:

public void addArrayEntry(String [] response){
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        //System.out.println(response.length);
        for (int i=1; i<response.length; i++){
            values.put(temperature,response[i]);

        }
        db.insert(tableStatistics,null,values);
        db.close();
    }

    catch (Exception e){
        Log.e("Error in insert", e.toString());
    }

}

【讨论】:

    【解决方案2】:

    尝试以下更改...

    public void addArrayEntry(String [] response){
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
    
        //System.out.println(response.length);
        for (int i=0; i<response.length; i++) {
            values.put(temperature,response[i]);            
        }
    
        db.insert(tableStatistics,null,values);
        db.close();
    }
    
    catch (Exception e){
        Log.e("Error in insert", e.toString());
    }
    
    }
    
    String createTable = "CREATE TABLE statistics ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "temperature REAL, "+
            "light INTEGER, "+
            "humidity INTEGER)";
    

    【讨论】:

    • 我尝试了替换函数,但输出是一样的。
    【解决方案3】:

    感谢大家的帮助。我终于成功地使用该函数插入了所需的值:

    public void addArrayEntry(String [] response) {
        try {
            SQLiteDatabase db = this.getWritableDatabase();
    
            ContentValues values = new ContentValues();
            values.put(temperature, response[1]);
            values.put(light, response[2]);
            values.put(humidity, response[3]);
            db.insert(tableStatistics, null, values);
            db.close();
        } catch (Exception e) {
            Log.e("Error in insert", e.toString());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 2011-01-20
      相关资源
      最近更新 更多