Simple application of Wang Liping--sqlite,sqliteopenhelper

Source: Internet
Author: User
Tags sqlite stub

The Android platform provides us with a database helper class to create or open a database, which inherits from the Sqliteopenhelper class, in which the constructor of the class invokes a method in the context to create and open a database object of 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 executed when the database is first created, where initialization such as creating tables is typically done 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 does not always return a read-only database object .
//in general, this method is the same as the return of the Getwriteabledatabase method,
//A read-only database object is returned only if 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 the Hero_info table 1 means delete all rows------> Click the Back button
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.