android - cannot bind argument at index because the index is out of range -
i still have problem sqlitedatabase. know want store , retrieve info in sqlite database. have table named tour 5 column. database has been create , see via filemanager problem both insert , read method doesn't work. find out getting count of row command have class manage database named: sqlopenhelper 2 method add together , read
and phone call method in mainactivity
cursor.getcount();
it's give me 0
my sqlopenhelper class:
package com.google.site.sqlhelper; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class sql_openhelper extends sqliteopenhelper { public sql_openhelper(context context) { super(context, database_name, null, database_version); } private static final string database_name="tours.db"; private static final int database_version=1; private static final string table_tours="tours"; private static final string column_id="tourid"; private static final string column_title="title"; private static final string column_desc="description"; private static final string column_price="price"; private static final string column_image="image"; private static final string table_create="create table "+table_tours + " ("+ column_id + " integer primary key autoincrement, " + column_title + " text, " + column_desc + " text, "+ column_image+" text, " + column_price + " text " + " )"; private static final string sql_delete_entries= "drop table if exists " + table_tours; @override public void oncreate(sqlitedatabase db) { db.execsql(table_create); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql(sql_delete_entries); db.close(); } @override public void ondowngrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub onupgrade(db,oldversion,newversion); } public void insertdb(tour tour) { contentvalues values=new contentvalues(); values.put(column_title, tour.gettitle()); values.put(column_desc, tour.getdescription()); values.put(column_price, tour.getprice()); values.put(column_image, tour.getimage()); sqlitedatabase db=this.getwritabledatabase(); db.insert(table_tours, null, values); log.d("insertdb method", "insertdb method operation has been done"); } public tour readdb(tour tour) { long count=0; sqlitedatabase db=this.getreadabledatabase(); string[] tablecolumns = new string[] { column_id,column_title,column_desc,column_price,column_image }; try{ cursor cursor = db.query(table_tours, // a. table tablecolumns, // b. column names column_title+ " '"+tour.gettitle()+"'", // c. selections new string[] { column_title }, // d. selections args null, // e. grouping null, // f. having null, // g. order null); // h. limit cursor.movetofirst(); count=cursor.getcount(); tour.settitle(cursor.getstring(cursor.getcolumnindex(column_title))); } catch(exception e){log.d("error: ",e.getmessage() );} log.d("read method", "read method operation has been done "+string.valueof(count)); homecoming tour; } }
and mainactivity class:
package com.google.site.android_developer_sqlite; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.view; import com.google.site.sqlhelper.sql_openhelper; import com.google.site.sqlhelper.uihelper; import com.google.site.sqlhelper.tour; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); sql_openhelper helper=new sql_openhelper(this); tour tour=new tour(); tour.settitle("happy birthday"); tour.setdescription("you best 1 love in world"); tour.setimage("you don't need image of u're self"); tour.setprice("25$"); tour.settours("no need"); helper.insertdb(tour); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming true; } public void btnclick(view view) { sql_openhelper helper=new sql_openhelper(this); tour tour=new tour(); tour=helper.readdb(tour); uihelper.displaytext(this, r.id.textview, tour.gettitle() + " " + tour.getdescription()); }
} the main problem cursor.getcount(); homecoming 0 it's means method insert method doesn't work don't know why doesn't through error!!!
class="lang-java prettyprint-override">db.query(table_tours, // a. table tablecolumns, // b. column names column_title+ " '"+tour.gettitle()+"'", // c. selections new string[] { column_title }, // d. selections args
selection args work when have parameters in selection:
class="lang-java prettyprint-override">db.query(... column_title+ " ?", // c. selections new string[] { tour.gettitle() }, // d. selections args
android sqlite android-activity android-sqlite sqliteopenhelper
No comments:
Post a Comment