Android Database Sqlite-android learning Journey (ix)

Source: Internet
Author: User

Brief introduction

Sqilte is a lightweight database, satisfying the basic operation of the database, due to the limited memory of the mobile side, so sqilte just can meet the basic requirements of mobile development.

Nonsense not much to say on the code

1. First introduce, SQLite management class Sqliteopenhelper. This class is used to manage the creation and release of a database, and it needs to inherit its methods.
The code is as follows:

 Public  class Db extends sqliteopenhelper {     Public Db(context context, String name, Cursorfactory factory,intVersion) {Super(Context,"DB",NULL,1);//TODO auto-generated constructor stub}@Override     Public void onCreate(Sqlitedatabase db) {//TODO auto-generated method stubDb.execsql ("CREATE TABLE User ("+"name TEXT DEFAULT \" \ ""+"Sex TEXT DEFAULT \" \ ")"); }@Override     Public void Onupgrade(Sqlitedatabase DB,intOldversion,intNewVersion) {//TODO auto-generated method stub}}

Where OnCreate when the database is created callback, Onupgrade method is the version number is increased, the automatic callback, so the database to update the operation is written inside.

Inserting operations on a database
 Public  class mainactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.main); DB db =NewDb ( This);There are two ways to get a database, which is to get read-write and to get a readable database. The Db.getreadabledatabase () method is obtained. Sqlitedatabase dbWriter = Db.getwritabledatabase (); Contentvalues CV =NewContentvalues (); Cv.put ("Name","Shangsan"); Cv.put ("Sex","Male");The first parameter indicates that the second represents an empty column, and the third represents the data to be filled in contentvalues encapsulation.         //dbwriter.insert (table, nullcolumnhack, values);Dbwriter.insert ("User",NULL, CV);    Db.close (); }}
Query operations
 Public  class mainactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.main); DB db =NewDb ( This);//Sqlitedatabase DbWriter = Db.getwritabledatabase ();//contentvalues CV = new Contentvalues ();//Cv.put ("name", "Shangsan");//Cv.put ("Sex", "male");////dbwriter.insert (table, nullcolumnhack, values);//      Sqlitedatabase dbraed = Db.getreadabledatabase ();//cursor c = dbraed.query (table, columns, selection, Selectionargs, GroupBy, having, by-and-by);Cursor C = Dbraed.query ("User",NULL,"Name=?",Newstring[]{"Zhangsan"},NULL,NULL,NULL); while(C.movetonext ()) {String name = c.getstring (C.getcolumnindex ("Name")); String sex = c.getstring (C.getcolumnindex ("Sex")); System.out.println (name+"  "+sex); }    }}

Android Database Sqlite-android learning Journey (ix)

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.