Sqlite数据库没有插入

时间:2014-04-17 15:15:24

标签: android database sqlite sql-insert android-cursor

我知道这是一个非常基本的问题,但我似乎无法弄明白。我的行没有插入数据库中。

任何人都知道出了什么问题?

 //this is the code where I try to insert the data

    dbAdapter.open();
    dbAdapter.EnterCreateHistory(DatabaseAdapter.WALKER_HISTORY_TABLE, "fdfsf", "fdfsf1", "fdfsf", "fdfsf", "fdfsf");
    dbAdapter.EnterCreateHistory(DatabaseAdapter.WALKER_HISTORY_TABLE, "fdfsf1", "fdfsf2", "fdfsf1", "fdfsf1", "fdfsf1");
    dbAdapter.EnterCreateHistory(DatabaseAdapter.WALKER_HISTORY_TABLE, "fdfsf1", "fdfsf3", "fdfsf1", "fdfsf1", "fdfsf1");
    dbAdapter.EnterCreateHistory(DatabaseAdapter.WALKER_HISTORY_TABLE, "fdfsf1", "fdfsf4", "fdfsf1", "fdfsf1", "fdfsf1");
    dbAdapter.EnterCreateHistory(DatabaseAdapter.WALKER_HISTORY_TABLE, "fdfsf1", "fdfsf5", "fdfsf1", "fdfsf1", "fdfsf1");

    Cursor walkerHistory = dbAdapter.getAllData(DatabaseAdapter.WALKER_HISTORY_TABLE);
 //here I check if the data is being inserted, 
//but the number of columns always comes back as 0
    Toast.makeText(getApplicationContext(), "Number of rows inserted:  "+walkerHistory.getCount(), Toast.LENGTH_SHORT).show();

以下是DatabaseAdapter中的OnCreate代码:

   public void EnterCreateHistory(String tableName, String recommendationDate, String recommendation, String activityDate, String activity, String analyses){
    ContentValues values= new ContentValues();
    db = mdb.getWritableDatabase(); 

    values.put(RECOMMENDATION_DATE,recommendationDate);
    values.put(RECOMMENDATION,recommendation);
    values.put(ACTIVITY_DATE,activityDate);
    values.put(ACTIVITY, activity);
    values.put(MONITOR,analyses);

    long rowid=db.insert(tableName, null, values);
    rowid1=rowid;
    Log.d(TAG, "IDD"+rowid1);

}

编辑:我根据@MDMalik的建议更新了我的代码,但它仍未插入。

Edit2:我不确定这是否重要,但我忘了提到数据库曾经工作过。我有4个条目使用相同的代码,但它只是停止添加新代码。

1 个答案:

答案 0 :(得分:0)

试试此代码

public void EnterCreateHistory(String tableName, String recommendationDate, String recommendation, String activityDate, String activity, String analyses){
ContentValues values= new ContentValues();

//    **//Add this line**
db = this.getWritableDatabase();       

values.put(RECOMMENDATION_DATE,recommendationDate);
values.put(RECOMMENDATION,recommendation);
values.put(ACTIVITY_DATE,activityDate);
values.put(ACTIVITY, activity);
values.put(MONITOR,analyses);

    long rowid=db.insert(tableName, null, values);
    rowid1=rowid;
    Log.d(TAG, "IDD"+rowid1);
}