The primary use and unit test examples of SQLite

Source: Internet
Author: User

package com.gxa.db;public class user {    private int id;     private String name;    private String pwd;     private int age;    private String sex;     public user (int id, string name, string pwd, int age ,  string sex)  {        this.id = id;         this.name = name;         this.pwd = pwd;        this.age = age;         this.sex = sex;    }     public user (String name, string pwd, int age, string sex)  {        this.name = name;         this.pwd = pwd;        this.age = age;         this.sex = sex;    }     Public int getid ()  {        return id;     }    public void setid (Int id)  {         this.id = id;    }    public  String getname ()  {        return name;     }    public void setname (String name)  {         this.name = name;    }     Public string getpwd()  {        return pwd;    }     public void setpwd (STRING PWD)  {         this.pwd = pwd;    }    public int getage ()  {        return age;    }     public void setage (int age)  {         This.age = age;    }    public string getsex ()  {        return sex;    }     public void setsex (String sex)  {         this.sex = sex;    }     @Override      public string tOstring ()  {        return  "user [id="  + id  +  ",  name="  + name +  ",  pwd="  + pwd +  ",  age = "                + age  +  ",  sex="  + sex +  "]";     }    }
Package com.gxa.db;import android.content.context;import android.database.sqlite.sqlitedatabase ;import android.database.sqlite.sqlitedatabase.cursorfactory;import  android.database.sqlite.sqliteopenhelper;public class mydatabasehelper extends  Sqliteopenhelper {    //text string  varchar  int integer numeric (10,2)  real fractional   blob binary data     /**     * unique   Uniqueness constraints      * not null  non-null constraints      *  check  CHECK constraint      * primary key  PRIMARY KEY constraint      * /    public static final string tab_user =  "CREATE  Table if not exists t_user ("              + "Id integer primary key&nbsP;autoincrement, "            +" Name varchar (  unique, "            +" pwd text  Default (' 123 '), "            +" Age integer  not null, "            +" Sex text  check (Sex in (' f ', ' m ')) "            +") " ;     public mydatabasehelper (Context context)  {         super (context,  "user.db",  null, 1);    }      @Override     public void oncreate (sqlitedatabase db)  {        db.execsql (Tab_user);    }      @Override  &Nbsp;  public void onupgrade (Sqlitedatabase db, int oldversion, int  newversion)  {            }}
package com.gxa.db;import java.util.arraylist;import java.util.list;import  Android.content.context;import android.database.cursor;import android.database.sqlite.sqlitedatabase ;p ublic class userdao {    public mydatabasehelper helper;     public userdao (Context context) {         Helper = new mydatabasehelper (context);     }    public  void adduser (User user) {        //id null         sqlitedatabase db = helper.getwritabledatabase ();         db.execsql ("insert into t_user  (Name,pwd,age, Sex)  values (?,?,?,?) ",                  new object[]{User.getname (), User.getpwd (), User.getage (), User.getsex ()});         Db.close ();     }    public void update (int id,String  name) {        sqlitedatabase db =  Helper.getwritabledatabase ();         db.execsql ("Update t_user  set name=? where id= ? ",                 new object[]{name,id});         db.close ();     }    public void delete (Int id) {         sqlitedatabase db = helper.getwritabledatabase ( );         db.execsql ("delete from t_user where id= ? ", New object[]{id});   &nBsp;     db.close ();     }    public list <user> queryall () {        list<user> users  = new arraylist<user> ();         sqlitedatabase db  = helper.getreadabledatabase ();        user user  = null;        cursor cursor = db.query ("T_user",  null, null, null, null, null, null);         if  (cursor!=null)  {             while (Cursor.movetonext ()) {                 int id = cursor.getint (Cursor.getcolumnindex ("id"));        &nbSp;        int age = cursor.getint ( Cursor.getcolumnindex ("Age"));                 string name = cursor.getstring (Cursor.getcolumnindex ("name"));                 string pwd =  cursor.getstring (Cursor.getcolumnindex ("pwd"));                 string sex = cursor.getstring (Cursor.getColumnIndex (" Sex "));                 user  = new user (Id, name, pwd, age, sex);                 users.add (user);             } &nBsp;          cursor.close ();         }        db.close ();         return users;    }}
Package Com.gxa.db;public class Constant {public static final String tab_name = "T_user";}
Package Com.gxa.db;import Android.os.bundle;import Android.app.activity;import Android.view.menu;public class Mainactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {super.oncreate        (savedinstancestate);    Setcontentview (R.layout.activity_main); }}
Package Com.gxa.db;import Android.os.bundle;import Android.app.activity;import Android.view.menu;public class Mainactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {super.oncreate        (savedinstancestate);    Setcontentview (R.layout.activity_main); }}


The primary use and unit test examples of SQLite

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.