Android SQLite use (1)

Source: Internet
Author: User

The Android operating system uses the SQLite database, which uses two methods to obtain database objects:

1. Get a database that already exists

Sqlitedatabase dbbrndi=sqlitedatabase.opendatabase ("/sdcard/zhycheng.db3", null,sqlitedatabase.open_readonly);


The first string parameter is the location of the database in the file system, the second parameter is generally null, and the third parameter controls how the database is opened.

This obtains the database object.

2. Create your own database

Create a new class, Inherit Sqliteopenhelper, add a method that is not implemented

The code is as follows

Package Your.zhycheng;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.database.sqlite.sqliteopenhelper;public Class Myhelper extends Sqliteopenhelper{public myhelper (context context, String name) {this (context,name,1);} Public Myhelper (context context, String Name,int version) {this (context,name,null,version);} Public Myhelper (context context, String name,//database name cursorfactory factory,int version) {Super (context, name, factory, version);} @Overridepublic void OnCreate (Sqlitedatabase db) {db.execsql ("CREATE table user (id int,name text)");} @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {}}


The obtained database is located in/DATA/DATA/YOURPACKAGE/DATABASES/ZHYCHENG.DB3
Then, by generating an object of the Myhelper class, call the

Myhelper mh=new Myhelper (This, "Zhycheng"); Sqlitedatabase db=mh.getreadabledatabase (); Sqlitedatabase db=mh.getwritabledatabase ();


Get a read-only and writable database, respectively.

Database operations can be performed after the database has been obtained the following two ways to manipulate the database

1. Execute SQL statements

Db.rawquery (Sql,args) db.execsql (SQL, Args) db.execsql (SQL)


The SQL above is a string-type database language, and args is a string array. If there is "?" in the preceding string Corresponds to the value that follows.

2. Use a specific method

Insert method

Myhelper mhz=new Myhelper (This, "Zhycheng", 2); Sqlitedatabase dbz=mhz.getwritabledatabase (); Contentvalues value=new contentvalues () value.put ("id", 1), Value.put ("name", "Zhangyicheng");d bz.insert ("User", null, value);//The second parameter must be NULL


Delete method

                   Myhelper mhsc=new Myhelper (This, "Zhycheng", 2);   Sqlitedatabase dbsc=mhsc.getwritabledatabase ();   Dbsc.delete ("User", "id=?", New string[]{"1"});   Dbsc.close ();

Modification method

                   Myhelper mhg=new Myhelper (This, "Zhycheng", 2); Sqlitedatabase dbg=mhg.getwritabledatabase (); Contentvalues vs=new contentvalues () vs.put ("id", 1);d bg.update ("User", VS,//set xxx=xx "Id=?", New string[]{"4"}//id= 4);//


Check method

Myhelper mhc=new Myhelper (This, "Zhycheng", 2); Sqlitedatabase dbc=mhc.getreadabledatabase (); Cursor c=dbc.query ("User",//Table name new string[]{"id", "name"},//query column "Id=?", New string[]{"2"},//wherenull, NULL, NULL); while (C.movetonext ()) {System.out.println (c.getstring (C.getcolumnindex ("name"));}


Finally, there is a function that is useful

Insert into user values (7,datetime (current_timestamp, ' localtime '))



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.