Common errors with Android SQLite

Source: Internet
Author: User

The use of leisure to write a simple SQL statement operation SQLite database, in the use of Simplecursoradapter an exception for a long time have not solved



Process:com.example.chunchuner.usesqltest405, pid:31206
Java.lang.IllegalArgumentException:column ' _id ' does not exist

Discover through a series of search data

Use Simplecursoradapter to encapsulate the cursor when the cursor requires that the column name of the underlying database's primary key column be _id
Otherwise, a java.lang.IllegalArgumentException exception will occur.

The code is as follows:
Package Com.example.nanchen.usesqltest405;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqliteexception;import Android.os.Bundle; Import Android.support.v7.app.appcompatactivity;import Android.view.view;import Android.widget.button;import Android.widget.cursoradapter;import Android.widget.edittext;import Android.widget.listview;import    Android.widget.simplecursoradapter;public class Mainactivity extends appcompatactivity {private Button insert;    Private EditText textview_title,textview_content;    Private Sqlitedatabase DB;    Private ListView ListView;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Create or open a database (you need to use absolute path here) db = Sqlitedatabase.openorcreatedatabase (This.getfilesdir (). toString () + "/MY.DB3", null);        Insert = (Button) Findviewbyid (R.id.insert); ListView = (ListView) FindviewByid (R.id.listview);        Textview_title = (EditText) Findviewbyid (R.id.title_text);        Textview_content = (EditText) Findviewbyid (r.id.content);                Insert.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {                String title = Textview_title.gettext (). toString ();                String content = Textview_content.gettext (). toString ();                try {insertdata (db, title, content);  }catch (sqliteexception e) {//Execute CREATE DATABASE table Db.execsql ("CREATE Table News_inf (_id integer                    Primary key AutoIncrement, "+" News_title varchar2,news_content varchar2) ";                InsertData (DB, title, content);                } cursor cursor = db.rawquery ("SELECT * from News_inf", null);            Inflatelist (cursor);    }        }); } private void InsertData (Sqlitedatabase db,string Title,string content) {db.execsql ("INSERT into news_inf values (null,?,?)", New String[]{title, content}); } private void Inflatelist (cursor cursor) {//Fill Simplecursoradapter//Use Simplecursoradapter to encapsulate cursor when curso R requires the column name of the primary key column for the underlying database to be _id//or Java.lang.IllegalArgumentException exception will occur simplecursoradapter adapter = new SIMPLECU Rsoradapter (this, Android. R.layout.simple_list_item_1,cursor, new string[]{"News_title", "News_content"}, New Int[]{r.i        D.title_text,r.id.content}, Cursoradapter.flag_register_content_observer);    Display Data Listview.setadapter (adapter);        } @Override protected void OnDestroy () {Super.ondestroy ();        When exiting the program, close Sqlitedatabase if (db! = null && db.isopen ()) {db.close (); }    }}

  



Common errors with Android SQLite

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.