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