android - SQLiteException: no such column: _id: for ContentProvider -


i declared _id auto increment column in contentprovider running on sqlite, , yet following "no such column: _id" error message in stacktrace, how fix error?

 fatal exception: asynctask  java.lang.runtimeexception: error occured while executing doinbackground()  caused by: android.database.sqlite.sqliteexception: no such column: _id:,  while compiling: select _id, title, text notes 

another strange thing did not call query method of contentprovider class anywhere in code , yet got location of error line of code in query method of contentprovider class, if contentprovider's query method not being called should not in stacktrace, is, here tells me location of error in stacktrace

com.example.contentproviderexample.providerexample.query(providerexample.java:132) 

contentprovider class providerexample line 132

cursor c = qb.query(db, projection, selection, selectionargs, null, null, sortorder); 

edit: after suggested uninstall or delete data app , run again, after did got different error stacktrace, here is:

 couldn't open notes.db writing (will try read-only):  android.database.sqlite.sqliteexception:  autoincrement allowed on integer primary key:  , while compiling: create table if not exists  notes (_id id integer primary key autoincrement, title text, text text); 

and here complete query method of contentprovider class

 @override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) {     sqlitequerybuilder qb = new sqlitequerybuilder();     qb.settables(table_name);     qb.setprojectionmap(notesprojectionmap);          switch(surimatcher.match(uri)){          case notes:              break;          case notes_id:              selection = selection + "_id=" + uri.getlastpathsegment();              break;          default:              throw new illegalargumentexception("unknown uri" + uri);              }          sqlitedatabase db = dbhelper.getreadabledatabase();          cursor c = qb.query(db, projection, selection, selectionargs, null, null, sortorder); // <-- no colunn _id error here          c.setnotificationuri(getcontext().getcontentresolver(), uri);          return c 

here more of code providerexample class extends contentprovider

 public class providerexample extends contentprovider {  public static final string database_name = "notes.db"; public static final int database_version = 1; public static final string table_name = "notes"; public static final string authority = "com.example.contentproviderexample.providerexample"; public static final urimatcher surimatcher; private static final int notes = 1; private static final int notes_id = 2; private static hashmap<string, string> notesprojectionmap;   static {         surimatcher = new urimatcher(urimatcher.no_match);         surimatcher.adduri(authority, table_name, notes);         surimatcher.adduri(authority, table_name + "/#", notes_id);     }   public static interface noteitems extends basecolumns {          // notes in content_uri plural actual table name singular version of word         public static final uri content_uri = uri.parse("content://" + authority + "/notes");         public static final string _id = "_id";         public static final string title = "title";         public static final string text = "text";         public static final string content_type = contentresolver.cursor_dir_base_type + "/vnd.example.providerexample";         // static final string single_record = "vnd.android.cursor.item/vnd.example.providerexample";         public static final string content_item_type = contentresolver.cursor_item_base_type + "/vnd.example.providerexample";         // static final string multiple_records = "vnd.android.cursor.dir/vnd.example.providerexample";          public static final string[] projection_all = {_id, title, text};         public static final string sort_order_default = title + " asc";   }   private static class databasehelper extends sqliteopenhelper{       databasehelper(context context){          super(context, database_name, null, database_version);      }       @override      public void oncreate(sqlitedatabase db) {      db.execsql("create table if not exists " + table_name + " (" + noteitems._id + " id integer primary key autoincrement, " +                noteitems.title + " text, " + noteitems.text + " text);");      }       @override      public void onupgrade(sqlitedatabase db, int previousversion, int newversion) {      db.execsql("drop table if exists " + table_name);      oncreate(db);      }   }  private databasehelper dbhelper;  @override public boolean oncreate() {     dbhelper = new databasehelper(getcontext());     return true; }  @override public string gettype(uri uri) {     switch (surimatcher.match(uri)){         case notes:             return noteitems.content_type;         case notes_id:             return noteitems.content_item_type;         default:             throw new illegalargumentexception("unknown uri" + uri);     }  }  

and here mainactivity class

   public class mainactivity extends activity implements loadermanager.loadercallbacks<cursor>{  private simplecursoradapter adapter; private loadermanager loadermanager; private cursorloader cursorloader; private listview listview; private int primarykey; private string primarykeystring; private string testtitle; private string testtext; private static final int loader_id = 1;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      primarykey = 0;     testtitle = "test title";     testtext = "test text";      providerexample providerexample = new providerexample();      primarykeystring = string.valueof(primarykey);     getloadermanager().initloader(loader_id, null, this);     adapter = new simplecursoradapter(this, r.layout.row_layout, null,             new string[]{primarykeystring, testtitle, testtext}, new int[]{r.id.textview1, r.id.textview2, r.id.textview3},             adapter.no_selection);     listview = (listview) findviewbyid(r.id.listview1);     listview.setadapter(adapter);      // load database test values     //loaddatabase();  }  @override public loader<cursor> oncreateloader(int id, bundle args) {     string[] projection = {providerexample.noteitems._id, providerexample.noteitems.title, providerexample.noteitems.text };     cursorloader = new cursorloader(this, providerexample.noteitems.content_uri, projection, null, null, null);     return cursorloader; }  @override public void onloadfinished(loader<cursor> loader, cursor cursor) {     adapter.swapcursor(cursor); }  @override public void onloaderreset(loader<cursor> loader) {     adapter.swapcursor(null); }  } 

i think while creating table doing mistake. alredy gave " id " inside quotes plus having noteitems._id makes "_id id " primary id. may need correct it..!!

 @override  public void oncreate(sqlitedatabase db) {  db.execsql("create table if not exists " + table_name + " (" + noteitems._id + " id integer primary key autoincrement, " +            noteitems.title + " text, " + noteitems.text + " text);");  } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -