Android 열 '_id'가 존재하지 않습니까?
메모장 예제에서 작동하는 문제가 있습니다. NotepadCodeLab / Notepadv1Solution의 코드는 다음과 같습니다.
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.notes_row, c, from, to);
이 코드는 잘 작동하는 것 같습니다. 그러나 명확하게하기 위해 ADB 유틸리티를 실행하고 SQLite 3을 실행했습니다. 스키마를 다음과 같이 검사했습니다.
sqlite> .schema
CREATE TABLE android_metadata (locale TEXT);
CREATE TABLE notes (_id integer primary key autoincrement, title text
not null, body text not null);
모든 것이 나에게 좋은 것 같습니다.
이제 내 응용 프로그램에 대해 살펴 보겠습니다. 제가 볼 수있는 한 몇 가지 사소한 변경만으로 기본적으로 동일합니다. 코드를 단순화하고 단순화했지만 문제가 지속됩니다.
String[] from = new String[] { "x" };
int[] to = new int[] { R.id.x };
SimpleCursorAdapter adapter = null;
try
{
adapter = new SimpleCursorAdapter(this, R.layout.circle_row, cursor, from, to);
}
catch (RuntimeException e)
{
Log.e("Circle", e.toString(), e);
}
내 응용 프로그램을 실행할 때 RuntimeException이 발생하고 내 Log.e()
문 에서 LogCat에 다음과 같은 인쇄가 표시됩니다 .
LogCat 메시지 :
java.lang.IllegalArgumentException : '_id'열이 없습니다.
따라서 SQLite 3으로 돌아가서 내 스키마의 차이점을 확인하십시오.
sqlite> .schema CREATE TABLE android_metadata (로케일 TEXT); CREATE TABLE 서클 (_id 정수 기본 키 자동 증가, 시퀀스 정수, 반경 실수, x 실수, y 실수);
'_id'가 어떻게 누락되었는지 모르겠습니다.
내가 뭘 잘못 했어?
내 응용 프로그램과 메모장 예제의 차이점 중 하나는 샘플 응용 프로그램이 이미 함께 제공되는 동안 Eclipse 마법사를 사용하여 처음부터 응용 프로그램을 만드는 것으로 시작했다는 것입니다. SQLite 데이터베이스를 사용하기 위해 새 응용 프로그램에서 수행해야하는 일종의 환경 변경이 있습니까?
CursorAdapter에 대한 문서는 다음과 같이 말합니다.
Cursor는 이름
_id
이 지정된 열을 포함해야합니다 . 그렇지 않으면이 클래스가 작동하지 않습니다.
는 SimpleCursorAdapter
파생 클래스이므로이 문이 적용됩니다. 그러나이 진술은 기술적으로 잘못되어 초보자에게 다소 오해의 소지가 있습니다. 커서 의 결과 세트_id
에는 커서 자체가 아니라 를 포함해야합니다 .
DBA에게는 이것이 분명합니다. 왜냐하면 그러한 종류의 속기 문서가 그들에게는 분명하기 때문입니다. 그러나 그 초보자들에게는 성명서가 불완전하면 혼란을 야기합니다. 커서는 반복기 또는 포인터와 비슷하며 데이터를 가로 지르는 메커니즘 만 포함하고 열 자체는 포함하지 않습니다.
로우더 문서 가 있음이 알 수있는 일례 포함 _id
에 포함되는 투사 파라미터.
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME,
Contacts.CONTACT_STATUS,
Contacts.CONTACT_PRESENCE,
Contacts.PHOTO_ID,
Contacts.LOOKUP_KEY,
};
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// ...
return new CursorLoader(getActivity(), baseUri,
CONTACTS_SUMMARY_PROJECTION, select, null,
Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
This has been answered and I would like to make it more comprehensive here.
SimpleCursorAdapter requires that the Cursor's result set must include a column named exactly "_id". Don't haste to change schema if you didn't define the "_id" column in your table. SQLite automatically added an hidden column called "rowid" for every table. All you need to do is that just select rowid explicitly and alias it as '_id' Ex.
SQLiteDatabase db = mHelper.getReadableDatabase();
Cursor cur = db.rawQuery( "select rowid _id,* from your_table", null);
Tim Wu's code really works...
If you are using db.query, then it would be like this...
db.query(TABLE_USER, new String[] {
"rowid _id",
FIELD_USERNAME,
},
FIELD_USERNAME + "=" + name,
null,
null,
null,
null);
Yes , I also change the SELECT string query to fix this issue.
String query = "SELECT t.*,t.id as _id FROM table t ";
What solved my issue with this error was that I had not included the _id column in my DB query. Adding that solved my problem.
This probably isn't relevant anymore, but I just hit the same problem today. Turns out column names are case sensitive. I had an _ID column, but Android expects an _id column.
If you read the docs on sqlite, creating any column of type INTEGER PRIMARY KEY will internally alias the ROWID, so it isn't worth the trouble of adding an alias in every SELECT, deviating from any common utilities that might take advantage of something like an enum of columns defining the table.
http://www.sqlite.org/autoinc.html
It is also more straightforward to use this as the ROWID instead of the AUTOINCREMENT option which can cause _ID can deviate from the ROWID. By tying _ID to ROWID it means that the primary key is returned from insert/insertOrThrow; if you are writing a ContentProvider you can use this key in the returned Uri.
Another way of dealing with the lack of an _id column in the table is to write a subclass of CursorWrapper which adds an _id column if necessary.
This has the advantage of not requiring any changes to tables or queries.
I have written such a class, and if it's of any interest it can be found at https://github.com/cmgharris/WithIdCursorWrapper
참고URL : https://stackoverflow.com/questions/3359414/android-column-id-does-not-exist
'program tip' 카테고리의 다른 글
0과 1 사이의 신속한 임의 부동 (0) | 2020.10.25 |
---|---|
Swift 사전을 콘솔에 예쁘게 인쇄하는 방법이 있습니까? (0) | 2020.10.25 |
jQuery없이 node.js에서 JSON 결합 또는 병합 (0) | 2020.10.25 |
$ date + 1 년? (0) | 2020.10.25 |
Windows 서비스를 설치할 수 없습니다. (0) | 2020.10.25 |