Sqlitedatabase implements access to SQLite

Source: Internet
Author: User
Tags sqlite database

Access to the SQLite database via Sqlitedatabase

----Main.java

public class main extends activity {sqlitedatabase db; listview listview; edittext edittext1, edittext2;  //the title and contextbutton button; to add @Overrideprotected  void oncreate (bundle savedinstancestate)  {super.oncreate (savedInstanceState); Setcontentview (R.layout.main);listview =  (ListView)  findviewbyid (r.id.listview1); editText1  =  (EditText)  findviewbyid (R.ID.EDITTEXT1);edittext2 =  (EditText)  findviewbyid ( R.ID.EDITTEXT2);button =  (Button)  findviewbyid (r.id.button1);// /data/data/ Com.example.dbtest/files --/my.db3  db = sqlitedatabase.openorcreatedatabase ( This.getfilesdir (). toString () +  "/my.db3",  null); Button.setonclicklistener (New onclicklistener ()  {@Overridepublic  void onclick (view v)  {String str1 =  Edittext1.gettext (). toString (); String str2 = edittext2.getteXT (). toString (); Try {inserttodb (DB, STR1, STR2); Cursor cursor = db.rawquery ("Select * from news_info", null); InflateListView ( cursor);}  catch  (sqliteexception e)  {db.execsql ("Create table news_info (_id integer  primary key autoincrement, "+ " News_title varchar (a), "+ " news_content  varchar (255)); Inserttodb (DB, STR1, STR2); Cursor cursor = db.rawquery ("Selecte * from news_info", null); InflateListView ( cursor);}});} Private void inserttodb (SQLITEDATABASE DB, STRING STR1, STRING STR2)  { Db.execsql ("Insert into news_info values (null, ?, ?)",  new string[] { str1, str2 });} Private void inflatelistview (cursor cursor)  {SimpleCursorAdapter adapter =  New simplecursoradapter (Main.this,r.layout.item, cursor, new&nbsP string[] {  "News_title", "News_content"  }, new int[] { r.id.textview1, R.id.textview2 },cursoradapter.flag_register_content_observer); Listview.setadapter (adapter);} @Overrideprotected  void ondestroy ()  {super.ondestroy (); if (Db!=null&&db.isopen ()) { Db.close ();}}}

Simplecursoradapter the cursor is encapsulated, the column name that requires the primary key column of the data table is _id. Because the Simplecursoradapter can only recognize columns named

The primary key of the _id. Otherwise you will get an error.

As with the Java operation JDBC, the database is finally closed db.close (); to reclaim resources.

---main.xml

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"      xmlns:tools= "Http://schemas.android.com/tools"     android:layout_width= "Match_parent"     android:layout_height= "Match_parent"     tools:context= "${ Relativepackage}.${activityclass} " >    <EditText         android:id= "@+id/edittext1"         android:layout_ Width= "Wrap_content"         android:layout_height= "Wrap_content"          android:layout_alignparentleft= "true"          android:layout_alignparenttop= "true"         android: Ems= "Ten"  >        <requestFocus />     </edittext> &Nbsp;  <edittext        android:id= "@+id/editText2"          android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android: Layout_alignparentleft= "true"         android:layout_below= "@+id/ EditText1 "        android:ems="  />     <button        android:id= "@+id/button1"          android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android:layout_ Alignparentleft= "true"         android:layout_below= "@+id/editText2"         android:text= "Inserting data"  />    <ListView         android:id= "@+id/listview1"         android:layout_width= " Match_parent "        android:layout_height=" Wrap_content "         android:layout_alignparentleft= "true"          android:layout_below= "@+id/button1"  >    </listview></ Relativelayout>

Listeview the layout file for each subkey Item.xml:

<?xml version= "1.0"  encoding= "Utf-8"? ><relativelayout xmlns:android= "http// Schemas.android.com/apk/res/android "    android:layout_width=" Match_parent "     android:layout_height= "Match_parent"  >    <TextView         android:id= "@+id/textview1"          android:layout_width= "Wrap_content"         android:layout_height = "Wrap_content"         android:layout_alignparentleft= "true"          android:layout_alignparenttop= "true"          android:text= "TextView"  />    <TextView         android:id= "@+id/textview2"          Android:layout_width= "Wrap_content"         android:layout_height= "Wrap_content"          android:layout_alignparenttop= "true"         android:layout _marginleft= "20DP"         android:layout_torightof= "@+id/textView1"         android:text= "TextView"  /></RelativeLayout>

Operating effect:


Sqlitedatabase implements access to SQLite

Related Article

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.