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:com.cyanogenmod.eleven.provider.LocalizedStore.java

public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
    // this table was created in version 3 so call the onCreate method if oldVersion <= 2
    // in version 4 we need to recreate the SongSortcolumns table so drop the table and call
    // onCreate if oldVersion <= 3
    if (oldVersion <= 3) {
        db.execSQL("DROP TABLE IF EXISTS " + SongSortColumns.TABLE_NAME);
        onCreate(db);/*from   www .j a  v  a  2 s .com*/
    }
}

From source file:org.lastmilehealth.collect.android.tasks.BluetoothService.java

private void clearForms() {
    SQLiteDatabase db = SQLiteDatabase.openDatabase(Collect.FORMS_DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
    db.execSQL("delete from forms");
    db.close();/*w w w.  ja v a 2  s . co m*/

    deletePreferences();
    try {
        org.apache.commons.io.FileUtils.cleanDirectory(new File(Collect.FORMS_PATH));
        org.apache.commons.io.FileUtils.cleanDirectory(new File(Collect.ROLES_PATH));
    } catch (Exception e) {
    }
}

From source file:org.mitre.svmp.common.DatabaseHandler.java

private void recreateTable(int tableID, SQLiteDatabase db) {
    // try to drop the table
    try {/*  w  w  w  .  ja v a2s.c  om*/
        db.execSQL(String.format("DROP TABLE IF EXISTS %s", Tables[tableID]));
    } catch (Exception e) {
        e.printStackTrace();
    }
    // create the table again
    createTable(tableID, db);
}

From source file:com.osfans.trime.DictionaryHelper.java

private boolean importDict(InputStream is) {
    boolean success = false;
    SQLiteDatabase db = getWritableDatabase();
    db.beginTransaction();/*from w  ww . j  ava  2 s. co m*/
    try {
        String line;
        StringBuilder content = new StringBuilder();
        InputStreamReader ir = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(ir);
        while ((line = br.readLine()) != null && !line.contentEquals(fs)) {
            content.append(line);
            content.append(newline);
        }

        Yaml yaml = new Yaml();
        Map<String, Object> y = (Map<String, Object>) (yaml.load(content.toString()));
        String table = (String) y.get("name");

        db.execSQL("DROP TABLE IF EXISTS " + table);
        db.execSQL(String.format("CREATE VIRTUAL TABLE %s USING fts3(hz, py)", table));

        ContentValues initialValues = new ContentValues(2);
        int max = is.available();
        int progress = 0;
        int count = 0;
        while ((line = br.readLine()) != null) {
            if (line.startsWith(comment))
                continue;
            String[] s = line.split("\t");
            if (s.length < 2)
                continue;
            initialValues.put("hz", s[0]);
            initialValues.put("py", s[1]);
            db.insert(table, null, initialValues);
            initialValues.clear();
            count++;
            if ((count % 1000) == 0) {
                progress = max - is.available();
                mBuilder.setProgress(max, progress, false)
                        .setContentText(String.format("%d / 100", progress * 100 / max));
                mNotifyManager.notify(notify_id, mBuilder.build());
            }
        }
        is.close();
        db.setTransactionSuccessful();
        success = true;
    } catch (Exception e) {
        throw new RuntimeException("Error import dict", e);
    } finally {
        db.endTransaction();
        mNotifyManager.cancel(notify_id);
    }
    return success;
}

From source file:org.ohmage.db.DbHelper.java

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.CAMPAIGNS + " (" + Campaigns._ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Campaigns.CAMPAIGN_URN + " TEXT, "
            + Campaigns.CAMPAIGN_NAME + " TEXT, " + Campaigns.CAMPAIGN_DESCRIPTION + " TEXT, "
            + Campaigns.CAMPAIGN_CREATED + " TEXT, " + Campaigns.CAMPAIGN_DOWNLOADED + " TEXT, "
            + Campaigns.CAMPAIGN_CONFIGURATION_XML + " TEXT, " + Campaigns.CAMPAIGN_STATUS + " INTEGER, "
            + Campaigns.CAMPAIGN_ICON + " TEXT, " + Campaigns.CAMPAIGN_PRIVACY + " TEXT " + ");");

    db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.SURVEYS + " (" + Surveys._ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Surveys.CAMPAIGN_URN + " TEXT, " // cascade delete from campaigns
            + Surveys.SURVEY_ID + " TEXT, " + Surveys.SURVEY_TITLE + " TEXT, " + Surveys.SURVEY_DESCRIPTION
            + " TEXT, " + Surveys.SURVEY_SUBMIT_TEXT + " TEXT, " + Surveys.SURVEY_SHOW_SUMMARY
            + " INTEGER DEFAULT 0, " + Surveys.SURVEY_EDIT_SUMMARY + " INTEGER DEFAULT 0, "
            + Surveys.SURVEY_SUMMARY_TEXT + " TEXT, " + Surveys.SURVEY_INTRO_TEXT + " TEXT, "
            + Surveys.SURVEY_ANYTIME + " INTEGER DEFAULT 1, " + Surveys.SURVEY_STATUS + " INTEGER DEFAULT 0"
            + ");");

    db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.SURVEY_PROMPTS + " (" + SurveyPrompts._ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + SurveyPrompts.SURVEY_PID + " INTEGER, " // cascade delete from surveys
            + SurveyPrompts.SURVEY_ID + " TEXT, " + SurveyPrompts.COMPOSITE_ID + " TEXT, "
            + SurveyPrompts.PROMPT_ID + " TEXT, " + SurveyPrompts.SURVEY_PROMPT_TEXT + " TEXT, "
            + SurveyPrompts.SURVEY_PROMPT_TYPE + " TEXT, " + SurveyPrompts.SURVEY_PROMPT_PROPERTIES + " TEXT "
            + ");");

    db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.RESPONSES + " (" + Responses._ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Responses.CAMPAIGN_URN + " TEXT, " // cascade delete from campaigns
            + Responses.RESPONSE_USERNAME + " TEXT, " + Responses.RESPONSE_DATE + " TEXT, "
            + Responses.RESPONSE_TIME + " INTEGER, " + Responses.RESPONSE_TIMEZONE + " TEXT, "
            + Responses.RESPONSE_LOCATION_STATUS + " TEXT, " + Responses.RESPONSE_LOCATION_LATITUDE + " REAL, "
            + Responses.RESPONSE_LOCATION_LONGITUDE + " REAL, " + Responses.RESPONSE_LOCATION_PROVIDER
            + " TEXT, " + Responses.RESPONSE_LOCATION_ACCURACY + " REAL, " + Responses.RESPONSE_LOCATION_TIME
            + " INTEGER, " + Responses.SURVEY_ID + " TEXT, " + Responses.RESPONSE_SURVEY_LAUNCH_CONTEXT
            + " TEXT, " + Responses.RESPONSE_JSON + " TEXT, " + Responses.RESPONSE_STATUS
            + " INTEGER DEFAULT 0, " + Responses.RESPONSE_HASHCODE + " TEXT" + ");");

    // make campaign URN unique in the campaigns table
    db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS " + Campaigns.CAMPAIGN_URN + "_idx ON " + Tables.CAMPAIGNS
            + " (" + Campaigns.CAMPAIGN_URN + ");");

    // create a "flat" table of prompt responses so we can easily compute
    // aggregates
    // across multiple survey responses (and potentially prompts)
    db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.PROMPT_RESPONSES + " (" + PromptResponses._ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + PromptResponses.RESPONSE_ID + " INTEGER, " // cascade delete from responses
            + PromptResponses.COMPOSITE_ID + " TEXT, " + PromptResponses.PROMPT_ID + " TEXT, "
            + PromptResponses.PROMPT_RESPONSE_VALUE + " TEXT, " + PromptResponses.PROMPT_RESPONSE_EXTRA_VALUE
            + " TEXT" + ");");

    // for responses, index the campaign and survey ID columns, as we'll be
    // selecting on them
    db.execSQL("CREATE INDEX IF NOT EXISTS " + Responses.CAMPAIGN_URN + "_idx ON " + Tables.RESPONSES + " ("
            + Responses.CAMPAIGN_URN + ");");
    db.execSQL("CREATE INDEX IF NOT EXISTS " + Responses.SURVEY_ID + "_idx ON " + Tables.RESPONSES + " ("
            + Responses.SURVEY_ID + ");");
    // also index the time column, as we'll use that for time-related
    // queries/*from www .j  a  va 2s.  c  o  m*/
    db.execSQL("CREATE INDEX IF NOT EXISTS " + Responses.RESPONSE_TIME + "_idx ON " + Tables.RESPONSES + " ("
            + Responses.RESPONSE_TIME + ");");

    // for responses, to prevent duplicates, add a unique key on the
    // 'hashcode' column, which is just a hash of the concatentation
    // of the campaign urn + survey ID + username + time of the response,
    // computed and maintained by us, unfortunately :\
    db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS " + Responses.RESPONSE_HASHCODE + "_idx ON "
            + Tables.RESPONSES + " (" + Responses.RESPONSE_HASHCODE + ");");

    // for prompt values, index on the response id for fast lookups
    db.execSQL("CREATE INDEX IF NOT EXISTS " + PromptResponses.RESPONSE_ID + "_idx ON "
            + Tables.PROMPT_RESPONSES + " (" + PromptResponses.RESPONSE_ID + ");");

    // --------
    // --- set up the triggers to implement cascading deletes, too
    // --------

    // annoyingly, sqlite 3.5.9 doesn't support recursive triggers.
    // we must first disable them before running these statements,
    // and each trigger has to delete everything associated w/the entity in
    // question
    db.execSQL("PRAGMA recursive_triggers = off");

    // delete everything associated with a campaign when it's removed
    db.execSQL("CREATE TRIGGER IF NOT EXISTS " + Tables.CAMPAIGNS + "_cascade_del AFTER DELETE ON "
            + Tables.CAMPAIGNS + " BEGIN "

            + "DELETE from " + Tables.SURVEY_PROMPTS + " WHERE " + SurveyPrompts._ID + " IN (" + " SELECT "
            + Tables.SURVEY_PROMPTS + "." + SurveyPrompts._ID + " FROM " + Tables.SURVEY_PROMPTS + " SP"
            + " INNER JOIN " + Tables.SURVEYS + " S ON S." + Surveys._ID + "=SP." + SurveyPrompts.SURVEY_PID
            + " WHERE S." + Surveys.CAMPAIGN_URN + "=old." + Campaigns.CAMPAIGN_URN + "); "

            + "DELETE from " + Tables.PROMPT_RESPONSES + " WHERE " + PromptResponses._ID + " IN (" + " SELECT "
            + Tables.PROMPT_RESPONSES + "." + PromptResponses._ID + " FROM " + Tables.PROMPT_RESPONSES + " PR"
            + " INNER JOIN " + Tables.RESPONSES + " R ON R." + Responses._ID + "=PR."
            + PromptResponses.RESPONSE_ID + " WHERE R." + Responses.CAMPAIGN_URN + "=old."
            + Campaigns.CAMPAIGN_URN + "); "

            + "DELETE from " + Tables.SURVEYS + " WHERE " + Surveys.CAMPAIGN_URN + "=old."
            + Campaigns.CAMPAIGN_URN + "; " + "DELETE from " + Tables.RESPONSES + " WHERE "
            + Responses.CAMPAIGN_URN + "=old." + Campaigns.CAMPAIGN_URN + "; " + "END;");

    db.execSQL("CREATE TRIGGER IF NOT EXISTS " + Tables.SURVEYS + "_cascade_del AFTER DELETE ON "
            + Tables.SURVEYS + " BEGIN " + "DELETE from " + Tables.SURVEY_PROMPTS + " WHERE "
            + SurveyPrompts.SURVEY_PID + "=old." + Surveys._ID + "; " + "END;");

    db.execSQL("CREATE TRIGGER IF NOT EXISTS " + Tables.RESPONSES + "_cascade_del AFTER DELETE ON "
            + Tables.RESPONSES + " BEGIN " + "DELETE from " + Tables.PROMPT_RESPONSES + " WHERE "
            + PromptResponses.RESPONSE_ID + "=old." + Responses._ID + "; " + "END;");
}

