java.lang.IllegalStateException: couldn ' t read row 0, col-1 from Cursorwindow. Make sure the Cursor was initialized correctly before accessing data from it.Sqlitedatabase During the learning process, when querying form data using query (), you encounter couldn ' t read row 0, col-1 from Cursorwindow error. excludes table field errors, misspellings, and query () input fields. When I use the Rawquery () method instead of the query () method, the problem is solved. The code is as follows: 1. Build table Mydatabasehelper
1 Private Static FinalString create_contacts = "CREATE table Contact ("2+ "ID integer primary key autoincrement,"3+ "Name text,"4+ "Number text");5 6 PublicMydatabasehelper (context context, String name, Cursorfactory factory,intversion) {7 Super(context, name, Factory, version);8 }9 Ten @Override One Public voidonCreate (Sqlitedatabase db) { A Db.execsql (create_contacts); -}
2.Query a form based on Searchname
1 Private voidsearchcontact (String searchname) {2db =dbhelper.getreadabledatabase ();3 //cursor cursor = db.query ("Contact", new string[] {"Name"}, "name =?", new string[] {searchname}, NULL, NULL, NULL); /c5>4cursor cursor = db.rawquery ("SELECT * from contact where name =?",Newstring[] {searchname});5 if(Cursor.movetofirst ()) {6 //String name = cursor.getstring (Cursor.getcolumnindex ("name"));7String number = cursor.getstring (Cursor.getcolumnindex ("number"));8 Tv_name.settext (searchname);9 Tv_number.settext (number);TenTel = "Tel:" +Number ; One}Else { ATv_name.settext ("404! Not Found "); -Tv_number.settext ("10086"); -Tel = "tel:10086"; the } - cursor.close (); - db.close (); -}
1. When using Query (string table, string[] columns, string selection, string[] Selectionargs, String groupBy, String having, string by)
String name = cursor.getstring (Cursor.getcolumnindex ("name"))
With this code test you can get to name and prove that the cursor is pointing correctly to the data you want to query.
String number = cursor.getstring (Cursor.getcolumnindex ("number"))
but this code will be reported couldn ' t read row 0, col-1 from Cursorwindow.
Getcolumnindex ("number") The return value is-1, baffled!
2. Using rawquery(String sql, String[] selectionargs)
both the name and number fields are accurate and the problem is solved perfectly.
Reprint Please specify source: http://www.cnblogs.com/michaelwong/p/4128299.html
Couldn ' t read row 0, col-1 from Cursorwindow