开发者

Values stored using ContentValues are not in correct order, WHY?

开发者 https://www.devze.com 2023-01-07 23:11 出处:网络
currently I\'m working on my 1st Android app and I encounter something I cannot solve. When I try to insert data into my own database there is an error: 07-18 03:41:04.414: ERROR/AndroidRuntime(3480):

currently I'm working on my 1st Android app and I encounter something I cannot solve. When I try to insert data into my own database there is an error: 07-18 03:41:04.414: ERROR/AndroidRuntime(3480): java.lang.NullPointerException

My code for inserting into table is:

public long createRecord(String created_time, String modified_time,
        long reading, String unit, String period, String remark) {

    ContentValues values = new ContentValues(6);
    values.put(KEY_CREATED_TIME, created_time);
    values.put(KEY_MODIFIED_TIME, modified_time); //System.out.println(values);
    values.put(KEY_RECORD, reading); //System.out.println(values);
    values.put(KEY_UNIT, unit); //System.out.println(values);
    values.put(KEY_PERIOD, period);// Sy开发者_JAVA百科stem.out.println(values);
    values.put(KEY_REMARK, remark); 

    System.out.println(values);
    return mDb.insert(DATABASE_TABLE, null , values);

}

This is under my Adapter class which is an instance of my DatabaseHelper class. For my DatabaseHelper class, I can copy created database from assets folder into system databases folder. But just cannot create data into table, I tried to print out the "values", it showed that:

created_time=2010-07-18 03:41:04 remark=on unit=mg/dL reading=67 period=Before Meal modified_time=00:00:00 which was not the way it supposed to be, so I guessed this might be the problem. Can anyone help me with this? Searched online but not so many info on this. Thanks in advance:)


The reason for the order not being retained is that ContentValues is backed by a HashMap. So that's ok.
The problem is likely caused by mDb being null (check it with System.out.println("" + mDb);).


if(db!=null){
         ContentValues row = new ContentValues();
         row.put("rt_id",tid);
         row.put("rs_id", sid);
         row.put("rsubject", rsubject);
         row.put("rdescr",rdescr);
         row.put("day", day);
         row.put("month",month);
         row.put("year",year);
         row.put("stamp", stamp);
         row.put("amount",amount);

         long chk = db.insert("records",null, row);

         if(chk!=0){
             Toast.makeText(myContext, "Record added successfully",Toast.LENGTH_LONG).show(); 
         }else{
             Toast.makeText(myContext, "Record added failed...! ",Toast.LENGTH_LONG).show();
         }
       }else{
           Toast.makeText(myContext, "could Not connected database..!!! ",Toast.LENGTH_LONG).show(); 
       }
     }catch(SQLiteException exc){
         Toast.makeText(myContext, "Record could not saved...!!! ",Toast.LENGTH_LONG).show();
     }finally{
         db.close();
         }

You can check your database object is null or not...Or checking it by just println it..All the best.


Have you seen the source code of sqliteDatabase i?It made a sql and handle it.but the order is based on :

   Set<Map.Entry<String, Object>> entrySet = null;
    if (initialValues != null && initialValues.size() > 0) {
        entrySet = initialValues.valueSet();
        Iterator<Map.Entry<String, Object>> entriesIter = entrySet.iterator();

I think something wrong with it? or the order will wrong when lots of values.Do you find somew

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号