Android SQLite database operation (top)

Source: Internet
Author: User

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://"Xmlns:tools= "Http://"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

 Packagecom.example.db;;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 (); }}

 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)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.