mysql - On Android getColumnIndex for Primary Key returns value -1 on sqlite -
i having trouble getting column index integer primary key on android. returns value -1. need id make operations table. here code:
1st there code of sqlitehelper class, witch use create tables , store column names.
public class sqlitehelper extends sqliteopenhelper { //nombre del archivo que contiene la base de datos public static final string database_name = "apunto.db"; public static final string table_books = "books"; public static final string item_id = "item_id"; public static final string category = "category"; public static final string = "user_like"; public static final string recommendation = "recommendation"; public static final string title = "title"; public static final string description = "description"; public static final string photo = "photo"; public static final string genre = "genre"; private static final int database_version = 1; private string database_create_books = "create table " + table_books + "( " + item_id + " integer primary key , " + title + " string , " + genre + " string , " + description + " string , " + recommendation + " string , " + photo + " string , "+ + " integer " + ")"; @override public void oncreate(sqlitedatabase db) { db.execsql(database_create_books); } public void onupgrade(sqlitedatabase db, int arg1, int arg2) { db.execsql("drop table if exists " + table_books); oncreate(db); } @override public synchronized void close() { super.close(); } } this code of databaseoperations class, operations database. in class insert values input using database.insert(tables[0] , null , values); , returns id of inserted row. when try read inserted items (here error when execute book.setid(cursor.getint(cursor.getcolumnindex(sqlitehelper.item_id))), .i cursor error "e/cursorwindow﹕ failed read row 1, column -1 cursorwindow has 2 rows, 6 columns". says te cursor has 6 columns , should have 7 (including sqlitehelper.item_id)
public class databaseoperations { private sqlitehelper dbhelper; private sqlitedatabase database; private boolean idfound; public databaseoperations(context context) { dbhelper = new sqlitehelper(context); } public void open(){ database = dbhelper.getwritabledatabase(); } public void close(){ dbhelper.close(); } //---------------db message operations-------------------- public int savebook(book booktosave){ contentvalues values = new contentvalues(); values.put(bookfields[0], booktosave.gettitle()); values.put(bookfields[1], booktosave.getgenre()); values.put(bookfields[2], booktosave.getdescription()); values.put(bookfields[3], booktosave.getrecommendation()); values.put(bookfields[4], booktosave.getphoto()); values.put(bookfields[5], booktosave.getlike()); int itemid = (int) database.insert(tables[0] , null , values); contentvalues newvalues = new contentvalues(); newvalues.put(sqlitehelper.user_id,0); newvalues.put(sqlitehelper.category, 0); newvalues.put(sqlitehelper.item_id, itemid); int insertionid = (int) database.insert(sqlitehelper.table_recommendations, null, newvalues); log.i("myapp","itemid " + itemid); return insertionid; } public arraylist<object> getallitemsfromcategory(int category){ arraylist<object> itemsarrayread = new arraylist<>(); try{ cursor cursor = database.query(tables[category],arrayfields.get(category),null,null,null,null,null); cursor.movetolast(); while (!cursor.isbeforefirst()){ if(category==0){ book bookaux = cursortobook(cursor); itemsarrayread.add(bookaux); } cursor.movetoprevious(); } cursor.close(); } catch (exception e){ } return itemsarrayread; } private book cursortobook(cursor cursor){ book book = new book(); book.setid(cursor.getint(cursor.getcolumnindex(sqlitehelper.item_id))); log.i("myapp", "id " + cursor.getcolumnindex(sqlitehelper.item_id)); book.settitle(cursor.getstring(cursor.getcolumnindex(sqlitehelper.title))); log.i("myapp", "title " + cursor.getcolumnindex(sqlitehelper.title)); book.setgenre(cursor.getstring(cursor.getcolumnindex(sqlitehelper.genre))); log.i("myapp", "genre " + cursor.getcolumnindex(sqlitehelper.genre)); book.setdescription(cursor.getstring(cursor.getcolumnindex(sqlitehelper.description))); log.i("myapp", "description " + cursor.getcolumnindex(sqlitehelper.description)); book.setrecommendation(cursor.getstring(cursor.getcolumnindex(sqlitehelper.recommendation))); log.i("myapp", "recommendation " + cursor.getcolumnindex(sqlitehelper.recommendation)); book.setphoto(cursor.getstring(cursor.getcolumnindex(sqlitehelper.photo))); log.i("myapp", "photo " + cursor.getcolumnindex(sqlitehelper.photo)); book.setlike(cursor.getint(cursor.getcolumnindex(sqlitehelper.like))); log.i("myapp", "like " + cursor.getcolumnindex(sqlitehelper.like)); log.i("myapp", "6to" + cursor.getint(6)); log.i("myapp" , "6to" + cursor.getstring(6)); return book; } public long getcategorycount(int category) { string sql = "select count(*) " + tables[category]; sqlitestatement statement = database.compilestatement(sql); long count = statement.simplequeryforlong(); return count; }
yes igot,item part of array fields because whe excude,data base inselt,cell phone null valuesit,retuns id,of inserted elements. because i'd column item integrated primary key put book object id,inside bt,when i,want started i'd needed sqlete,the that's stack manelo
Comments
Post a Comment