Android SQLite basic operation

Source: Internet
Author: User

Create a new Class (Ex:userdbhelper) inheritance Sqliteopenhelper

  • Constructor built-in database
      Public Userdbhelper (context context, int version) {        Super (context, db_name,null,version);    }
  • OnCreate method inside the table is built
       /**     * is only called when the database is generated, generally we will     build a table in this method     * @param sqlitedatabase */    @Override public    Void OnCreate (Sqlitedatabase sqlitedatabase) {        log.d (TAG, "onCreate:");        String drop_table = "drop table IF EXISTS" + table_name + ";";        LOG.D (TAG, "oncreate:drop_table_sql" + drop_table);        Sqlitedatabase.execsql (drop_table);        String create_sql = "Create TABLE IF not EXISTS" + table_name + "(" +                "_id INTEGER PRIMARY KEY autoincrement not NUL L, ' +                ' name VARCHAR NOT NULL, ' + ' age INTEGER not NULL, ' +                ' height LONG not null, ' + ' weight FLOAT not null, ' +< c15/> "married INTEGER not NULL," + "update_time varchar is not NULL" +                 ", phone VARCHAR, password varchar" +                ");";        log.d (TAG, "crate_table sql:" +create_sql);        Sqlitedatabase.execsql (Create_sql);    }
  • Onupgrade method inside is to upgrade the database table
    @Override public    void Onupgrade (sqlitedatabase sqlitedatabase, int oldversion, int newversion) {        log.d (TAG, " onupgrade:oldversion= "+oldversion+" newversion: "+newversion);        if (NewVersion > 1) {            String alertsql = "ALTER TABLE" + table_name + "ADD COLUMN phone VARCHAR;";            LOG.D (TAG, "onupgrade:alertsql=" +alertsql);            Sqlitedatabase.execsql (alertsql);            Alertsql = "ALTER TABLE" + table_name + "ADD COLUMN password VARCHAR;";            LOG.D (TAG, "onupgrade:alertsql=" +alertsql);            Sqlitedatabase.execsql (Alertsql);        }    }


    Package com.safeluck.floatwindow.db;

    Import android.content.ContentValues;
    Import Android.content.Context;
    Import Android.database.sqlite.SQLiteDatabase;
    Import Android.database.sqlite.SQLiteOpenHelper;
    Import Android.util.Log;

    Import java.util.List;

    /**
    * AAA
    * Created by LZW on 2018/8/23. 15:28:33
    * Email: [Email protected]
    * All Rights saved! Chongqing Anyun Tech Co. LTD
    */
    public class Userdbhelper extends Sqliteopenhelper {
    private static final String db_name = "UserDB";
    private static final int db_version = 1;
    private static Userdbhelper Mhelper;
    Private static final String table_name = "user";
    private static final String TAG = "Userdbhelper";
    Private sqlitedatabase DB = null;
    Public Userdbhelper (context context, int version) {
    Super (context, db_name,null,version);
    }

    public static Userdbhelper getinstance (Context context,int version) {
    if (version>0 && mhelper = = null) {
    Mhelper = new Userdbhelper (context,version);
    }else if (mhelper = = null) {
    Mhelper = new Userdbhelper (context,db_version);
    }
    return mhelper;
    }


    /**
    * Get a read-only database
    * @return
    */
    Public Sqlitedatabase Getreadlink () {
    DB = Mhelper.getreadabledatabase ();
    return DB;
    }

    Public Sqlitedatabase Getwritedb () {
    DB = Mhelper.getwritabledatabase ();
    return DB;
    }

    /**
    * Called only when the database is generated, generally we will
    * The table is built in this method
    * @param sqlitedatabase
    */
    @Override
    public void OnCreate (Sqlitedatabase sqlitedatabase) {
    LOG.D (TAG, "onCreate:");
    String drop_table = "drop table IF EXISTS" + table_name + ";";
    LOG.D (TAG, "oncreate:drop_table_sql" + drop_table);
    Sqlitedatabase.execsql (drop_table);

    String create_sql = "Create TABLE IF not EXISTS" + table_name + "(" +
    "_id INTEGER PRIMARY KEY autoincrement not NULL," +
    ' name VARCHAR NOT NULL, ' + ' age ' INTEGER not NULL, ' +
    "Height LONG is not null," + "weight FLOAT is not null," +
    ' Married INTEGER not null, ' + ' update_time VARCHAR not NULL ' +
    ", phone varchar, password varchar" +
    ");";
    LOG.D (TAG, "crate_table sql:" +create_sql);
    Sqlitedatabase.execsql (Create_sql);
    }

    @Override
    public void Onupgrade (sqlitedatabase sqlitedatabase, int oldversion, int newversion) {
    LOG.D (TAG, "onupgrade:oldversion=" +oldversion+ "newversion:" +newversion);
    if (NewVersion > 1) {
    String alertsql = "ALTER TABLE" + table_name + "ADD COLUMN phone VARCHAR;";
    LOG.D (TAG, "onupgrade:alertsql=" +alertsql);
    Sqlitedatabase.execsql (Alertsql);
    Alertsql = "ALTER TABLE" + table_name + "ADD COLUMN password VARCHAR;";
    LOG.D (TAG, "onupgrade:alertsql=" +alertsql);
    Sqlitedatabase.execsql (Alertsql);

    }

    }

    /**
    * Delete a row of data
    * @param condition
    */
    public void DeleteRow (String condition) {
    Db.delete (Table_name,condition,null);
    }


    Public long AddUser (list<userinfo> Userinfos) {
    Long reuslt =-1;
    for (int i = 0; i < userinfos.size (); i++) {
    UserInfo info = userinfos.get (i);
    Contentvalues CV = new Contentvalues ();
    Cv.put ("name", Info.name);
    Cv.put ("Age", info.age);
    Cv.put ("height", info.height);
    Cv.put ("Weight", info.weight);
    Cv.put ("Married", Info.married);
    Cv.put ("Update_time", info.update_time);
    Cv.put ("Phone", info.phone);
    Cv.put ("password", Info.password);
    REUSLT = Db.insert (table_name, "", CV);
    }
    LOG.D (TAG, "adduser:result=" +reuslt);
    return reuslt;
    }

    }

Android SQLite basic operation

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.