Couldn ' t read row 0, col-1 from Cursorwindow

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.