Android アプリを作成し、データベースを使用しています。私が使用しているテーブルの 1 つは学期テーブルで、次のステートメントを使用して作成されます。
final String createSemesterTable =
"CREATE TABLE IF NOT EXISTS" + semesterTable +
"( " + _id + " INTEGER PRIMARY KEY AUTOINCREMENT," +
semesterName + " TEXT," +
isCurrent + " INTEGER," +
GPA + " REAL" + ");";
簡単なバージョン:
CREATE TABLE IF NOT EXISTS Semesters(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
semester_name TEXT,
is_current TEXT
GPA REAL);
私の主な活動では、データベースからのデータを使用して項目を埋める、リスト ビューにリンクするカーソル アダプターを作成しようとします。
これが私の onCreate(..) メソッドの一部です:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
dbase = new DBHelper(getBaseContext());
sqldbase = dbase.getWritableDatabase();
ListView list = (ListView)findViewById(R.id.semesterList);
String columns[] = {DBHelper._id, DBHelper.semesterName, DBHelper.isCurrent, DBHelper.GPA};
String from[] = {DBHelper.semesterName, DBHelper.GPA};
int to[] = {R.id.semesterName, R.id.semesterGPA};
Cursor cursor = sqldbase.query(DBHelper.semesterTable, columns, null, null, null, null, null);
adap = new SimpleCursorAdapter(getBaseContext(),R.layout.semester_list_view,cursor, columns, to);
list.setAdapter(adap);
}
ただし、コンパイル/実行すると、次のメッセージが表示されます。
07-06 20:34:25.950: E/AndroidRuntime(2825): java.lang.RuntimeException: Unable to start activity
android.database.sqlite.SQLiteException: no such column: _id (code 1): ,
while compiling: SELECT _id, Semester_Name, Is_Current, GPA FROM Semesters
_id 列 (実際には SQLite で必要) を使用しているため、何が問題なのかわかりません。また、なぜ _id 列が (その名前で) 必要なのですか? 複数のテーブルがある場合、主キーに異なる名前を設定できますか?それともそれぞれに _id が必要ですか?
ありがとう。