我知道有很多关于这个问题的帖子,但我找不到解决方案。所以我很抱歉重复。
这是我的数据库代码:
public static final String DATABASE_NAME = "MyGames.db";
public static final String TABEL_CONSOLE = "Consol_table";
public static final String COL_CONSOLE_ID = "_id";
public static final String COL_CONSOLE_NAME = "NAME";
public static final String COL_CONSOLE_PRODUCER = "PRODUCER";
public static final String COL_CONSOLE_GENERATION = "GENERATION";
public static final String COL_CONSOLE_TYPE = "TYPE";
public static final String COL_CONSOLE_PUBLICATION = "PUBLICATION";
public static final String COL_CONSOLE_ADDED = "ADDED";
public static final String TABEL_VIDEOGAME = "Videogame_table";
public static final String COL_VIDEOGAME_ID = "_id";
public static final String COL_VIDEOGAME_NAME = "NAME";
public static final String COL_VIDEOGAME_GENRE = "GENRE";
public static final String COL_VIDEOGAME_DEVELOPER = "DEVELOPER";
public static final String COL_VIDEOGAME_PRODUCER = "PRODUCER";
public static final String COL_VIDEOGAME_PUBLICATION = "PUBLICATION";
public static final String COL_VIDEOGAME_ADDED = "ADDED";
public static final String TABEL_GENRE = "Genere_table";
public static final String COL_GENRE_ID = "_id";
public static final String COL_GENRE_NAME = "NAME";
public static final String TABEL_VIDEOGAME_PLATFORM = "Videogameplatform_table";
public static final String COL_VIDEOGAME_PLATFORM_ID = "_id";
public static final String COL_VIDEOGAME_PLATFORM_VIDEOGAME_ID = "VIDEOGAME_id";
public static final String COL_VIDEOGAME_PLATFORM_CONSOLE_ID = "CONSOLE_id";
public DataBase(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
createConsoleTable(db);
createVideoGameTable(db);
createGenreTable(db);
createVideoGamePlatformTable(db);
}
private void createVideoGamePlatformTable(SQLiteDatabase db) {
db.execSQL("create table " + TABEL_VIDEOGAME_PLATFORM + "(" +
COL_VIDEOGAME_PLATFORM_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COL_VIDEOGAME_PLATFORM_VIDEOGAME_ID + " INTEGER NOT NULL," +
COL_VIDEOGAME_PLATFORM_CONSOLE_ID + " INTEGER NOT NULL," +
"FOREIGN KEY( " + COL_VIDEOGAME_PLATFORM_VIDEOGAME_ID + " ) REFERENCES " + TABEL_VIDEOGAME + " (" + COL_VIDEOGAME_ID + " )" +
"FOREIGN KEY( " + COL_VIDEOGAME_PLATFORM_CONSOLE_ID + " ) REFERENCES " + TABEL_CONSOLE + " (" + COL_CONSOLE_ID + " )" +
");");
db.execSQL("INSERT INTO " + TABEL_VIDEOGAME_PLATFORM + "(" +
COL_VIDEOGAME_PLATFORM_VIDEOGAME_ID + "," +
COL_VIDEOGAME_PLATFORM_CONSOLE_ID + ")" +
"VALUES " +
"('0', '0')," +
"('0', '2')," +
"('0', '7')," +
"('1', '0')," +
"('1', '2')," +
"('1', '7')," +
"('2', '2')," +
"('3', '6')," +
"('4', '9')," +
"('5', '0')," +
"('5', '2')," +
"('5', '7');"
);
}
private void createGenreTable(SQLiteDatabase db) {
db.execSQL("create table " + TABEL_GENRE + "(" +
COL_GENRE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COL_GENRE_NAME + " TEXT(40) NOT NULL" +
");");
db.execSQL("INSERT INTO " + TABEL_GENRE + "(" +
COL_GENRE_NAME + ")" +
"VALUES " +
"('GDR'), " +
"('FPS'), " +
"('TPS'), " +
"('Platform')" +
";"
);
}
private void createVideoGameTable(SQLiteDatabase db) {
db.execSQL("create table " + TABEL_VIDEOGAME + "(" +
COL_VIDEOGAME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COL_VIDEOGAME_NAME + " TEXT(40) NOT NULL," +
COL_VIDEOGAME_GENRE + " INTEGER," +
COL_VIDEOGAME_DEVELOPER + " TEXT(25)," +
COL_VIDEOGAME_PRODUCER + " TEXT(25)," +
COL_VIDEOGAME_PUBLICATION + " TEXT(15)," +
COL_VIDEOGAME_ADDED + " INTEGER," +
"FOREIGN KEY( " + COL_VIDEOGAME_GENRE + " ) REFERENCES " + TABEL_GENRE + " (" + COL_GENRE_ID + " )" +
");");
db.execSQL("INSERT INTO " + TABEL_VIDEOGAME + " (" +
COL_VIDEOGAME_NAME + "," +
COL_VIDEOGAME_GENRE + "," +
COL_VIDEOGAME_DEVELOPER + "," +
COL_VIDEOGAME_PRODUCER + "," +
COL_VIDEOGAME_PUBLICATION + ","+
COL_CONSOLE_ADDED + ")" +
"VALUES " +
"('Mass Effect', '0', 'BioWare', 'Eletronic Arts ','2007', '0')," +
"('Mass Effect 2', '0', 'BioWare', 'Eletronic Arts ','2011', '0')," +
"('Uncharted 3', '2', 'NaugtyDog', 'Sony ','2012', '0')," +
"('Halo 5', '1', 'Bungie', 'Microsoft','2015', '0')," +
"('Mario', '3', 'Nintendo', 'Nintendo ','2015', '0')," +
"('Mass Effect 3', '0', 'BioWare', 'Eletronic Arts ','2013', '0')" +
";"
);
}
private void createConsoleTable(SQLiteDatabase db) {
db.execSQL("create table " + TABEL_CONSOLE + "( " +
COL_CONSOLE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COL_CONSOLE_NAME + " TEXT(40) NOT NULL," +
COL_CONSOLE_PRODUCER + " TEX(40)," +
COL_CONSOLE_GENERATION + " TEXT(15)," +
COL_CONSOLE_TYPE + " TEXT(15)," +
COL_CONSOLE_PUBLICATION + " TEXT(15)," +
COL_CONSOLE_ADDED + " INTEGER" +
");");
db.execSQL("INSERT INTO " + TABEL_CONSOLE + " ( " +
COL_CONSOLE_NAME + "," +
COL_CONSOLE_PRODUCER + "," +
COL_CONSOLE_GENERATION + "," +
COL_CONSOLE_TYPE + "," +
COL_CONSOLE_PUBLICATION + "," +
COL_CONSOLE_ADDED + ")" +
"VALUES ('PC', ' ', ' ', ' ', ' ', '0')," + //0
"('Playstation 4', 'Sony', 'Octave', 'Home', ' ', '0')," + //1
"('Playstation 3', 'Sony', 'Seventh', 'Home', ' ', '0')," + //2
"('Playstation 2', 'Sony', 'Sixth', 'Home', ' ', '0')," + //3
"('Playstation 1', 'Sony', 'fifth', 'Home', ' ', '0')," + //4
"('Playstation Vita', 'Sony', 'Octave', 'Home', ' ', '0')," + //5
"('XboxOne', 'Microsoft', 'Octave', 'Home', ' ', '0')," + //6
"('Xbox 360', 'Microsoft', 'Seventh', 'Home', ' ', '0')," + //7
"('WiiU', 'Nintendo', 'Octave', 'Home', ' ', '0')," + //8
"('Wii', 'Nintendo', 'Seventh', 'Home', ' ', '0')," + //9
"('Playstation Portable', 'Sony', 'Seventh', 'Home', ' ', '0')" + //10
";"
);
}
这是调用SimpleCursorAdapter的地方:
ListView consoleGames = (ListView) popupView.findViewById(R.id.listView_all_games);
Cursor cursor = myDb.getConsoleGames(console);
String[] fromFieldNames = new String[]{DataBase.COL_VIDEOGAME_NAME};
int[] toViewsIDs = new int[]{R.id.textViewGameName};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(popupView.getContext(), R.layout.item_all_games_list, cursor, fromFieldNames, toViewsIDs, 0);
consoleGames.setAdapter(myCursorAdapter);
这是查询
public Cursor getConsoleGames(String console) {
SQLiteDatabase db = this.getWritableDatabase();
final String query1 = "select " + COL_CONSOLE_ID + " from " + TABEL_CONSOLE + " WHERE " + COL_CONSOLE_NAME + " like " + "'" + console + "'";
final String query2 = "select " + COL_VIDEOGAME_PLATFORM_VIDEOGAME_ID + " from " + TABEL_VIDEOGAME_PLATFORM + " WHERE " + COL_VIDEOGAME_PLATFORM_CONSOLE_ID + " = ( " + query1 + ")";
String query = "select " + COL_VIDEOGAME_NAME + " from " + TABEL_VIDEOGAME + " WHERE " + COL_VIDEOGAME_ID + " = ( " + query2 + ");" ;
Cursor res = db.rawQuery(query, null);
return res;
}
这是LogCat:
12-14 12:14:14.894 6607-6607/com.tutecentral.mygames E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tutecentral.mygames, PID: 6607
java.lang.IllegalArgumentException: column '_id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:151)
at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:93)
at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:89)
at com.tutecentral.mygames.MainActivity.popUpAddGameEvent(MainActivity.java:157)
at com.tutecentral.mygames.MainActivity.showPopUpAddGame(MainActivity.java:143)
at com.tutecentral.mygames.MainActivity.access$200(MainActivity.java:40)
at com.tutecentral.mygames.MainActivity$2.onClick(MainActivity.java:124)
at android.view.View.performClick(View.java:4820)
at android.view.View$PerformClick.run(View.java:20157)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5578)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
我该如何解决这个问题?
答案 0 :(得分:2)
问题在于您的查询。您使用的是CursorAdapter
,它希望找到列_id
作为您正在使用它的光标的一部分。将其添加到您的查询中。 E.g。
String query = "select " + COL_VIDEOGAME_ID + "," + COL_VIDEOGAME_NAME + " from " + TABEL_VIDEOGAME + " WHERE " + COL_VIDEOGAME_ID + " = ( " + query2 + ");" ;