From source file:io.vit.vitio.Managers.ConnectDatabase.java

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String CREATE_SUBJECTS_TABLE = "CREATE TABLE " + TABLE_COURSES + "(" + KEY_CLASNBR + " INTEGER PRIMARY KEY,"
            + KEY_TITLE + " TEXT," + KEY_SLOT + " TEXT," + KEY_TYPE + " TEXT," + KEY_TYPE_SHORT + " TEXT,"
            + KEY_LTPC + " TEXT," + KEY_CODE + " TEXT," + KEY_MODE + " TEXT," + KEY_OPTION + " TEXT,"
            + KEY_VENUE + " TEXT," + KEY_FACULTY + " TEXT," + KEY_REGISTRATIONSTATUS + "  TEXT," + KEY_BILL_DATE
            + " TEXT," + KEY_BILL_NUMBER + " TEXT," + KEY_PROJECT_TITLE + " TEXT," + KEY_COURSE_JSON + " TEXT,"
            + KEY_ATTENDANCE + " TEXT," + KEY_TIMINGS + " TEXT," + KEY_MARKS + " TEXT" + ");";
    sqLiteDatabase.execSQL(CREATE_SUBJECTS_TABLE);
}

From source file:com.eTilbudsavis.etasdk.DbHelper.java

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    EtaLog.i(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion
            + ", which will destroy all old data");

    synchronized (LOCK) {
        db.execSQL("DROP TABLE IF EXISTS " + LIST_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + ITEM_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + SHARE_TABLE);
    }/* w  w w.  j a  v  a  2  s . com*/

    onCreate(db);
}

