Simple application of Wang Liping--sqlite,sqliteopenhelper

Source: Internet
Author: User
Tags sqlite

The Android platform provides us with a database helper class to create or open a database that inherits from the Sqliteopenhelper class. In the constructor for the class, call the method in the context to create and open a database object with the specified name. The main task of inheriting and extending the Sqliteopenhelper class is to rewrite the following two methods.

public class Mysqlitehelper extends Sqliteopenhelper {
Public Mysqlitehelper (Context context, String name, Cursorfactory factory,
int version) {
Super (context, name, Factory, version);
TODO auto-generated Constructor stub
}
This method is run when the database is first created. Generally speaking, create tables and so on initialization work here
Execsql Creating a Table
@Override
public void OnCreate (Sqlitedatabase db) {
TODO auto-generated Method Stub
Create a table
String sql= "CREATE table if not exists hero_info (" + "ID of integer primary key," + "name varchar," + "level integer)";
Db.execsql (SQL);
}
@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
TODO auto-generated Method Stub

}

Mainactivity.java

Package Com.example.sqllite;
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.widget.TextView;
Public class Mainactivity extends Activity {
private TextView TV;
private mysqlitehelper H;


@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
init ();
    }
Public void Init () {
     tv= (TextView) Findviewbyid (r.id.tv);
    h=new Mysqlitehelper (This, "my.db", NULL, 1);
    insertandupdatedata (h);
    //Query data
    String result = Querydata (h);
    Tv.settextcolor (color.red);
    tv.settextsize (20.0f);
    Tv.settext ("The name \ t level \ n" +result);
   
   
    }
    
 
private void Insertandupdatedata (Mysqlitehelper helper) {
//TODO auto-generated method stub

//calling the Getreadabledatabase method returns a database object that is not always read-only.
//in general, the method and the return of the Getwriteabledatabase method are the same
//Only read-only database objects are returned when the database is open only for read-only permissions or when the disk is full.

    Sqlitedatabase db = Helper.getwritabledatabase ();
    //Use the Execsql method to insert data into the table
    Db.execsql ("INSERT into Hero_info (name,level) VALUES (' BB ', 0)");
    //Insert data into a table using the Insert method
    contentvalues values = new Contentvalues ();
    values.put ("name", "XH");
    values.put ("level", 5);
    //Call method to insert data
    Db.insert ("Hero_info", "id", values);
    //Update the data in the table using the Update method
    //emptying the Contentvalues object
    values.clear ();
    values.put ("name", "XH");
    values.put ("level", ten);
    //Update XH at level
    db.update ("Hero_info", values, "level = 5", null);
    //Close Sqlitedatabase object
    db.close ();


//Querying data from the database
Public String Querydata (mysqlitehelper myhelper) {
String result = "";
//Get database Objects
Sqlitedatabase db = Myhelper.getreadabledatabase ();
//Query the data in the table
cursor cursor = db.query ("Hero_info", NULL, NULL, NULL, NULL, NULL, "ID ASC");
//Gets the index of the name column
int nameindex = Cursor.getcolumnindex ("name");
//Gets the index of the level column
int levelindex = Cursor.getcolumnindex ("level");
For (Cursor.movetofirst ();! (Cursor.isafterlast ()); Cursor.movetonext ()) {
result = result + cursor.getstring (nameindex) + "\t\t";
result = result + Cursor.getint (levelindex) + "\ n";
}
cursor.close ();//close result set
db.close ();//Close database objects
return result;
}
@Override
protected void OnDestroy () {
Sqlitedatabase db = H.getwritabledatabase ();//Get Database object
//Delete all data in Hero_info table 1 means delete all rows------> click Backbutton
db.delete ("Hero_info", "1", null);
Super.ondestroy ();
}


Simple application of Wang Liping--sqlite,sqliteopenhelper

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.