Android built database SQLite store SD card or memory

Source: Internet
Author: User

Android CREATE Database call Sqliteopenhelper, generally do not directly operate Sqlitedatabase .

is through Sqliteopenhelper to get



public class Dbopenhelper extends Sqliteopenhelper {

private static final int version = 1;//defines the database version
private static final String PATH = Environment
. getExternalStorageDirectory (). GetPath ();
Some don't have an SD card, so I didn't write it in.
private static final String DBNAME = "flexemdata.db";//define Database name path+ "/" +
Private final static String TAG = "dbopen";


Public Dbopenhelper (Context context) {//define Constructors

Super (context, DBNAME, null,version);//Overriding the constructor of a base class
}


Callback This method when you create the database for the first time
@Override
public void OnCreate (Sqlitedatabase db) {
LOG.I (TAG, "Create data Table");
Db.begintransaction ();
try {
Create a live data table
Db.execsql ("CREATE TABLE alarm (Sensorname integer primary key autoincrement,uid, code, Alarmmessage, Timecreated,boxuid )");
LOG.I (TAG, "Initialize database");
Initializing real-time data tables
Db.execsql ("INSERT into alarm (UID,CODE,ALARMMESSAGE,TIMECREATED,BOXUID) VALUES (' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ')");


Db.settransactionsuccessful ();
} finally {
Db.endtransaction ();
}
}


Callback this method when the database version number is updated
@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion)//Overwrite base class Onupgrade method so that the database version number is updated
{
oldversion= old repository version number. newversion= New Repository Version number
Db.execsql ("DROP TABLE IF EXISTS" + DBNAME); Delete old information table
OnCreate (DB);
}

public Boolean DeleteDatabase (context context) {
Return Context.deletedatabase (DBNAME);

}


This class has the following database creation, the relative path is directly stored in the program below. Assuming an absolute path, you can choose. Whether the SD card.

In the class used

Private Dbopenhelper helper;//Creating Dbopenhelper objects
Private Sqlitedatabase db;//Creating Sqlitedatabase objects

{
Helper = new Dbopenhelper (context);//Initialize Dbopenhelper object

db = Helper.getreadabledatabase ();//Initialize Sqlitedatabase object
}

Use getreadabledatabase directly to get the database Sqlitedatabase object.

Here is how to voluntarily choose to have an SD card, there is an SD card, not stored in memory.

Public Dbopenhelper (Context context) {//define Constructors

Super (context, DBNAME, null,version);//Overriding the constructor of a base class
}

This constructor is changed to the end

Public Dbopenhelper (Context context,string name) {//define Constructors

Super (context, name, null,version);//Overriding the constructor of a base class
}

And infer from the activity that was used.

if   (Android.os.Environment.getExternalStorageState (). Equals (

  Android.os.Environment.MEDIA_MOUNTED))  { 

name=< Span style= "font-size:18px" >environment .getexternalstoragedirectory (). GetPath () + "/" + "alarm.db";

}else{

Name= "Alarm.db";

}

and then call

Helper = new Dbopenhelper (context. name);//Initialize the Dbopenhelper object

You can infer it on your own initiative.




Android built database SQLite store SD card or memory

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.