无法在ContentValues中插入数据

时间:2013-06-04 22:58:28

标签: android

您好我正在使用此代码将数据插入我的数据库。在另一个活动中我使用Cursor,我可以分别看到table1或table2的数据。但如果我同时使用两者,我只能看到表2的数据。

当我使用游标获取数据时,我看到一个是空的。我想我没有正确插入数据。

我做错了什么?

ContentValues cv = new ContentValues();

//表1

cv.put(idAttendance, 1);
cv.put(attendPlayer, "Tiago");

db.insert(AttendanceTable, idAttendance, cv);

    cv.put(idAttendance, 2);
    cv.put(attendPlayer, "Joao");
    db.insert(AttendanceTable, idAttendance, cv);


    cv.put(idAttendance, 3);
    cv.put(attendPlayer, "Pedro");

  db.insert(AttendanceTable, idAttendance, cv);

// TABLE2

    cv.put(idTrainInd, 1);
    cv.put(TIindicator, "Remate Sucesso");
    db.insert(TrainingIndicatorTable, idTrainInd, cv);

    cv.put(idTrainInd, 2);
    cv.put(TIindicator, "Remate SEM Sucesso");
    db.insert(TrainingIndicatorTable, idTrainInd, cv);

仍然无法保存数据。

我的班级是这样的:

public class DatabaseHelper extends SQLiteOpenHelper {

private ... // database and tables


public DatabaseHelper(Context context) {
      super(context, dbName, null,33); 


}

SQLiteDatabase db = null;



@Override
public void onCreate(SQLiteDatabase db) {


//here i create all tables
db.execSQL("CREATE TABLE ... ) 


db=this.getWritableDatabase();


//THEN I USE THE CODE OF MY FIRST QUESTION

ContentValues cv=new ContentValues();

//TABLE 1
cv.put(idAttendance, 1);
cv.put(attendPlayer, "Tiago");

db.insert(AttendanceTable, idAttendance, cv);



cv.put(idAttendance, 2);
cv.put(attendPlayer, "Joao");
db.insert(AttendanceTable, idAttendance, cv);


cv.put(idAttendance, 3);
cv.put(attendPlayer, "Pedro");

db.insert(AttendanceTable, idAttendance, cv);


//TABLE2

//TABLE2
cv.put(idTrainInd, 1);
cv.put(TIindicator, "Remate Sucesso");
db.insert(TrainingIndicatorTable, idTrainInd, cv);

cv.put(idTrainInd, 2);
cv.put(TIindicator, "Remate SEM Sucesso");
db.insert(TrainingIndicatorTable, idTrainInd, cv);


//THEN I USE THIS TO RETURN GET THE DATA BUT TABLE TWO IS STILL EMPTY

/**Get all players from local database*/
public Cursor getPlayers() {

 //   db = this.getReadableDatabase();

    Cursor c=db.rawQuery("SELECT player FROM Attendancetabl", null);

    //c.close();
    return c;

}






/**Get all players from local database*/
public Cursor getIndicadors() {

   // db = this.getReadableDatabase();

    Cursor c=db.rawQuery("SELECT indicator FROM TrainingIndicador", null);

    //c.close();


    return c;

}

这是第一个问题的同样问题。

2 个答案:

答案 0 :(得分:0)

尝试使用支持代码:

private void insertData(SQLiteDatabase base, String table, String columnHack, ContentValues values) {
    base.beginTransaction();
    base.insertWithOnConflict(table, columnHack, values, SQLiteDatabase.CONFLICT_REPLACE);
    base.setTransactionSuccessful();
    base.endTransaction();
}

然后只需调用你的db.insert这一个代码:

insertData(db, TrainingIndicatorTable, idTrainInd, cv);

答案 1 :(得分:0)

内容值插入的代码片段如下所示。

 private ContentValues initialValues;
    initialValues = new ContentValues();                    
                        initialValues.put("User_ID", User_ID);                      
                        initialValues.put("FirstName", FirstName);
                        initialValues.put("LastName", LastName);
                        initialValues.put("UserID",  UserID));
                        initialValues.put("Password",Password1);
                        initialValues.put("Email", Email);
                       mydb.insertTitle(initialValues,"CheckLogin"); 

//这将是您插入值的数据库类或方法。希望这能帮助你。