Department of Famous Door Android (9)

Source: Internet
Author: User
Tags final sqlite

Introduced

Using SQLite in Android, ContentProvider

Database Support (SQLite)-The Android development platform provides an API for manipulating SQLite databases

Content provider (ContentProvider)-when data needs to be shared between applications, you can use ContentProvider to define URIs in a program so that other applications can access the specified data through this URI

1, SQLite's Demo

Databasehelper.java

Package com.webabcd.SQLite;

Import Android.content.Context;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteOpenHelper;
Import Android.database.sqlite.SQLiteDatabase.CursorFactory;

Helper Classes for database operations
public class Databasehelper extends Sqliteopenhelper {

Databasehelper (context context, String name, cursorfactory cursorfactory, int version) {
Super (context, name, cursorfactory, version);
}

@Override
public void OnCreate (Sqlitedatabase db) {
TODO operations on a database after the database is created
}

@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
TODO Change the operation of the database version
}

@Override
public void OnOpen (Sqlitedatabase db) {
Super.onopen (DB);

TODO first executes each time the database is successfully opened
}
}

Main.java

Package com.webabcd.SQLite;

Import Java.util.Random;

Import android.app.Activity;
Import android.content.ContentValues;
Import Android.database.Cursor;
Import android.database.SQLException;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TextView;

public class Main extends activity {

Private Databasehelper DBHelper;

private static final String database_name= "db.db";
private static final int database_version=1;
private static final String table_name= "employee";

TextView txtmsg;

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

Dbhelper=new Databasehelper (this, database_name, NULL,
Database_version);

Txtmsg= (TextView) This.findviewbyid (r.id.txtmsg);

Button btn1= (button) This.findviewbyid (R.ID.BTN1);
Btn1.settext ("CREATE TABLE");
Btn1.setonclicklistener (New Button.onclicklistener () {
public void OnClick (View v) {
CreateTable ();
}
});

Button btn2= (button) This.findviewbyid (R.ID.BTN2);
Btn2.settext ("Insert 3 Records");
Btn2.setonclicklistener (New Button.onclicklistener () {
public void OnClick (View v) {
InsertItem ();
}
});

Button btn3= (button) This.findviewbyid (R.ID.BTN3);
Btn3.settext ("Delete all Records");
Btn3.setonclicklistener (New Button.onclicklistener () {
public void OnClick (View v) {
DeleteItem ();
}
});

Button btn4= (button) This.findviewbyid (R.ID.BTN4);
Btn4.settext ("Update specified data");
Btn4.setonclicklistener (New Button.onclicklistener () {
public void OnClick (View v) {
UpdateItem ();
}
});

Button btn5= (button) This.findviewbyid (R.ID.BTN5);
Btn5.settext ("Show All Data");
Btn5.setonclicklistener (New Button.onclicklistener () {
public void OnClick (View v) {
Showitems ();
}
});

Button btn6= (button) This.findviewbyid (R.ID.BTN6);
Btn6.settext ("delete table");
Btn6.setonclicklistener (New Button.onclicklistener () {
public void OnClick (View v) {
Droptable ();
}
});
}

Creating data Tables
private void CreateTable () {
Sqlitedatabase db=dbhelper.getwritabledatabase ();
String sql= "CREATE TABLE IF not EXISTS" + table_name
+ "(ID integer PRIMARY KEY, Name VARCHAR, age INTEGER);";
try {
Db.execsql (SQL);
Txtmsg.append ("data table successfully created \ n");
catch (SQLException ex) {
Txtmsg.append ("Data table creation error \ n" + ex.tostring () + "\ n");
}
}

Inserting data
private void InsertItem () {
Sqlitedatabase db=dbhelper.getwritabledatabase ();

try {
Random random=new Random ();
for (int i=0 i < 3; i++) {
String sql= "INSERT INTO" + table_name
+ "(name, age) VALUES (' name ' + string.valueof (i))
+ "'," + random.nextint () + ")";
Execsql ()-Executes the specified SQL
Db.execsql (SQL);
}
Txtmsg.append ("successfully insert 3 data \ n");
catch (SQLException ex) {
Txtmsg.append ("Insert data failed \ n" + ex.tostring () + "\ n");
}
}

Delete data
private void DeleteItem () {
try {
Sqlitedatabase db=dbhelper.getwritabledatabase ();
Db.delete (TABLE_NAME, "ID < 999999", NULL);
Txtmsg.append ("successfully delete data \ n");
catch (SQLException e) {
Txtmsg.append ("delete data failed \ n");
}
}

Update data
private void UpdateItem () {
Sqlitedatabase db=dbhelper.getwritabledatabase ();

try {
Contentvalues values=new contentvalues ();
Values.put ("Name", "batch updated names");

Db.update (table_name, values, "ID&LT;?", New string[] {"3"});
Txtmsg.append ("Successfully update data \ n");
catch (SQLException e) {
Txtmsg.append ("Update data failed \ n");
}
}

Querying data
private void Showitems () {
Sqlitedatabase db=dbhelper.getreadabledatabase ();

try {
String[] column={"id", "name", "Age"};
Cursor cursor=db.query (table_name, column, NULL, NULL, NULL,
NULL, NULL);
Integer Num=cursor.getcount ();
Txtmsg.append ("Total" + integer.tostring (num) + "record \ n");
Cursor.movetofirst ();

while (Cursor.getposition ()!= Cursor.getcount ()) {
Txtmsg.append (Integer.tostring (Cursor.getposition ()) + ","
+ string.valueof (cursor.getstring (0)) + ","
+ cursor.getstring (1) + ","
+ string.valueof (cursor.getstring (2)) + "\ n");
Cursor.movetonext ();
}
catch (SQLException ex) {
Txtmsg.append ("read data failed \ n" + ex.tostring () + "\ n");
}
}

Delete data table
private void droptable () {
Sqlitedatabase db=dbhelper.getwritabledatabase ();
String sql= "DROP TABLE IF EXISTS" + table_name;
try {
Db.execsql (SQL);
Txtmsg.append ("Data table deletion succeeded \ n");
catch (SQLException ex) {
Txtmsg.append ("Data table deletion error \ n" + ex.tostring () + "\ n");
}
}
}

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.