SQLite database application in Android

Source: Internet
Author: User

1. Features

Compared with other data types. It can store any type, except the primary key. If the primary key is Integer primary key, only 64-bit integers can be stored.

1) the following statement can be used to create table:

Create Table person (personid integer primary key autocrement, name varchar (20 ))

We recommend that you write varchar (20) later.


2) Get the id value after auto-increment: Select last_insert_rowid ()


Case:

1. Create a database

Sqliteopenhelper. getreadabledatabase () or getwritabledatabase ()

Automatically create a database.


Package COM. tong. service; import android. content. context; import android. database. SQLite. sqlitedatabase; import android. database. SQLite. sqlitedatabase. cursorfactory; import android. database. SQLite. sqliteopenhelper; public class dbopen extends sqliteopenhelper {public dbopen (context) {// database name Tong. DB uses the system's default cursor. the version number of the factory database file is super (context, "tong. DB ", null, 1) ;}@ overridepublic void oncreate (sqlitedatabase dB) {// The database table generated when the data is first created // sqlitedatabase is encapsulated with db.exe csql ("create table person (personid integer primary key autoincrement, name varchar (20 )) ") ;}@ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// call when the version number changes // execute the update operation // use a field db.exe csql ("alter table person add phone varchar (12) null ");}}




Write a test class to create a database.

Package COM. tong. test; import COM. tong. service. dbopen; import android. test. androidtestcase; public class dbopentest extends androidtestcase {public void testcreatedb () throws exception {dbopen = new dbopen (getcontext (); // automatically create a database dbopen. getreadabledatabase ();}}


Do not forget to add the configuration file to the unit test:

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. example. dbapp"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">

<Uses-SDK
Android: minsdkversion = "8"
Android: targetsdkversion = "17"/>

<Application
Android: allowbackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: Label = "@ string/app_name"
Android: theme = "@ style/apptheme">
<Activity
Android: Name = "com. Tong. dbapp. mainactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>

<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

<Uses-library Android: Name = "android. Test. Runner"/>

</Application>
<Instrumentation Android: Name = "android. Test. instrumentationtestrunner"
Android: targetpackage = "com. example. dbapp" Android: Label = "test Tong app">
</Instrumentation>
</Manifest>

Run the test case. The database has been created!




Android_metadata is an automatically created table that records language information.




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.