I have a ListView and I wanted to display my cursor into it, but the app crashes when I execute the code :
Caused by: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT DISTINCT _id, membername, memberstudylevel, memberbirthday FROM members
This is my code for the database's class:
package tn.iac.myapp.application10;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.List;
public class
MyBDHelter extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 4;
private static final String DATABASE_NAME = "base_de_membre.db";
public static final String TABLE_MEMBERS = "members";
public static final String COLUMN_MEMBERID = "_id";
public static final String COLUMN_MEMBERNAME = "membername";
public static final String COLUMN_MEMBERSTUDYLEVEL = "memberstudylevel";
public static final String COLUMN_EMAIL = "memberemail";
public static final String COLUMN_MEMBERBIRTHDAY = "memberbirthday";
public static final String COLUMN_PASSWORD = "password";
public MyBDHelter(Context context, String name,SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_MEMBERS + "("
+COLUMN_MEMBERID +"INTEGER PRIMARY KEY,"
+ COLUMN_MEMBERNAME + " TEXT,"
+ COLUMN_MEMBERSTUDYLEVEL + " TEXT, "
+ COLUMN_MEMBERBIRTHDAY + " TEXT, "
+ COLUMN_EMAIL + " TEXT, "
+ COLUMN_PASSWORD + " TEXT);";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBERS);
onCreate(db);
}
public Cursor getallusers() {
SQLiteDatabase db = getReadableDatabase();
String[] strings = {"_id"," membername", " memberstudylevel", "memberbirthday"};
Cursor c=db.query(
true,
TABLE_MEMBERS,
strings,
"",
null,
null,
null,
null,
null
);
if(c!=null){
c.moveToFirst();}
db.close();
return c;
}
public void addmember(Membre membre) {
ContentValues values = new ContentValues();
values.put(COLUMN_MEMBERNAME, membre.get_name());
values.put(COLUMN_MEMBERBIRTHDAY, membre.get_birth());
values.put(COLUMN_MEMBERSTUDYLEVEL, membre.get_study_level());
values.put(COLUMN_PASSWORD, membre.get_password());
values.put(COLUMN_EMAIL, membre.get_Email());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_MEMBERS, null, values);
db.close();
}
public boolean exist(String name, String password) {
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_MEMBERS + " WHERE " + COLUMN_MEMBERNAME + "=\"" + name + "\" AND " + COLUMN_PASSWORD + "=\"" + password + "\";";
Cursor c = db.rawQuery(query, null);
if (c.getCount() >= 1)
return true;
return false;
}
public void changepassword(String password, String oldpassword, String name) {
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_MEMBERS + " SET " + COLUMN_PASSWORD + "=\"" + password + "\" WHERE " + COLUMN_MEMBERNAME + "=\"" + name + "\"AND "
+ COLUMN_PASSWORD + "=\"" +
oldpassword + "\";";
db.execSQL(query);
db.close();
}
public Membre databasetostring(String password) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_MEMBERS, new String[]{COLUMN_MEMBERNAME,
COLUMN_EMAIL, COLUMN_MEMBERSTUDYLEVEL, COLUMN_MEMBERBIRTHDAY, COLUMN_PASSWORD}, COLUMN_PASSWORD + "=?",
new String[]{password.toString()}, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Membre membre = new Membre(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
return membre;
}
}
And this is my code to show the ListView:
package tn.iac.myapp.application10;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class List_user extends ActionBarActivity {
ListView listView;
MyBDHelter bdHelter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_user);
listView = (ListView) findViewById(R.id.listView);
bdHelter = new MyBDHelter(getApplicationContext(), null, null, 4);
Cursor cursor = bdHelter.getallusers();
int[] toviews = {R.id.user_id_item, R.id.name_item_user, R.id.studylevell_user_item, R.id.birthday_userèitem};
String[] strings = {"_id", " membername", "memberstudylevel", "memberbirthday"};
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.user_item, cursor,
strings, toviews);
listView.setAdapter(simpleCursorAdapter);
}}
P.S.: I declared the primary key "_id", and I incremented the database's version, but the problem still exists
Error:
Caused by: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT DISTINCT _id, membername, memberstudylevel, memberbirthday FROM members
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at tn.iac.myapp.application10.MyBDHelter.getallusers(MyBDHelter.java:51)
at tn.iac.myapp.application10.List_user.onCreate(List_user.java:19)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:1)
You need a space here:
+COLUMN_MEMBERID +"INTEGER PRIMARY KEY,"
has to be
+COLUMN_MEMBERID +" INTEGER PRIMARY KEY,"
THEN increment the DATABASE_VERSION constant.
(Or uninstall and then reinstall your app)
答案 1 :(得分:-1)
Caused by: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT DISTINCT _id, membername, memberstudylevel, memberbirthday FROM members
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at tn.iac.myapp.application10.MyBDHelter.getallusers(MyBDHelter.java:51)
at tn.iac.myapp.application10.List_user.onCreate(List_user.java:19)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)