-1

So I am trying out a tutorial for SQLite I have seen in the internet, and after tweaking it a bit I launched it and it crashes. Before modifying the code, it works. I know that some of the functions are deprecated, but that didn't seem to be the issue because as I said it ran before I modified it. I also know that the error is on line 44 on one of my classes (I have put a comment in this line to distinguish it) but I don't see why it gives me an error. Here is my code:

AndroidSQLite.java:

package com.example.sqlite;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class AndroidSQLite extends Activity {

 EditText inputContent1, inputContent2, inputContent3;
 Button buttonAdd, buttonDeleteAll;

 private SQLiteAdapter mySQLiteAdapter;
 ListView listContent;

 SimpleCursorAdapter cursorAdapter;
 Cursor cursor;

   /** Called when the activity is first created. */
@Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       inputContent1 = (EditText)findViewById(R.id.UserID);
       inputContent2 = (EditText)findViewById(R.id.Password);
       inputContent3 = (EditText)findViewById(R.id.Fname);
       buttonAdd = (Button)findViewById(R.id.add);
       buttonDeleteAll = (Button)findViewById(R.id.deleteall);

       listContent = (ListView)findViewById(R.id.contentlist);

       mySQLiteAdapter = new SQLiteAdapter(this);
       mySQLiteAdapter.openToWrite();

       cursor = mySQLiteAdapter.queueAll();
       String[] from = new String[]{SQLiteAdapter.KEY_USERS_USERID, SQLiteAdapter.KEY_USERS_PASSWORD, SQLiteAdapter.KEY_USERS_FNAME};
       int[] to = new int[]{R.id.id, R.id.text1, R.id.text2};
       cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
       listContent.setAdapter(cursorAdapter);

       buttonAdd.setOnClickListener(buttonAddOnClickListener);
       buttonDeleteAll.setOnClickListener(buttonDeleteAllOnClickListener);

   }

   Button.OnClickListener buttonAddOnClickListener = new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   String data1 = inputContent1.getText().toString();
   String data2 = inputContent2.getText().toString();
   String data3 = inputContent3.getText().toString();
   mySQLiteAdapter.insert(data1, data2, data3);
   updateList();
  }

   };

   Button.OnClickListener buttonDeleteAllOnClickListener = new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   mySQLiteAdapter.deleteAll();
   updateList();
  }

   };

 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  mySQLiteAdapter.close();
 }

 @SuppressWarnings("deprecation")
private void updateList(){
  cursor.requery();
   }

}

SQLiteAdapter.java

package com.example.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SQLiteAdapter {

 public static final String DB_NAME = "adserve";
 public static final String DB_TABLE_USERS = "users";
 public static final int DB_VERSION = 1;
 public static final String KEY_USERS_USERID = "_id";
 public static final String KEY_USERS_PASSWORD = "Password";
 public static final String KEY_USERS_FNAME = "Fname";

 //create table MY_DATABASE (ID integer primary key, Content text not null);
 private static final String SCRIPT_CREATE_DATABASE =
  "create table " + DB_TABLE_USERS + " ("
  + KEY_USERS_USERID + " integer primary key, "
  + KEY_USERS_PASSWORD + " text not null, "
  + KEY_USERS_FNAME + " text not null);";

 private SQLiteHelper sqLiteHelper;
 private SQLiteDatabase sqLiteDatabase;

 private Context context;

 public SQLiteAdapter(Context c){
  context = c;
 }

 public SQLiteAdapter openToRead() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, DB_NAME, null, DB_VERSION);
  sqLiteDatabase = sqLiteHelper.getReadableDatabase();
  return this; 
 }

 public SQLiteAdapter openToWrite() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, DB_NAME, null, DB_VERSION);
  sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  return this; 
 }

 public void close(){
  sqLiteHelper.close();
 }

 public long insert(String UserID, String Password, String Fname){

  ContentValues contentValues = new ContentValues();
  contentValues.put(KEY_USERS_USERID, UserID);
  contentValues.put(KEY_USERS_PASSWORD, Password);
  contentValues.put(KEY_USERS_FNAME, Fname);
  return sqLiteDatabase.insert(DB_TABLE_USERS, null, contentValues);
 }

 public int deleteAll(){
  return sqLiteDatabase.delete(DB_TABLE_USERS, null, null);
 }

 public Cursor queueAll(){
  String[] columns = new String[]{KEY_USERS_USERID, KEY_USERS_PASSWORD, KEY_USERS_FNAME};
  Cursor cursor = sqLiteDatabase.query(DB_TABLE_USERS, columns, null, null, null, null, null);

  return cursor;
 }

 public class SQLiteHelper extends SQLiteOpenHelper {

  public SQLiteHelper(Context context, String name, CursorFactory factory, int version) {
   super(context, name, factory, version);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
   // TODO Auto-generated method stub
   db.execSQL(SCRIPT_CREATE_DATABASE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // TODO Auto-generated method stub
  }
 } 
}

And my logcat:

02-20 10:04:53.811: E/AndroidRuntime(1940): FATAL EXCEPTION: main
02-20 10:04:53.811: E/AndroidRuntime(1940): Process: com.example.sqlite, PID: 1940
02-20 10:04:53.811: E/AndroidRuntime(1940): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlite/com.example.sqlite.AndroidSQLite}: java.lang.IllegalArgumentException: column '_id' does not exist
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.os.Looper.loop(Looper.java:137)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.app.ActivityThread.main(ActivityThread.java:4998)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at java.lang.reflect.Method.invokeNative(Native Method)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at java.lang.reflect.Method.invoke(Method.java:515)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at dalvik.system.NativeStart.main(Native Method)
02-20 10:04:53.811: E/AndroidRuntime(1940): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.widget.CursorAdapter.init(CursorAdapter.java:172)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.widget.CursorAdapter.<init>(CursorAdapter.java:120)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at com.example.sqlite.AndroidSQLite.onCreate(AndroidSQLite.java:43)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.app.Activity.performCreate(Activity.java:5243)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-20 10:04:53.811: E/AndroidRuntime(1940):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-20 10:04:53.811: E/AndroidRuntime(1940):     ... 11 more
Saint-Clair
  • 59
  • 3
  • 10

2 Answers2

3

Quoting the documentation for CursorAdapter:

The Cursor must include a column named "_id" or this class will not work

You do not have such a column in your Cursor, as far as I can tell.

One solution would be to switch from query() to rawQuery(), so you can add ROWID AS _id to your list of columns, to fulfil the CursorAdapter contract.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

The SQLite Site says,

In SQLite, table rows normally have a 64-bit signed integer ROWID which is unique among all rows in the same table. (WITHOUT ROWID tables are the exception.)

So if you want to use a column only as an integer primary key, you can directly access the in built one using ROWID.

and the document on Cursor adapter says,

The Cursor must include a column named "_id" or this class will not work. Additionally, using MergeCursor with this class will not work if the merged Cursors have overlapping values in their "_id" columns.

which is the error you are getting.

Also if searched, you could find solution to yours on already answered SO questions like this one.

Community
  • 1
  • 1
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74