本文整理匯總了Java中android.content.Context.openOrCreateDatabase方法的典型用法代碼示例。如果您正苦於以下問題:Java Context.openOrCreateDatabase方法的具體用法?Java Context.openOrCreateDatabase怎麽用?Java Context.openOrCreateDatabase使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.content.Context
的用法示例。
在下文中一共展示了Context.openOrCreateDatabase方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: queryTableByName
import android.content.Context; //導入方法依賴的package包/類
/**
* 查詢數據庫中所有的表.
* 不能通過獲取data/data/package_name/databases/xxx文件的方式獲取數據庫表.
*
* @param context {@link Context}
* @param databaseName 數據庫名稱
* @return all table
*/
public static List<String> queryTableByName(Context context, String databaseName) {
List<String> list = null;
List<File> databaseList = queryDatabaseList(context);
if (databaseList != null && databaseList.size() > 0) {
if (!isDatabaseExits(databaseList, databaseName)) {
return list;
}
SQLiteDatabase database = context.openOrCreateDatabase(databaseName, Context.MODE_PRIVATE, null);
if (database != null) {
Cursor cursor = database.rawQuery("select name from sqlite_master where type = 'table'", null);
if (cursor != null) {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
list = new ArrayList<>();
do {
String tableName = cursor.getString(0);
if (!TextUtils.isEmpty(tableName) && !(tableName.equals("android_metadata")
|| tableName.equals("sqlite_sequence"))) {
list.add(tableName);
}
} while (cursor.moveToNext());
}
cursor.close();
}
}
}
return list;
}
示例2: queryDatabaseData
import android.content.Context; //導入方法依賴的package包/類
/**
* 查詢指定數據庫中指定表的數據.
* 當前隻能全部查詢,後期可以進行優化
*
* @param context {@link Context}
* @param name database
* @param table table
* @return list
*/
public static List<TablePackage> queryDatabaseData(Context context, String name, String table) {
List<TablePackage> all = null;
//判斷數據庫是否存在
// TODO 重構查詢方式.
List<File> databaseList = queryDatabaseList(context);
if (!isDatabaseExits(databaseList, name)) {
return null;
}
// TODO 重構查詢方式.
//判斷表是否存在
List<String> tableList = queryTableByName(context, name);
if (!isTableExits(tableList, table)) {
return null;
}
SQLiteDatabase database = context.openOrCreateDatabase(name, Context.MODE_PRIVATE, null);
if (database != null) {
Cursor cursor = database.query(table, null, null, null, null, null, null);
if (cursor != null) {
String[] columnName = cursor.getColumnNames();
if (cursor.getCount() > 0) {
cursor.moveToFirst();
all = new ArrayList<>();
do {
TablePackage tablePackage = new TablePackage();
for (int index = 0; index < columnName.length; index++) {
//TODO 後麵可以修改為根據column的類型來獲取相應的值,暫時測試麽有問題
int columnIndex = cursor.getColumnIndex(columnName[index]);
tablePackage.put(columnName[index], columnIndex > -1 ? cursor.getString(columnIndex) : "");
}
all.add(tablePackage);
} while (cursor.moveToNext());
}
cursor.close();
}
database.close();
}
return all;
}
示例3: MessageDB
import android.content.Context; //導入方法依賴的package包/類
/**
* 初始化數據庫,與聊天紀錄表
* @param context
*/
public MessageDB(Context context) {
db = context.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE,
null);
db.execSQL("CREATE table IF NOT EXISTS "
+ TABLE_NAME
+ " (ID INTEGER PRIMARY KEY AUTOINCREMENT,U_id INTEGER, T_id TEXT,NickName TEXT,HeadId INTEGER, Message TEXT,Time TEXT,IsCome INTEGER)");
}
示例4: DatabaseOperations
import android.content.Context; //導入方法依賴的package包/類
public DatabaseOperations(Context context) {
super(context, dbname, null, 1);
//------------------------------------Database Opening/ (Database Creation and Table Creations)--------------
db = context.openOrCreateDatabase(dbname,SQLiteDatabase.CREATE_IF_NECESSARY,null);
String tbl="create table if not exists notes ( noteid integer primary key autoincrement, notetitle text, notecontent text, notecreated DATETIME DEFAULT CURRENT_TIMESTAMP)";
db.execSQL(tbl);
//-----------------------------------------------------------------------------------------------------------
}
示例5: CityDB
import android.content.Context; //導入方法依賴的package包/類
public CityDB(Context context, String path) {
db = context.openOrCreateDatabase(path, Context.MODE_PRIVATE, null);
}
示例6: createDBTables
import android.content.Context; //導入方法依賴的package包/類
public static SQLiteDatabase createDBTables(Context context){
SQLiteDatabase sqlDB=context.openOrCreateDatabase("dbKartik12#4",context.MODE_PRIVATE, null);
//sqlDB.execSQL("DROP TABLE ToDoList");
sqlDB.execSQL("CREATE TABLE IF NOT EXISTS ToDoList(Task varchar(200) PRIMARY KEY,Status char(5),DeadlineDate varchar(10))");
return sqlDB;
}