Android SQLite database operation (top)

Source: Internet
Author: User
Tags sqlite database

The Android system comes with a database called SQLite. In this article we use a demo to illustrate an example of an Android operating database.

by the

The database files created by Android are stored under the/data/data/< package name >/database, and the suffix is typically xxx.db

See the database file, we generally use a visual tool called SQLite Expert to view.

Let's take a simple example to create a data database that creates a table.

Note that while we have packaged APIs available in Android Development, SQL statements are best mastered.

Let's look at the picture first.

, the following is an open interface with SQLite expert, after running the app, click on the button will create a database named people, the database has a girl table. (If the table is not created, there is also a table below the data, which is the system automatically generated table, but we do not care.) )

And then look at the code

Layout file Activity_main.xml

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"android:orientation= "vertical"Tools:context= "Com.example.db.MainActivity" >    <ButtonAndroid:onclick= "Create_datebase"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Create datebase" /></LinearLayout>

Java files

Mainactivity.java

 Packagecom.example.db;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View; Public classMainactivityextendsActivity {PrivateMysqlite Mydatebasehelper; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Mydatebasehelper=NewMysqlite ( This, "People.db",NULL, 1); }         Public voidCreate_datebase (View v) {/** If the database is not created, create and obtain a writable (and actually readable) database if the database is created, get a writable (and actually readable) database * Path data/data/database/people.db */mydatebasehelper.getwritabledatabase (); }}

Mysqlite.java

 Packagecom.example.db;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.widget.Toast;//creating Sqllite requires inheriting the Sqliteopenhelper class abstract class Public classMysqliteextendssqliteopenhelper{ Public Static FinalString create_girl = "CREATE table gril (_id integer primary key Autoincrement,name char (), Age Integer,phone char (20))" ; PrivateContext Mcontext; /** Construction Method parameter Description * First: Incoming context object * Second: The name of the database to be created * Third: Oil standard factory, the introduction of oil target object, is actually a pointer function. Similar to the ResultSet function, this parameter typically passes in NULL * Fourth: Database version number, which is called when upgrading.     The version number must be greater than 1 * Construction method four parameters passed in is actually called to the parent class. * */     PublicMysqlite (Context context, String name, Cursorfactory factory,intversion) {        Super(context, name, Factory, version); //TODO auto-generated Constructor stubMcontext =context; }    //This method is called when the database is created@Override Public voidonCreate (Sqlitedatabase db) {//TODO auto-generated Method Stub        /*Db.execsql (Create_book); Toast.maketext (Mcontext, "Create successded", 0). Show ();*/Db.execsql (Create_girl); Toast.maketext (Mcontext,"Create successded", 0). Show (); }    //called when the database is upgraded@Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {        //TODO auto-generated Method Stub            }}

Android SQLite database operation (top)

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.