若需要实现在文件系统的其他位置存放数据库,需实现以下步骤,缺一不可。
一 代码实现
//打开数据库,若数据库不存在,则创建数据库文件
File f = new File("/opt/Test.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(f, null);
//执行SQL语句,打开数据表,若数据表不存在,则新建
db.execSQL("CREATE TABLE IF NOTEXISTS TableName1 (_id INTEGER PRIMARY KEY,Column1 TEXT,Column2 TEXT)");
// 新建key为数据表中列名的值对
ContentValues values = new ContentValues();
values.put("Column1", String.valueOf(100));
values.put("Column2", "title");
// Insert the new row, returning the primary key value of the new row
long newRowId;
newRowId = db.insert(
"TableName1",
"nullable",
values);
//计算当前数据表中的行数,行数超过10行,则将最旧的数据删除
SQLiteStatement statement = db.compileStatement("SELECT COUNT(*) FROM TableName1;");
long row = statement.simpleQueryForLong();
if (row > 10)
{
db.execSQL(" delete from tablename1where _id=(select min(_id) from tablename1);");
}
//将数据表内容id按反序排列
String[] projection = {
"_id",
"Column1",
"Column2",
};
String sortOrder ="_id" + " DESC";
Cursor cursor = db.query(
HistoryDataEntry.TABLE_NAME, // The table to query
projection, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
//遍历读数据库内容,并将每行数据中第1列数据显示在控件上
cursor.moveToFirst();
String strText = "";
while (cursor.moveToNext())
{
long lValue = cursor.getLong(1);
strText += String.valueOf(lValue)+ " ";
}
EditText editText = (EditText) findViewById(R.id.edit1);
editText.setText(strText);
//cursor使用完毕要及时关掉,以免内存泄漏
if (cursor != null)
{
cursor.close();
cursor = null;
}
二在AndroidManifest.xml文件中增加系统权限
android:sharedUserId="android.uid.system"
三 使用系统源码工具签名
Java –jar signapk.jar platform.x509.pem platform.pk8 MyFirstApp.apk singed\MyFirstApp.apk
则singed目录下的MyFirstApp.apk即是具备系统权限的apk。