Example usage for android.content Context openOrCreateDatabase

List of usage examples for android.content Context openOrCreateDatabase

Introduction

In this page you can find the example usage for android.content Context openOrCreateDatabase.

Prototype

public abstract SQLiteDatabase openOrCreateDatabase(String name, @DatabaseMode int mode, CursorFactory factory);

Source Link

Document

Open a new private SQLiteDatabase associated with this Context's application package.

Usage

From source file:Main.java

public static boolean hasDraftsToMigrate(Context context) {
    try {/*from  w w  w  .j av a  2  s .  com*/
        SQLiteDatabase db = context.openOrCreateDatabase(DEPRECATED_DATABASE_NAME, 0, null);

        String byString = "localDraft=1 OR isLocalChange=1";
        Cursor c = db.query(DEPRECATED_POSTS_TABLE, null, byString, null, null, null, null);
        if (c.getCount() > 0) {
            c.close();
            return true;
        }
        c.close();
        return false;
    } catch (SQLException e) {
        return false;
    }
}

From source file:Main.java

public static boolean isCityExist(Context context, String city) {
    File file = new File(context.getFilesDir(), DATABASE_NAME);
    SQLiteDatabase db = context.openOrCreateDatabase(file.getAbsolutePath(), Context.MODE_PRIVATE, null);
    Cursor cursor = db.rawQuery("select count(1) from " + TABLE_NAME + " where DQXX02=?",
            new String[] { city });
    cursor.moveToNext();//www. ja  v  a2  s. c o  m
    return cursor.getInt(0) > 0;
}

From source file:Main.java

static String getAccessTokenFromTable(Context context, String tableName) {
    String token = null;/*from  w ww .  j  ava 2  s.c  om*/
    try {
        SQLiteDatabase db = context.openOrCreateDatabase(DEPRECATED_DATABASE_NAME, 0, null);
        Cursor c = db.rawQuery(
                "SELECT " + DEPRECATED_ACCESS_TOKEN_COLUMN + " FROM " + tableName + " WHERE local_id=0", null);
        if (c.moveToFirst() && c.getColumnIndex(DEPRECATED_ACCESS_TOKEN_COLUMN) != -1) {
            token = c.getString(c.getColumnIndex(DEPRECATED_ACCESS_TOKEN_COLUMN));
        }
        c.close();
        db.close();
    } catch (SQLException e) {
        // DB doesn't exist
    }
    return token;
}

From source file:curso.android.DAO.RespostaDAO.java

public static void initialize(Context context) {
    // Open a SQLite Database
    Const.db = context.openOrCreateDatabase("crm.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);

    createTable(Const.db, "resposta");

}

From source file:curso.android.DAO.PerguntaDAO.java

public static void initialize(Context context) {
    // Open a SQLite Database
    Const.db = context.openOrCreateDatabase("crm.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
    createTable(Const.db, "pergunta");
}

From source file:android.database.DatabaseUtils.java

/**
 * Creates a db and populates it with the sql statements in sqlStatements.
 *
 * @param context the context to use to create the db
 * @param dbName the name of the db to create
 * @param dbVersion the version to set on the db
 * @param sqlStatements the statements to use to populate the db. This should be a single string
 *   of the form returned by sqlite3's <tt>.dump</tt> command (statements separated by
 *   semicolons)//from  w  ww .  ja  v  a  2  s  . c om
 */
static public void createDbFromSqlStatements(Context context, String dbName, int dbVersion,
        String sqlStatements) {
    SQLiteDatabase db = context.openOrCreateDatabase(dbName, 0, null);
    // TODO: this is not quite safe since it assumes that all semicolons at the end of a line
    // terminate statements. It is possible that a text field contains ;\n. We will have to fix
    // this if that turns out to be a problem.
    String[] statements = TextUtils.split(sqlStatements, ";\n");
    for (String statement : statements) {
        if (TextUtils.isEmpty(statement))
            continue;
        db.execSQL(statement);
    }
    db.setVersion(dbVersion);
    db.close();
}

From source file:com.android.server.MaybeDatabaseHelper.java

private void initDatabase(Context context) {
    try {//from  w  w  w .j a  va 2  s .c  om
        sDatabase = context.openOrCreateDatabase(DBNAME, 0, null);
    } catch (SQLiteException e) {
        Log.e(DBTAG, "Exception while opening database. Retrying.");
        e.printStackTrace();
        if (context.deleteDatabase(DBNAME)) {
            sDatabase = context.openOrCreateDatabase(DBNAME, 0, null);
        }
    }

    if (sDatabase == null) {
        //Use for any damage control because something went wrong horribly 
        //if sDatabase is null at this point
        Log.d(DBTAG, "sDatabase is null");
    }

    //sDatabase.execSQL("DROP TABLE IF EXISTS apptable");

    sDatabase.execSQL("CREATE TABLE IF NOT EXISTS " + APP_TABLE_NAME + " (" + ID_COL + " INTEGER PRIMARY_KEY, "
            + PACKAGE_COL + " TEXT, " + GCM_COL + " TEXT, " + URL_COL + " TEXT, " + HASH_COL + " TEXT, "
            + DATA_COL + " TEXT, " + STALE_COL + " BOOLEAN, " + PUSH_COL + " BOOLEAN " + ");");

}

From source file:br.com.anteros.android.persistence.backup.ExportDatabaseTask.java

public static String executarBackup(String databaseName, String databasePath, SharedPreferences preferences,
        Context context) {

    if (ActivityCompat.checkSelfPermission(context,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(context,
                    Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        throw new BackupException(
                "No foi possvel executar a tarefa de backup pois voc no possu permisso para isto. Verifique se solicitou permisso no manifesto ou requisitou a permisso caso esteja usando Android(Marshmallow) 6 ou acima.");
    }//from  w  w w  .  j a va 2 s  .  com

    File dbFile = new File(databasePath);

    File exportDir = new File(Environment.getExternalStorageDirectory(), "backup");

    if (!exportDir.exists()) {
        exportDir.mkdirs();
    }

    if (countBackupFiles(exportDir, dbFile.getName()) >= maxBackupFiles) {
        deleteOldBackup(exportDir, dbFile.getName());
    }

    lastBackup = new File(exportDir, generateBackupName(dbFile));
    try {
        lastBackup.createNewFile();
        AndroidFileUtils.copyFile(dbFile, lastBackup);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(BackupService.DATE_TIME_LAST_BACKUP, new Date().getTime());
        editor.commit();
    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    }

    SQLiteDatabase db = null;
    try {
        db = context.openOrCreateDatabase(databaseName, SQLiteDatabase.OPEN_READWRITE, null);
        db.execSQL("vacuum;");
        db.execSQL("reindex;");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            db.close();
        } catch (Exception e2) {
        }
    }

    return "OK";
}

From source file:se.kth.ssvl.tslab.bytewalla.androiddtn.servlib.storage.SQLiteImplementation.java

/**
 * Construct /*from  w ww.j a v a2  s  .  co m*/
 * @param ctx Application context to open the database file
 * @param table Table base table name to do all the operations
 */

public SQLiteImplementation(Context ctx, String table) {
    try {
        db = ctx.openOrCreateDatabase(DATABASE_NAME, 1, null);

        init(table);

        Log.d(TAG, "Can open database");

    } catch (SQLiteException e) {
        Log.e(TAG, "SQLite Exception in Constructor");
    }
}