本文整理汇总了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;
}