From source file:com.mk4droid.IMC_Services.DatabaseHandler.java

/** OnUpgrade delete any previous tables and create them again */
@Override/*w  w w .java2s .com*/
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_Categories);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_Issues);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_IssuesPics);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_IssuesThumbs);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_Version);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CategVersion);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_Votes);

    // Create tables again
    onCreate(db);
}

From source file:org.disciple.db.Abatis.java

/**
 * DB connector/*  w  w  w .  j  a va 2s. c o m*/
 * @param db SQLiteDatabase object
 *
 */
@Override
public void onCreate(SQLiteDatabase db) {
    int pointer = context.getResources().getIdentifier(INIT_CREATE_SQL, "string", context.getPackageName());
    if (pointer == 0) {
        Log.e(TAG, "undefined sql id - initialize");
    } else {
        String[] createTabelSqls = context.getResources().getString(pointer).split(";");
        for (String sql : createTabelSqls) {
            db.execSQL(sql + ";");
        }
    }
}

From source file:pl.selvin.android.syncframework.content.BaseContentProvider.java

public void onCreateDataBase(SQLiteDatabase db) {
    try {/*from   w  ww . ja v  a2s.c  o  m*/
        for (TableInfo table : contentHelper.getAllTables()) {
            String create = table.CreateStatement();
            db.execSQL(create);
        }
        db.execSQL(String.format(
                "CREATE TABLE [%s] ([%s] VARCHAR NOT NULL, [%s] VARCHAR, [%s] LONG NOT NULL, [%s] INT NOT NULL, PRIMARY KEY([%s]))",
                BlobsTable.NAME, BlobsTable.C_NAME, BlobsTable.C_VALUE, BlobsTable.C_DATE, BlobsTable.C_STATE,
                BlobsTable.C_NAME));
    } catch (Exception e) {
        if (BuildConfig.DEBUG) {
            Log.e(BaseContentProvider.TAG, "BaseContentProvider-onCreateDataBase " + e.toString());
        }
    }
}