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