Example usage for android.database.sqlite SQLiteDatabase execSQL

List of usage examples for android.database.sqlite SQLiteDatabase execSQL

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase execSQL.

Prototype

public void execSQL(String sql) throws SQLException 

Source Link

Document

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

Usage

From source file:org.opendatakit.common.android.database.DataModelDatabaseHelper.java

public static void deleteTableAndData(SQLiteDatabase db, String formId) {
    try {/*w  w  w .j  av a 2  s .c  o  m*/
        IdInstanceNameStruct ids = getIds(db, formId);

        String whereClause = TableDefinitionsColumns.TABLE_ID + " = ?";
        String[] whereArgs = { ids.tableId };

        db.beginTransaction();

        // Drop the table used for the formId
        db.execSQL("DROP TABLE IF EXISTS " + ids.tableId + ";");

        // Delete the table definition for the tableId
        int count = db.delete(TABLE_DEFS_TABLE_NAME, whereClause, whereArgs);

        // Delete the column definitions for this tableId
        db.delete(COLUMN_DEFINITIONS_TABLE_NAME, whereClause, whereArgs);

        // Delete the uploads for the tableId
        String uploadWhereClause = InstanceColumns.DATA_TABLE_TABLE_ID + " = ?";
        db.delete(UPLOADS_TABLE_NAME, uploadWhereClause, whereArgs);

        // Delete the values from the 4 key value stores
        db.delete(KEY_VALUE_STORE_DEFAULT_TABLE_NAME, whereClause, whereArgs);
        db.delete(KEY_VALUE_STORE_ACTIVE_TABLE_NAME, whereClause, whereArgs);
        db.delete(KEY_VALUE_STORE_SERVER_TABLE_NAME, whereClause, whereArgs);
        db.delete(KEY_VALULE_STORE_SYNC_TABLE_NAME, whereClause, whereArgs);

        db.setTransactionSuccessful();

    } catch (Exception ex) {
        Log.e(t, "Exception during deletion of data for formId:" + formId + " exception: " + ex.toString());
    } finally {
        db.endTransaction();
    }
}

From source file:org.getlantern.firetweet.util.content.DatabaseUpgradeHelper.java

public static void safeUpgrade(final SQLiteDatabase db, final String table, final String[] newColNames,
        final String[] newColTypes, final boolean dropDirectly, final boolean strictMode,
        final Map<String, String> colAliases, final OnConflict onConflict) {

    if (newColNames == null || newColTypes == null || newColNames.length != newColTypes.length)
        throw new IllegalArgumentException(
                "Invalid parameters for upgrading table " + table + ", length of columns and types not match.");

    // First, create the table if not exists.
    final NewColumn[] newCols = NewColumn.createNewColumns(newColNames, newColTypes);
    final String createQuery = createTable(true, table).columns(newCols).buildSQL();
    db.execSQL(createQuery);

    // We need to get all data from old table.
    final String[] oldCols = getColumnNames(db, table);
    if (strictMode) {
        final String oldCreate = getCreateSQL(db, table);
        final Map<String, String> map = getTypeMapByCreateQuery(oldCreate);
        boolean differenct = false;
        for (final NewColumn newCol : newCols) {
            if (!newCol.getType().equalsIgnoreCase(map.get(newCol.getName()))) {
                differenct = true;//from   w ww . j a v  a2  s. c  o m
            }
        }
        if (!differenct)
            return;
    } else if (oldCols == null || FiretweetArrayUtils.contentMatch(newColNames, oldCols))
        return;
    if (dropDirectly) {
        db.beginTransaction();
        db.execSQL(dropTable(true, table).getSQL());
        db.execSQL(createQuery);
        db.setTransactionSuccessful();
        db.endTransaction();
        return;
    }
    final String tempTable = String.format(Locale.US, "temp_%s_%d", table, System.currentTimeMillis());
    db.beginTransaction();
    db.execSQL(alterTable(table).renameTo(tempTable).buildSQL());
    db.execSQL(createQuery);
    final String[] notNullCols = getNotNullColumns(newCols);
    final String insertQuery = createInsertDataQuery(table, tempTable, newColNames, oldCols, colAliases,
            notNullCols, onConflict);
    if (insertQuery != null) {
        db.execSQL(insertQuery);
    }
    db.execSQL(dropTable(true, tempTable).getSQL());
    db.setTransactionSuccessful();
    db.endTransaction();
}

From source file:org.mariotaku.twidere.util.content.DatabaseUpgradeHelper.java

