您好,我正在使用此代码将数据插入到我的数据库中。在另一个活动中,我使用光标,我可以单独查看 table1 或 table2 的数据。但如果我同时使用,我只能看到表 2 的数据。
当我使用游标获取数据时,我看到一个是空的。我想我没有正确插入数据。
我究竟做错了什么?
内容值 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);
//表2
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;
}
和第一个问题一样。