List of usage examples for android.database.sqlite SQLiteDatabase execSQL
public void execSQL(String sql) throws SQLException
From source file:edu.stanford.mobisocial.dungbeetle.DBHelper.java
private void createTable(SQLiteDatabase db, String tableName, String[] uniqueCols, String... cols) { assert cols.length % 2 == 0; String s = "CREATE TABLE " + tableName + " ("; for (int i = 0; i < cols.length; i += 2) { s += cols[i] + " " + cols[i + 1]; if (i < (cols.length - 2)) { s += ", "; } else {/*from w w w .j ava 2s .c o m*/ s += " "; } } if (uniqueCols != null && uniqueCols.length > 0) { s += ", UNIQUE (" + Util.join(uniqueCols, ",") + ")"; } s += ")"; Log.i(TAG, s); db.execSQL(s); }
From source file:edu.stanford.mobisocial.dungbeetle.DBHelper.java
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion); if (oldVersion <= 23) { Log.w(TAG, "Schema too old to migrate, dropping all."); dropAll(db);// w w w .ja v a2 s . c om onCreate(db); return; } if (oldVersion <= 24) { Log.w(TAG, "Adding columns 'presence' and 'status' to contact table."); db.execSQL("ALTER TABLE " + Contact.TABLE + " ADD COLUMN " + Contact.STATUS + " TEXT"); db.execSQL("ALTER TABLE " + Contact.TABLE + " ADD COLUMN " + Contact.PRESENCE + " INTEGER DEFAULT " + Presence.AVAILABLE); } if (oldVersion <= 25) { Log.w(TAG, "Adding columns 'presence' and 'status' to contact table."); db.execSQL("ALTER TABLE " + Group.TABLE + " ADD COLUMN " + Group.FEED_NAME + " TEXT"); } if (oldVersion <= 26) { Log.w(TAG, "Adding column 'picture' to contact table."); db.execSQL("ALTER TABLE " + Contact.TABLE + " ADD COLUMN " + Contact.PICTURE + " BLOB"); } if (oldVersion <= 27) { Log.w(TAG, "Adding column 'last_presence_time' to contact table."); db.execSQL("ALTER TABLE " + Contact.TABLE + " ADD COLUMN " + Contact.LAST_PRESENCE_TIME + " INTEGER DEFAULT 0"); } if (oldVersion <= 28) { Log.w(TAG, "Adding column 'picture' to my_info table."); db.execSQL("ALTER TABLE " + MyInfo.TABLE + " ADD COLUMN " + MyInfo.PICTURE + " BLOB"); } if (oldVersion <= 29) { Log.w(TAG, "Adding column 'version' to group table."); db.execSQL("ALTER TABLE " + Group.TABLE + " ADD COLUMN " + Group.VERSION + " INTEGER DEFAULT -1"); } if (oldVersion <= 30) { Log.w(TAG, "Adding column 'E' to object table."); db.execSQL("ALTER TABLE " + DbObject.TABLE + " ADD COLUMN " + DbObject.ENCODED + " BLOB"); createIndex(db, "INDEX", "objects_by_encoded", DbObject.TABLE, DbObject.ENCODED); } if (oldVersion <= 31) { Log.w(TAG, "Adding column 'child_feed' to object table."); db.execSQL("ALTER TABLE " + DbObject.TABLE + " ADD COLUMN " + DbObject.CHILD_FEED_NAME + " TEXT"); createIndex(db, "INDEX", "child_feeds", DbObject.TABLE, DbObject.CHILD_FEED_NAME); } if (oldVersion <= 32) { // Bug fix. Log.w(TAG, "Updating app state objects."); db.execSQL("UPDATE " + DbObject.TABLE + " SET " + DbObject.CHILD_FEED_NAME + " = NULL WHERE " + DbObject.CHILD_FEED_NAME + " = " + DbObject.FEED_NAME); } if (oldVersion <= 33) { Log.w(TAG, "Adding column 'nearby' to contact table."); db.execSQL("ALTER TABLE " + Contact.TABLE + " ADD COLUMN " + Contact.NEARBY + " INTEGER DEFAULT 0"); } if (oldVersion <= 34) { Log.w(TAG, "Adding column 'secret' to contact table."); db.execSQL("ALTER TABLE " + Contact.TABLE + " ADD COLUMN " + Contact.SHARED_SECRET + " BLOB"); } if (oldVersion <= 35) { Log.w(TAG, "Adding column 'last_updated' to group table."); db.execSQL("ALTER TABLE " + Group.TABLE + " ADD COLUMN " + Group.LAST_UPDATED + " INTEGER"); } if (oldVersion <= 36) { // Can't easily drop columns, but 'update_id' and 'is_child_feed' are dead columns. Log.w(TAG, "Adding column 'parent_feed_id' to group table."); db.execSQL( "ALTER TABLE " + Group.TABLE + " ADD COLUMN " + Group.PARENT_FEED_ID + " INTEGER DEFAULT -1"); Log.w(TAG, "Adding column 'last_object_id' to group table."); db.execSQL( "ALTER TABLE " + Group.TABLE + " ADD COLUMN " + Group.LAST_OBJECT_ID + " INTEGER DEFAULT -1"); } if (oldVersion <= 37) { // Can't easily drop columns, but 'update_id' and 'is_child_feed' are dead columns. Log.w(TAG, "Adding column 'num_unread' to group table."); db.execSQL("ALTER TABLE " + Group.TABLE + " ADD COLUMN " + Group.NUM_UNREAD + " INTEGER DEFAULT 0"); } if (oldVersion <= 38) { Log.w(TAG, "Adding column 'raw' to object table."); db.execSQL("ALTER TABLE " + DbObject.TABLE + " ADD COLUMN " + DbObject.RAW + " BLOB"); } // sadly, we have to do this again because incoming voice obj's were not being split! if (oldVersion <= 50) { Log.w(TAG, "Converting voice and picture objs to raw."); Log.w(TAG, "Converting objs to raw."); Cursor c = db.query(DbObject.TABLE, new String[] { DbObject._ID }, DbObject.TYPE + " = ? AND " + DbObject.RAW + " IS NULL", new String[] { PictureObj.TYPE }, null, null, null); ArrayList<Long> ids = new ArrayList<Long>(); if (c.moveToFirst()) do { ids.add(c.getLong(0)); } while (c.moveToNext()); c.close(); DbEntryHandler dbh = DbObjects.forType(PictureObj.TYPE); for (Long id : ids) { c = db.query(DbObject.TABLE, new String[] { DbObject.JSON, DbObject.RAW }, DbObject._ID + " = ? ", new String[] { String.valueOf(id.longValue()) }, null, null, null); if (c.moveToFirst()) try { String json = c.getString(0); byte[] raw = c.getBlob(1); c.close(); if (raw == null) { Pair<JSONObject, byte[]> p = dbh.splitRaw(new JSONObject(json)); if (p != null) { json = p.first.toString(); raw = p.second; updateJsonAndRaw(db, id, json, raw); } } } catch (JSONException e) { } c.close(); } c = db.query(DbObject.TABLE, new String[] { DbObject._ID }, DbObject.TYPE + " = ? AND " + DbObject.RAW + " IS NULL", new String[] { VoiceObj.TYPE }, null, null, null); ids = new ArrayList<Long>(); if (c.moveToFirst()) do { ids.add(c.getLong(0)); } while (c.moveToNext()); c.close(); dbh = DbObjects.forType(VoiceObj.TYPE); for (Long id : ids) { c = db.query(DbObject.TABLE, new String[] { DbObject.JSON, DbObject.RAW }, DbObject._ID + " = ? ", new String[] { String.valueOf(id.longValue()) }, null, null, null); if (c.moveToFirst()) try { String json = c.getString(0); byte[] raw = c.getBlob(1); c.close(); if (raw == null) { Pair<JSONObject, byte[]> p = dbh.splitRaw(new JSONObject(json)); if (p != null) { json = p.first.toString(); raw = p.second; updateJsonAndRaw(db, id, json, raw); } } } catch (JSONException e) { } c.close(); } } if (oldVersion <= 40) { Log.w(TAG, "Adding column 'E' to object table."); db.execSQL("ALTER TABLE " + DbObject.TABLE + " ADD COLUMN " + DbObject.HASH + " INTEGER"); createIndex(db, "INDEX", "objects_by_hash", DbObject.TABLE, DbObject.HASH); db.execSQL("DROP INDEX objects_by_encoded"); db.delete(DbObject.TABLE, DbObject.TYPE + " = ?", new String[] { "profile" }); db.delete(DbObject.TABLE, DbObject.TYPE + " = ?", new String[] { "profilepicture" }); ContentValues cv = new ContentValues(); cv.putNull(DbObject.ENCODED); db.update(DbObject.TABLE, cv, null, null); } if (oldVersion <= 41) { db.execSQL("DROP INDEX objects_by_sequence_id"); db.execSQL("CREATE INDEX objects_by_sequence_id ON " + DbObject.TABLE + "(" + DbObject.CONTACT_ID + ", " + DbObject.FEED_NAME + ", " + DbObject.SEQUENCE_ID + ")"); } //secret to life, etc if (oldVersion <= 42) { db.execSQL("DROP INDEX objects_by_creator_id"); db.execSQL("CREATE INDEX objects_by_creator_id ON " + DbObject.TABLE + "(" + DbObject.CONTACT_ID + ", " + DbObject.SENT + ")"); } if (oldVersion <= 44) { // oops. db.execSQL("DROP TABLE IF EXISTS " + DbRelation.TABLE); createRelationBaseTable(db); } if (oldVersion <= 45) { db.execSQL("ALTER TABLE " + Contact.TABLE + " ADD COLUMN " + Contact.LAST_OBJECT_ID + " INTEGER"); db.execSQL("ALTER TABLE " + Contact.TABLE + " ADD COLUMN " + Contact.LAST_UPDATED + " INTEGER"); db.execSQL("ALTER TABLE " + Contact.TABLE + " ADD COLUMN " + Contact.NUM_UNREAD + " INTEGER DEFAULT 0"); } if (oldVersion <= 46) { db.execSQL("ALTER TABLE " + DbObject.TABLE + " ADD COLUMN " + DbObject.DELETED + " INTEGER DEFAULT 0"); } if (oldVersion <= 47) { addRelationIndexes(db); } if (oldVersion <= 44) { createUserAttributesTable(db); } if (oldVersion <= 49) { if (oldVersion > 44) { db.execSQL("ALTER TABLE " + DbRelation.TABLE + " ADD COLUMN " + DbRelation.RELATION_TYPE + " TEXT"); createIndex(db, "INDEX", "relations_by_type", DbRelation.TABLE, DbRelation.RELATION_TYPE); } db.execSQL("UPDATE " + DbRelation.TABLE + " SET " + DbRelation.RELATION_TYPE + " = 'parent'"); } if (oldVersion <= 52) { Log.w(TAG, "Adding column 'about' to my_info table."); try { db.execSQL("ALTER TABLE " + MyInfo.TABLE + " ADD COLUMN " + MyInfo.ABOUT + " TEXT DEFAULT ''"); } catch (Exception e) { // because of bad update, we just ignore the duplicate column error } } if (oldVersion <= 53) { db.execSQL("ALTER TABLE " + Contact.TABLE + " ADD COLUMN " + Contact.HIDDEN + " INTEGER DEFAULT 0"); } if (oldVersion <= 55) { db.execSQL("ALTER TABLE " + DbObj.TABLE + " ADD COLUMN " + DbObj.COL_KEY_INT + " INTEGER"); } if (oldVersion <= 56) { db.execSQL("DROP INDEX attrs_by_contact_id"); createIndex(db, "INDEX", "attrs_by_contact_id", DbContactAttributes.TABLE, DbContactAttributes.CONTACT_ID); } if (oldVersion <= 57) { db.execSQL("ALTER TABLE " + DbObject.TABLE + " ADD COLUMN " + DbObject.LAST_MODIFIED_TIMESTAMP + " INTEGER"); db.execSQL("UPDATE " + DbObject.TABLE + " SET " + DbObject.LAST_MODIFIED_TIMESTAMP + " = " + DbObject.TIMESTAMP); } if (oldVersion <= 58) { db.execSQL("ALTER TABLE " + Group.TABLE + " ADD COLUMN " + Group.GROUP_TYPE + " TEXT DEFAULT 'group'"); db.execSQL("UPDATE " + Group.TABLE + " SET " + Group.GROUP_TYPE + " = 'group'"); } if (oldVersion <= 59) { createIndex(db, "INDEX", "objects_last_modified", DbObject.TABLE, DbObject.LAST_MODIFIED_TIMESTAMP); } if (oldVersion <= 60) { db.execSQL("ALTER TABLE " + Contact.TABLE + " ADD COLUMN " + Contact.PUBLIC_KEY_HASH_64 + " INTEGER DEFAULT 0"); createIndex(db, "INDEX", "contacts_by_pkp", Contact.TABLE, Contact.PUBLIC_KEY_HASH_64); Cursor peeps = db .rawQuery("SELECT " + Contact._ID + "," + Contact.PUBLIC_KEY + " FROM " + Contact.TABLE, null); peeps.moveToFirst(); while (!peeps.isAfterLast()) { db.execSQL("UPDATE " + Contact.TABLE + " SET " + Contact.PUBLIC_KEY_HASH_64 + " = " + hashPublicKey(peeps.getBlob(1)) + " WHERE " + Contact._ID + " = " + peeps.getLong(0)); peeps.moveToNext(); } peeps.close(); } db.setVersion(VERSION); }
From source file:org.digitalcampus.oppia.application.DbHelper.java
public void createCourseTable(SQLiteDatabase db) { String m_sql = "create table " + COURSE_TABLE + " (" + COURSE_C_ID + " integer primary key autoincrement, " + COURSE_C_VERSIONID + " int, " + COURSE_C_TITLE + " text, " + COURSE_C_LOCATION + " text, " + COURSE_C_SHORTNAME + " text," + COURSE_C_SCHEDULE + " int," + COURSE_C_IMAGE + " text," + COURSE_C_DESC + " text," + COURSE_C_ORDER_PRIORITY + " integer default 0, " + COURSE_C_LANGS + " text)"; db.execSQL(m_sql); }
From source file:org.digitalcampus.oppia.application.DbHelper.java
public void createActivityTable(SQLiteDatabase db) { String a_sql = "create table " + ACTIVITY_TABLE + " (" + ACTIVITY_C_ID + " integer primary key autoincrement, " + ACTIVITY_C_COURSEID + " int, " + ACTIVITY_C_SECTIONID + " int, " + ACTIVITY_C_ACTID + " int, " + ACTIVITY_C_ACTTYPE + " text, " + ACTIVITY_C_STARTDATE + " datetime null, " + ACTIVITY_C_ENDDATE + " datetime null, " + ACTIVITY_C_ACTIVITYDIGEST + " text, " + ACTIVITY_C_TITLE + " text)"; db.execSQL(a_sql); }
From source file:org.digitalcampus.oppia.application.DbHelper.java
public void createClientTrackerTable(SQLiteDatabase db) { String sql = "CREATE TABLE [" + CLIENT_TRACKER_TABLE + "] (" + "[" + CLIENT_TRACKER_C_ID + "]" + " integer primary key autoincrement, " + "[" + CLIENT_TRACKER_C_START + "]" + " integer default 0, " + "[" + CLIENT_TRACKER_C_END + "] integer default 0, " + "[" + CLIENT_TRACKER_C_CLIENT + "] integer , " + "[" + CLIENT_TRACKER_C_USER + "] text , " + // "["+CLIENT_TRACKER_C_ISSENT +"] integer default 0 ," + "[" + CLIENT_TRACKER_C_CLIENTSTATUS + "] integer default 0 " + ");"; db.execSQL(sql); }
From source file:it.bradipao.berengar.DbTool.java
public static int gson2db(SQLiteDatabase mDB, File jsonFile) { // vars// w w w.j a v a 2 s. c om int iTableNum = 0; FileReader fr = null; BufferedReader br = null; JsonReader jr = null; String name = null; String val = null; String mTable = null; String mTableSql = null; ArrayList<String> aFields = null; ArrayList<String> aValues = null; ContentValues cv = null; // file readers try { fr = new FileReader(jsonFile); br = new BufferedReader(fr); jr = new JsonReader(br); } catch (FileNotFoundException e) { Log.e(LOGTAG, "error in gson2db file readers", e); } // parsing try { // start database transaction mDB.beginTransaction(); // open root { jr.beginObject(); // iterate through root objects while (jr.hasNext()) { name = jr.nextName(); if (jr.peek() == JsonToken.NULL) jr.skipValue(); // number of tables else if (name.equals("tables_num")) { val = jr.nextString(); iTableNum = Integer.parseInt(val); if (GOLOG) Log.d(LOGTAG, "TABLE NUM : " + iTableNum); } // iterate through tables array else if (name.equals("tables")) { jr.beginArray(); while (jr.hasNext()) { // start table mTable = null; aFields = null; jr.beginObject(); while (jr.hasNext()) { name = jr.nextName(); if (jr.peek() == JsonToken.NULL) jr.skipValue(); // table name else if (name.equals("table_name")) { mTable = jr.nextString(); } // table sql else if (name.equals("table_sql")) { mTableSql = jr.nextString(); if ((mTable != null) && (mTableSql != null)) { mDB.execSQL("DROP TABLE IF EXISTS " + mTable); mDB.execSQL(mTableSql); if (GOLOG) Log.d(LOGTAG, "DROPPED AND CREATED TABLE : " + mTable); } } // iterate through columns name else if (name.equals("cols_name")) { jr.beginArray(); while (jr.hasNext()) { val = jr.nextString(); if (aFields == null) aFields = new ArrayList<String>(); aFields.add(val); } jr.endArray(); if (GOLOG) Log.d(LOGTAG, "COLUMN NAME : " + aFields.toString()); } // iterate through rows else if (name.equals("rows")) { jr.beginArray(); while (jr.hasNext()) { jr.beginArray(); // iterate through values in row aValues = null; cv = null; while (jr.hasNext()) { val = jr.nextString(); if (aValues == null) aValues = new ArrayList<String>(); aValues.add(val); } jr.endArray(); // add to database cv = new ContentValues(); for (int j = 0; j < aFields.size(); j++) cv.put(aFields.get(j), aValues.get(j)); mDB.insert(mTable, null, cv); if (GOLOG) Log.d(LOGTAG, "INSERT IN " + mTable + " : " + aValues.toString()); } jr.endArray(); } else jr.skipValue(); } // end table jr.endObject(); } jr.endArray(); } else jr.skipValue(); } // close root } jr.endObject(); jr.close(); // successfull transaction mDB.setTransactionSuccessful(); } catch (IOException e) { Log.e(LOGTAG, "error in gson2db gson parsing", e); } finally { mDB.endTransaction(); } return iTableNum; }
From source file:org.digitalcampus.oppia.application.DbHelper.java
public void createLogTable(SQLiteDatabase db) { String l_sql = "create table " + TRACKER_LOG_TABLE + " (" + TRACKER_LOG_C_ID + " integer primary key autoincrement, " + TRACKER_LOG_C_COURSEID + " integer, " + TRACKER_LOG_C_DATETIME + " datetime default current_timestamp, " + TRACKER_LOG_C_ACTIVITYDIGEST + " text, " + TRACKER_LOG_C_DATA + " text, " + TRACKER_LOG_C_SUBMITTED + " integer default 0, " + TRACKER_LOG_C_INPROGRESS + " integer default 0, " + TRACKER_LOG_C_COMPLETED + " integer default 0, " + TRACKER_LOG_C_USERID + " integer default 0 " + ")"; db.execSQL(l_sql); }
From source file:org.digitalcampus.oppia.application.DbHelper.java
public void createUserTable(SQLiteDatabase db) { String sql = "CREATE TABLE [" + USER_TABLE + "] (" + "[" + USER_C_ID + "]" + " integer primary key autoincrement, " + "[" + USER_C_USERNAME + "]" + " TEXT, " + "[" + USER_C_FIRSTNAME + "] TEXT, " + "[" + USER_C_LASTNAME + "] TEXT, " + "[" + USER_C_PASSWORDENCRYPTED + "] TEXT, " + "[" + USER_C_APIKEY + "] TEXT, " + "[" + USER_C_LAST_LOGIN_DATE + "] datetime null, " + "[" + USER_C_NO_LOGINS + "] integer default 0, " + "[" + USER_C_POINTS + "] integer default 0, " + "[" + USER_C_BADGES + "] integer default 0 " + ");"; db.execSQL(sql); }
From source file:org.digitalcampus.oppia.application.DbHelper.java
public void createQuizAttemptsTable(SQLiteDatabase db) { String sql = "create table " + QUIZATTEMPTS_TABLE + " (" + QUIZATTEMPTS_C_ID + " integer primary key autoincrement, " + QUIZATTEMPTS_C_DATETIME + " datetime default current_timestamp, " + QUIZATTEMPTS_C_DATA + " text, " + QUIZATTEMPTS_C_ACTIVITY_DIGEST + " text, " + QUIZATTEMPTS_C_SENT + " integer default 0, " + QUIZATTEMPTS_C_COURSEID + " integer, " + QUIZATTEMPTS_C_USERID + " integer default 0, " + QUIZATTEMPTS_C_SCORE + " real default 0, " + QUIZATTEMPTS_C_MAXSCORE + " real default 0, " + QUIZATTEMPTS_C_PASSED + " integer default 0)"; db.execSQL(sql); }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * Drop the given tableId and remove all the files (both configuration and * data attachments) associated with that table. * //from w ww . j a v a 2 s.co m * @param db * @param appName * @param tableId */ public void deleteDBTableAndAllData(SQLiteDatabase db, final String appName, final String tableId) { SyncETagsUtils seu = new SyncETagsUtils(); boolean dbWithinTransaction = db.inTransaction(); try { String whereClause = TableDefinitionsColumns.TABLE_ID + " = ?"; String[] whereArgs = { tableId }; if (!dbWithinTransaction) { db.beginTransaction(); } // Drop the table used for the formId db.execSQL("DROP TABLE IF EXISTS \"" + tableId + "\";"); // Delete the server sync ETags associated with this table seu.deleteAllSyncETags(db, tableId); // Delete the table definition for the tableId int count = db.delete(DatabaseConstants.TABLE_DEFS_TABLE_NAME, whereClause, whereArgs); // Delete the column definitions for this tableId db.delete(DatabaseConstants.COLUMN_DEFINITIONS_TABLE_NAME, whereClause, whereArgs); // Delete the uploads for the tableId String uploadWhereClause = InstanceColumns.DATA_TABLE_TABLE_ID + " = ?"; db.delete(DatabaseConstants.UPLOADS_TABLE_NAME, uploadWhereClause, whereArgs); // Delete the values from the 4 key value stores db.delete(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, whereClause, whereArgs); db.delete(DatabaseConstants.KEY_VALULE_STORE_SYNC_TABLE_NAME, whereClause, whereArgs); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } // And delete the files from the SDCard... String tableDir = ODKFileUtils.getTablesFolder(appName, tableId); try { FileUtils.deleteDirectory(new File(tableDir)); } catch (IOException e1) { e1.printStackTrace(); throw new IllegalStateException("Unable to delete the " + tableDir + " directory", e1); } String assetsCsvDir = ODKFileUtils.getAssetsFolder(appName) + "/csv"; try { Collection<File> files = FileUtils.listFiles(new File(assetsCsvDir), new IOFileFilter() { @Override public boolean accept(File file) { String[] parts = file.getName().split("\\."); return (parts[0].equals(tableId) && parts[parts.length - 1].equals("csv") && (parts.length == 2 || parts.length == 3 || (parts.length == 4 && parts[parts.length - 2].equals("properties")))); } @Override public boolean accept(File dir, String name) { String[] parts = name.split("\\."); return (parts[0].equals(tableId) && parts[parts.length - 1].equals("csv") && (parts.length == 2 || parts.length == 3 || (parts.length == 4 && parts[parts.length - 2].equals("properties")))); } }, new IOFileFilter() { // don't traverse into directories @Override public boolean accept(File arg0) { return false; } // don't traverse into directories @Override public boolean accept(File arg0, String arg1) { return false; } }); FileUtils.deleteDirectory(new File(tableDir)); for (File f : files) { FileUtils.deleteQuietly(f); } } catch (IOException e1) { e1.printStackTrace(); throw new IllegalStateException("Unable to delete the " + tableDir + " directory", e1); } }