First knowledge of SQLite

Source: Internet
Author: User

Basic Concepts :

SQLite is an open-source, embedded relational database that implements a self-contained, 0 configuration, transactional SQL database engine. It is characterized by its high portability, ease of use, compact structure, high efficiency and reliability.

Sqliteopenhleper (Important Database helper Class):

Sqliteopenhleper is a helper class that inherits from Sqlitedatabase and is used to manage the creation of databases and version updates, and the two main ways to implement this are: OnCreate (Sqlitedatabase db), Onupgrade (sqlitedatabase db,int oldversion,int newversion), respectively, create the database and update the database.

1  PackageCom.example.sqliteData.OpenHelper;2 3 ImportAndroid.content.Context;4 Importandroid.database.sqlite.SQLiteDatabase;5 ImportAndroid.database.sqlite.SQLiteOpenHelper;6 7 /**8 * Database Helper class9  */Ten  Public classPersonopenhelperextendsSqliteopenhelper { One     /** A      * @paramContext -      */ -      PublicPersonopenhelper (Context context) { the         Super(Context, "person.db",NULL, 1); -     } -  -     /** + * When the database library was first created, call OnCreate () -      * +      * @paramDB The database that was created A      */ at @Override -      Public voidonCreate (Sqlitedatabase db) { -Db.execsql ("CREATE TABLE person" (ID integer primary key autoincreament,name varchar (), number varchar (20)) "); -     } -  - @Override in      Public voidOnupgrade (Sqlitedatabase sqlitedatabase,intIintI2) { -         //I don't usually use it when I'm a beginner to     } +}

SQLite additions and deletions : Sqllite provides an API for manipulating databases, and we can manipulate them by instantiating Sqlitedatabase. (insert,query,update)
 Add Data Insert (string table, String nullcolumnhack, contentvalues values)

Parameter explanation:

Table name

Nullcolumnhack default values for empty columns are generally written as null

Contentvalues fill in map into row name and column values. Code Demo:

     Public void Add (string name, string pwd) {        = helper.getwritabledatabase ();        contentvalues CV=new  contentvalues ();        Cv.put ("username", name);        Cv.put ("password", pwd);        Db.insert ("User",null, CV);         // nullcolumnhack default values for empty columns         db.close ();    }

Deleting Data Delete (string table, String Whereclause, string[] whereargs)

Table name

Whereclause Delete Condition

Whereargs Delete Condition Value Data Group code Demo

   Public void Delete (String name) {        = helper.getwritabledatabase ();        String whereclause= "id=?"; // ? Represents the placeholder        string[] whereargs={string.valueof (1)};        Db.delete ("User", Whereclause,whereargs);        Db.close ();    }

Querying data Query (string table, string[] columns, string selection, string[] Selectionargs, String groupBy, String having, string by, String limit)

Table name

Columns Column Name

Selection conditional clauses, equivalent to where

An array of parameters for Selectionargs conditional words

GroupBy Grouping columns

Having group conditions

Sort by order class

Limit paging Query limits

Complement: Cursor return value equals result set ResultSet code display

     PublicList<person>FindAll () {Sqlitedatabase db=helper.getwritabledatabase (); List<Person> persons =NewArraylist<person>(); Cursor Cursor= Db.query ("User",NULL,NULL,NULL,NULL,NULL,NULL,NULL);  while(Cursor.movetonext ()) {intid = cursor.getint (cursor.getcolumnindex ("id"))); String name= Cursor.getstring (Cursor.getcolumnindex ("name")); String Number= Cursor.getstring (Cursor.getcolumnindex ("number")); Person P=NewPerson (ID, name, number);        Persons.add (P);        } cursor.close ();        Db.close (); returnpersons; }

Modifying data upgrade(string table, contentvalues values, String whereclause, string[] whereargs)

Table name

Contentvalues key value pair map

Selection conditional clauses, equivalent to where

An array of parameters for Selectionargs conditional words

        Sqlitedatabase db = helper.getwritabledatabase ();         New contentvalues ();        Values.put ("password", "123456");         = "Id=?" ;         = {string.valueof (1)};        Db.update ("User", Values, Whereclause, Whereargs);        Db.close ();
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.