public static void safeUpgrade(final SQLiteDatabase db, final String table, final String[] newColNames,
        final String[] newColTypes, final boolean dropDirectly, final boolean strictMode,
        final Map<String, String> colAliases, final OnConflict onConflict) {

    if (newColNames == null || newColTypes == null || newColNames.length != newColTypes.length)
        throw new IllegalArgumentException(
                "Invalid parameters for upgrading table " + table + ", length of columns and types not match.");

    // First, create the table if not exists.
    final NewColumn[] newCols = NewColumn.createNewColumns(newColNames, newColTypes);
    final String createQuery = createTable(true, table).columns(newCols).buildSQL();
    db.execSQL(createQuery);

    // We need to get all data from old table.
    final String[] oldCols = getColumnNames(db, table);
    if (strictMode) {
        final String oldCreate = getCreateSQL(db, table);
        final Map<String, String> map = getTypeMapByCreateQuery(oldCreate);
        boolean different = false;
        for (final NewColumn newCol : newCols) {
            if (!newCol.getType().equalsIgnoreCase(map.get(newCol.getName()))) {
                different = true;/*from w w w  .  ja v a2  s. co m*/
            }
        }
        if (!different)
            return;
    } else if (oldCols == null || TwidereArrayUtils.contentMatch(newColNames, oldCols))
        return;
    if (dropDirectly) {
        db.beginTransaction();
        db.execSQL(dropTable(true, table).getSQL());
        db.execSQL(createQuery);
        db.setTransactionSuccessful();
        db.endTransaction();
        return;
    }
    final String tempTable = String.format(Locale.US, "temp_%s_%d", table, System.currentTimeMillis());
    db.beginTransaction();
    db.execSQL(alterTable(table).renameTo(tempTable).buildSQL());
    db.execSQL(createQuery);
    final String[] notNullCols = getNotNullColumns(newCols);
    final String insertQuery = createInsertDataQuery(table, tempTable, newColNames, oldCols, colAliases,
            notNullCols, onConflict);
    if (insertQuery != null) {
        db.execSQL(insertQuery);
    }
    db.execSQL(dropTable(true, tempTable).getSQL());
    db.setTransactionSuccessful();
    db.endTransaction();
}

From source file:com.contentful.vault.SqliteHelper.java

static void deleteTables(SQLiteDatabase db) {
    String[] columns = new String[] { "name" };
    String selection = "type = ? AND name != ?";
    String[] args = new String[] { "table", "android_metadata" };
    Cursor cursor = db.query("sqlite_master", columns, selection, args, null, null, null);
    List<String> tables = null;
    try {//from ww  w .  j a v a2  s.  c o  m
        if (cursor.moveToFirst()) {
            tables = new ArrayList<>();
            do {
                tables.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }
    if (tables != null) {
        db.beginTransaction();
        try {
            for (String table : tables) {
                db.execSQL("DROP TABLE " + escape(table));
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static void unBanUser(String decodedfrom, Context context) {

    DBHelper db = new DBHelper(context);
    SQLiteDatabase dbwrite = db.getWritableDatabase();

    dbwrite.execSQL("DELETE FROM banned_users WHERE name=" + esc(decodedfrom));
    dbwrite.close();//from  w w w .j a va 2 s  .c  om
    db.close();
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static void unBanThread(String group, String clean_subject, Context context) {
    int groupid = getGroupIdFromName(group, context);

    DBHelper db = new DBHelper(context);
    SQLiteDatabase dbwrite = db.getWritableDatabase();

    dbwrite.execSQL("DELETE FROM banned_threads WHERE subscribed_group_id=" + groupid + " AND clean_subject="
            + esc(clean_subject));/* w  ww.  ja  v  a2  s.  c om*/
    dbwrite.close();
    db.close();

}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static void updateUnreadInGroupsTable(int unreadCount, int groupid, Context context) {
    DBHelper dbhelper = new DBHelper(context);
    SQLiteDatabase dbwriter = dbhelper.getWritableDatabase();
    dbwriter.execSQL("UPDATE subscribed_groups SET unread_count=" + unreadCount + " WHERE _id=" + groupid);
    dbwriter.close();//  w  w w .j a  v  a2 s . co m
    dbhelper.close();
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static void markAsRead(long server_article_number, Context context) {
    DBHelper dbhelper = new DBHelper(context);
    SQLiteDatabase dbwriter = dbhelper.getWritableDatabase();
    dbwriter.execSQL("UPDATE headers SET read=1, read_unixdate=" + System.currentTimeMillis()
            + " WHERE server_article_number=" + server_article_number);
    dbwriter.close();//from  w  ww .  j ava 2 s . c o m
    dbhelper.close();
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static void restartAllGroupsMessages(Context context) {

    DBHelper db = new DBHelper(context);
    SQLiteDatabase dbwrite = db.getWritableDatabase();

    dbwrite.execSQL("DELETE FROM headers");
    dbwrite.execSQL("UPDATE subscribed_groups SET lastFetched=-1, unread_count=0");
    FSUtils.deleteDirectory(//from ww  w . java  2s.  co  m
            UsenetConstants.EXTERNALSTORAGE + "/" + UsenetConstants.APPNAME + "/offlinecache/groups");
    FSUtils.deleteDirectory(UsenetConstants.EXTERNALSTORAGE + "/" + UsenetConstants.APPNAME + "/"
            + UsenetConstants.ATTACHMENTSDIR);
    dbwrite.close();
    db.close();
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static void markAsUnRead(long server_article_number, Context context) {

    DBHelper dbhelper = new DBHelper(context);
    SQLiteDatabase dbwriter = dbhelper.getWritableDatabase();
    dbwriter.execSQL(
            "UPDATE headers SET read=0, read_unixdate=0 WHERE server_article_number=" + server_article_number);
    dbwriter.close();// w w w.ja  v a2s .  co m
    dbhelper.close();
}