android-database Operation A

Source: Internet
Author: User
Package com.yarin.android.Examples_06_05;


Import android.app.Activity;
Import android.content.ContentValues;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import android.view.KeyEvent;
Import Android.widget.LinearLayout;
Import Android.widget.ListAdapter;
Import Android.widget.ListView;
Import Android.widget.SimpleCursorAdapter;


public class Activity01 extends activity {
private static int micount = 0;


/* Database Objects/*
Private sqlitedatabase msqlitedatabase = null;


/* Database name * *
Private final static String database_name = "EXAMPLES_06_05.DB";
/* Table name * *
Private final static String table_name = "Table1";


/* fields in table * *
Private final static String table_id = "_id";
Private final static String Table_num = "NUM";
Private final static String Table_data = "DATA";


/* Create a table SQL statement * *
Private final static String create_table = "CREATE TABLE" + table_name + ("+ table_id +" INTEGER PRIMARY KEY, "
+ Table_num + "Interger," + Table_data + "TEXT";


/* Linear Layout * *
LinearLayout m_linearlayout = null;
/* List View-display data in the database * *
ListView M_listview = null;


/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);


/* Create LinearLayout Layout Object * *
M_linearlayout = new LinearLayout (this);
/* Set the properties of the layout linearlayout * *
M_linearlayout.setorientation (linearlayout.vertical);
M_linearlayout.setbackgroundcolor (Android.graphics.Color.BLACK);


/* Create ListView Object * *
M_listview = new ListView (this);
Linearlayout.layoutparams param = new Linearlayout.layoutparams (LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
M_listview.setbackgroundcolor (Color.Black);


/* Add M_listview to m_linearlayout layout * *
M_linearlayout.addview (M_listview, param);


/* Set Display m_linearlayout layout * *
Setcontentview (m_linearlayout);


Open a database that already exists
Msqlitedatabase = this.openorcreatedatabase (database_name, mode_private, NULL);


Get the database phones cursor
try {
/* Create a table in database msqlitedatabase * *
Msqlitedatabase.execsql (create_table);
catch (Exception e) {
Updataadapter ();
}
}


public boolean onKeyUp (int keycode, keyevent event) {
Switch (keycode) {
Case Keyevent.keycode_dpad_left:
AddData ();
Break
Case Keyevent.keycode_dpad_right:
DeleteData ();
Break
Case Keyevent.keycode_1:
Updata ();
Break
Case keyevent.keycode_2:
Deletetable ();
Break
Case Keyevent.keycode_3:
DeleteDatabase ();
Break
}
return true;
}


/* Delete Database * *
public void DeleteDatabase () {
This.deletedatabase (database_name);
This.finish ();
}


/* Delete a table * *
public void deletetable () {
Msqlitedatabase.execsql ("DROP TABLE" + table_name);
This.finish ();
}


/* Update a piece of data * *
public void Updata () {
Contentvalues CV = new Contentvalues ();
Cv.put (Table_num, Micount);
Cv.put (Table_data, "modified data" + Micount);


/* Update Data * *
Msqlitedatabase.update (TABLE_NAME, CV, table_num + "=" + integer.tostring (miCount-1), null);


Updataadapter ();
}


/* Add a piece of data to the table * *
public void AddData () {
Contentvalues CV = new Contentvalues ();
Cv.put (Table_num, Micount);
Cv.put (table_data, "test database Data" + Micount);
/* Insert Data * *
Msqlitedatabase.insert (table_name, NULL, CV);
micount++;
Updataadapter ();
}


/* Delete the specified data from the table.
public void DeleteData () {


/* Delete Data * *
Msqlitedatabase.execsql ("DELETE from" + table_name + "WHERE _id=" + integer.tostring (micount));


micount--;
if (Micount < 0) {
Micount = 0;
}
Updataadapter ();
}


/* More lines trying to show * *
public void Updataadapter () {
Get the database phones cursor
Cursor cur = msqlitedatabase.query (table_name, new string[] {table_id, table_num, table_data}, NULL, NULL,
NULL, NULL, NULL);


Micount = Cur.getcount ();
if (cur!= null && cur.getcount () >= 0) {
ListAdapter is a bridge between ListView and backstage data.
ListAdapter adapter = new Simplecursoradapter (This,
Define the display template for each row in the list
Indicates that each row contains two data items
Android. R.layout.simple_list_item_2,
Cursor Object for Database
Cur
Fetching data from the Table_num and table_data two columns of the database
New string[] {table_num, table_data},
Views corresponding to name and number
New int[] {Android. R.id.text1, Android. R.ID.TEXT2});


* * Add adapter to M_listview.
M_listview.setadapter (adapter);
}
}


/* Key Event Processing * *
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back) {
/* When exiting, do not forget to close * *
Msqlitedatabase.close ();
This.finish ();
return true;
}
Return Super.onkeydown (KeyCode, event);
}
}
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.