Use SQLite database (a) to build a table in Android

Source: Internet
Author: User

First, implement a class, inherit the Sqliteopenhelper class, and implement the constructor, OnCreate () Onupgrade ()

Import Android.content.context;import Android.database.databaseerrorhandler;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.database.sqlite.sqliteopenhelper;public class Dbopenhelper extends Sqliteopenhelper {private static String Nam        E = "mydb.db";//indicates the name of the database private static int version = 2;//indicates the version number of the database public Dbopenhelper (context context) {    Super (context, name, null, version); }//When the database is created, it is executed for the first time, completing the creation of tables on the database @Override public void onCreate (Sqlitedatabase db) {//TODO Auto-genera  Ted method stubs//Supported data types: Integer data, String type, date type, binary data type, String sql = "CREATE table person (ID integer PRIMARY key        Autoincrement,name varchar (+), address varchar (64)) ";    Db.execsql (SQL);  } @Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//TODO auto-generated     Method stub String sql = "ALTER TABLE person add sex varchar (8)";   Db.execsql (SQL); }}

Call OnCreate () only if you call Getwritabledatabase () or getreadabledatabase ()


Import Com.example.android_db.db.dbopenhelper;import Android.test.androidtestcase;public class MyTest extends androidtestcase {public    MyTest () {        //TODO auto-generated constructor stubs    } public          void Createdb () { C4/>dbopenhelper helper = new Dbopenhelper (GetContext ());        Helper.getwritabledatabase ();    }}


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.