Android Notes (39) data storage in Android--sqlite (i)

Source: Internet
Author: User

SQLite is a lightweight relational database built into Android that is fast and resource-intensive, and typically requires only hundreds of K of memory, making it ideal for use on mobile devices.

SQLite not only supports standard SQL syntax, but also adheres to the acid transactions of the database, so if you have other database bases, you'll get started quickly. Unlike other databases, SQLite can be used without setting a user name and password.

Unlike other databases, SQLite has no more complex data types than other databases, and its data types are simple: integer for integer, real for float, text for type, blob for binary type

Create a database

Sqliteopenhelper Abstract classes can help us create and upgrade databases.

Inheriting sqliteopenhelper needs to implement its two methods: OnCreate () and Onupgrade (), and then implement the logic of creating and upgrading the database in both methods.

Sqliteopenhelper also has two methods Getreadabledatabase () and Getwirtabledatabase (), both of which can create or open an existing database and return an object that can read and write to the database. The difference is that when the database is not writable, the object returned by the Getreadabledatabase () method will open the database in a read-only manner, and getwirtabledatabase () will throw an exception.

A simple example:

Mainactivity.java

 PackageCn.lixyz.sqlitedemo;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {PrivateButton Bt_createdb; PrivateMydatabasehelper Mydatabasehelper; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Bt_createdb=(Button) Findviewbyid (r.id.bt_createdb); Mydatabasehelper=NewMydatabasehelper ( This, "Bookstore.db",NULL, 1); Bt_createdb.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {mydatabasehelper.getwritabledatabase ();    }        }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); //noinspection simplifiableifstatement        if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }}

Mydatabasehelper.java

 PackageCn.lixyz.sqlitedemo;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.widget.Toast;/*** Created by LGB on 2015/10/16.*/ Public classMydatabasehelperextendsSqliteopenhelper { Public Static FinalString create_book = "CREATE table book (ID integer primary key autoincrement,author text, price real,pages integer,name t EXT) "; PrivateContext Mcontext;  PublicMydatabasehelper (context context, String name, Sqlitedatabase.cursorfactory factory,intversion) {        Super(context, name, Factory, version); Mcontext=context; } @Override Public voidonCreate (Sqlitedatabase db) {db.execsql (Create_book); Toast.maketext (Mcontext,"Created successfully", Toast.length_short). Show (); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {    }}

Activity_main.xml

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ButtonAndroid:id= "@+id/bt_createdb"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "CREATE DATABASE" /></LinearLayout>

Execution Result:

View in Ddms

We open this DB file using Navicat:

Visible, database and table creation succeeded.

Android Notes (39) data storage in Android--sqlite (i)

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.