Android-create a database

Source: Internet
Author: User

Sqllite is similar to Microsoft Access. The following describes how to create a database file in Android.

1. dbopenhelper

 
Package COM. njupt. SQLite; import android. content. context; import android. database. SQLite. sqlitedatabase; import android. database. SQLite. sqliteopenhelper; public class dbopenhelper extends sqliteopenhelper {public dbopenhelper (context) {super (context, "njupt1.db", null, 2);} public void oncreate (sqlitedatabase dB) {// if the file does not exist, run system. out. println ("oncreate... Zhang Zetian is my goddess 1" mongomongodb.exe csql ("create table person1 (ID integer primary key autoincrement, name varchar (20 ))");} @ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// The file exists. When the version is upgraded, run system. out. println ("onupdate · my goddess is Zhang Zetian 1" mongomongodb.exe csql ("alter table person1 add balance integer ");}}

2. dbtest

Package COM. njupt. SQLite; import android. test. androidtestcase; public class dbtest extends androidtestcase {public void test1 () {dbopenhelper helper = new dbopenhelper (getcontext (); helper. getwritabledatabase (); // get writable database connections }}

3. The result is as follows:

1)

Explanation: The first log is output at the first execution. The second log is output when the database version is upgraded.

2)

Explanation: After the database is successfully created, you will find a new directory named databases in the/data/project, the database file you created is saved in the databases directory.

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.