Java Code Examples for com.j256.ormlite.table.TableUtils#createTableIfNotExists()

The following examples show how to use com.j256.ormlite.table.TableUtils#createTableIfNotExists() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatabaseHelper.java    From SightRemote with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
    try {
        TableUtils.createTableIfNotExists(connectionSource, BolusDelivered.class);
        TableUtils.createTableIfNotExists(connectionSource, BolusProgrammed.class);
        TableUtils.createTableIfNotExists(connectionSource, EndOfTBR.class);
        TableUtils.createTableIfNotExists(connectionSource, Offset.class);
        TableUtils.createTableIfNotExists(connectionSource, PumpStatusChanged.class);
        TableUtils.createTableIfNotExists(connectionSource, TimeChanged.class);
        TableUtils.createTableIfNotExists(connectionSource, CannulaFilled.class);
        TableUtils.createTableIfNotExists(connectionSource, DailyTotal.class);
        TableUtils.createTableIfNotExists(connectionSource, CartridgeInserted.class);
        TableUtils.createTableIfNotExists(connectionSource, BatteryInserted.class);
        TableUtils.createTableIfNotExists(connectionSource, TubeFilled.class);
        TableUtils.createTableIfNotExists(connectionSource, OccurenceOfAlert.class);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: DatabaseHelper.java    From AndroidAPS with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
    try {
        if (L.isEnabled(L.DATABASE))
            log.info("onCreate");
        TableUtils.createTableIfNotExists(connectionSource, TempTarget.class);
        TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
        TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
        TableUtils.createTableIfNotExists(connectionSource, DbRequest.class);
        TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class);
        TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
        TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
        TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class);
        TableUtils.createTableIfNotExists(connectionSource, TDD.class);
        TableUtils.createTableIfNotExists(connectionSource, InsightHistoryOffset.class);
        TableUtils.createTableIfNotExists(connectionSource, InsightBolusID.class);
        TableUtils.createTableIfNotExists(connectionSource, InsightPumpID.class);
        database.execSQL("INSERT INTO sqlite_sequence (name, seq) SELECT \"" + DATABASE_INSIGHT_BOLUS_IDS + "\", " + System.currentTimeMillis() + " " +
                "WHERE NOT EXISTS (SELECT 1 FROM sqlite_sequence WHERE name = \"" + DATABASE_INSIGHT_BOLUS_IDS + "\")");
        database.execSQL("INSERT INTO sqlite_sequence (name, seq) SELECT \"" + DATABASE_INSIGHT_PUMP_IDS + "\", " + System.currentTimeMillis() + " " +
                "WHERE NOT EXISTS (SELECT 1 FROM sqlite_sequence WHERE name = \"" + DATABASE_INSIGHT_PUMP_IDS + "\")");
    } catch (SQLException e) {
        log.error("Can't create database", e);
        throw new RuntimeException(e);
    }
}
 
Example 3
Source File: DatabaseHelper.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
public void resetExtededBoluses() {
    try {
        TableUtils.dropTable(connectionSource, ExtendedBolus.class, true);
        TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
        updateEarliestDataChange(0);
    } catch (SQLException e) {
        log.error("Unhandled exception", e);
    }
    scheduleExtendedBolusChange();
}
 
Example 4
Source File: OrmLiteDatabaseHelper.java    From android-atleap with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
    try {
        List<Class<?>> classes = getUriMatcher().getClasses();
        for (Class clazz : classes) {
            TableUtils.createTableIfNotExists(connectionSource, clazz);
        }
    } catch (SQLException e) {
        Log.e(TAG, "Cannot create database", e);
    }
}
 
Example 5
Source File: BaseOrmLiteSQLiteHelper.java    From file-downloader with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
    // 通过TableUtils这个类新建Module类对应的表
    try {
        // 通过TableUtils类创建所有的表
        for (Class<?> table : supportTables) {
            TableUtils.createTableIfNotExists(connectionSource, table);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: TreatmentService.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
public void resetTreatments() {
    try {
        TableUtils.dropTable(this.getConnectionSource(), Treatment.class, true);
        TableUtils.createTableIfNotExists(this.getConnectionSource(), Treatment.class);
        DatabaseHelper.updateEarliestDataChange(0);
    } catch (SQLException e) {
        log.error("Unhandled exception", e);
    }
    scheduleTreatmentChange(null, true);
}
 
Example 7
Source File: TreatmentService.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    try {
        if (L.isEnabled(L.DATATREATMENTS))
            log.info("onCreate");
        TableUtils.createTableIfNotExists(this.getConnectionSource(), Treatment.class);
    } catch (SQLException e) {
        log.error("Can't create database", e);
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: FoodService.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
public void resetFood() {
    try {
        TableUtils.dropTable(this.getConnectionSource(), Food.class, true);
        TableUtils.createTableIfNotExists(this.getConnectionSource(), Food.class);
    } catch (SQLException e) {
        log.error("Unhandled exception", e);
    }
    scheduleFoodChange();
}
 
Example 9
Source File: FoodService.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    try {
        if (L.isEnabled(L.DATAFOOD))
            log.info("onCreate");
        TableUtils.createTableIfNotExists(this.getConnectionSource(), Food.class);
    } catch (SQLException e) {
        log.error("Can't create database", e);
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: DatabaseHelper.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
public void resetTDDs() {
    try {
        TableUtils.dropTable(connectionSource, TDD.class, true);
        TableUtils.createTableIfNotExists(connectionSource, TDD.class);
    } catch (SQLException e) {
        log.error("Unhandled exception", e);
    }
}
 
Example 11
Source File: DatabaseHelper.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
public void resetProfileSwitch() {
    try {
        TableUtils.dropTable(connectionSource, ProfileSwitch.class, true);
        TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class);
    } catch (SQLException e) {
        log.error("Unhandled exception", e);
    }
    scheduleProfileSwitchChange();
}
 
Example 12
Source File: DatabaseHelper.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
public void resetCareportalEvents() {
    try {
        TableUtils.dropTable(connectionSource, CareportalEvent.class, true);
        TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
    } catch (SQLException e) {
        log.error("Unhandled exception", e);
    }
    scheduleCareportalEventChange();
}
 
Example 13
Source File: Database.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
public <T,ID> Dao<T, ID> createTable(Class<T> c, Observer observer) throws Exception
{
	addObserver(observer);
	TableUtils.createTableIfNotExists(source, c);
	Dao<T, ID> dao = DaoManager.createDao(source, c);
	return dao;
}
 
Example 14
Source File: DatabaseHelper.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
public void resetTemporaryBasals() {
    try {
        TableUtils.dropTable(connectionSource, TemporaryBasal.class, true);
        TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class);
        updateEarliestDataChange(0);
    } catch (SQLException e) {
        log.error("Unhandled exception", e);
    }
    VirtualPumpPlugin.getPlugin().setFakingStatus(false);
    scheduleTemporaryBasalChange();
}
 
Example 15
Source File: DatabaseHelper.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
public void resetTempTargets() {
    try {
        TableUtils.dropTable(connectionSource, TempTarget.class, true);
        TableUtils.createTableIfNotExists(connectionSource, TempTarget.class);
    } catch (SQLException e) {
        log.error("Unhandled exception", e);
    }
    scheduleTemporaryTargetChange();
}
 
Example 16
Source File: ORMLiteIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@BeforeClass
public static void setup() throws SQLException {
    connectionSource = new JdbcPooledConnectionSource("jdbc:h2:mem:myDb");
    TableUtils.createTableIfNotExists(connectionSource, Library.class);
    TableUtils.createTableIfNotExists(connectionSource, Address.class);
    TableUtils.createTableIfNotExists(connectionSource, Book.class);

    libraryDao = DaoManager.createDao(connectionSource, Library.class);

    bookDao = DaoManager.createDao(connectionSource, Book.class);
}
 
Example 17
Source File: DatabaseHelper.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
    try {
        this.oldVersion = oldVersion;
        this.newVersion = newVersion;

        if (oldVersion < 7) {
            log.info(DatabaseHelper.class.getName(), "onUpgrade");
            TableUtils.dropTable(connectionSource, TempTarget.class, true);
            TableUtils.dropTable(connectionSource, BgReading.class, true);
            TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
            TableUtils.dropTable(connectionSource, DbRequest.class, true);
            TableUtils.dropTable(connectionSource, TemporaryBasal.class, true);
            TableUtils.dropTable(connectionSource, ExtendedBolus.class, true);
            TableUtils.dropTable(connectionSource, CareportalEvent.class, true);
            TableUtils.dropTable(connectionSource, ProfileSwitch.class, true);
            onCreate(database, connectionSource);
        } else if (oldVersion < 10) {
            TableUtils.createTableIfNotExists(connectionSource, InsightHistoryOffset.class);
            TableUtils.createTableIfNotExists(connectionSource, InsightBolusID.class);
            TableUtils.createTableIfNotExists(connectionSource, InsightPumpID.class);
            database.execSQL("INSERT INTO sqlite_sequence (name, seq) SELECT \"" + DATABASE_INSIGHT_BOLUS_IDS + "\", " + System.currentTimeMillis() + " " +
                    "WHERE NOT EXISTS (SELECT 1 FROM sqlite_sequence WHERE name = \"" + DATABASE_INSIGHT_BOLUS_IDS + "\")");
            database.execSQL("INSERT INTO sqlite_sequence (name, seq) SELECT \"" + DATABASE_INSIGHT_PUMP_IDS + "\", " + System.currentTimeMillis() + " " +
                    "WHERE NOT EXISTS (SELECT 1 FROM sqlite_sequence WHERE name = \"" + DATABASE_INSIGHT_PUMP_IDS + "\")");
        } else if (oldVersion < 11) {
            database.execSQL("UPDATE sqlite_sequence SET seq = " + System.currentTimeMillis() + " WHERE name = \"" + DATABASE_INSIGHT_BOLUS_IDS + "\"");
            database.execSQL("UPDATE sqlite_sequence SET seq = " + System.currentTimeMillis() + " WHERE name = \"" + DATABASE_INSIGHT_PUMP_IDS + "\"");
        }
    } catch (SQLException e) {
        log.error("Can't drop databases", e);
        throw new RuntimeException(e);
    }
}
 
Example 18
Source File: OrmLiteHelper.java    From AndroidQuick with MIT License 5 votes vote down vote up
private void createTable() {
    try {
        TableUtils.createTableIfNotExists(connectionSource, User.class);
        TableUtils.createTableIfNotExists(connectionSource, Tag.class);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example 19
Source File: OrmLiteHelper.java    From AndroidQuick with MIT License 5 votes vote down vote up
private void update(SQLiteDatabase db, int from) {
    try {
        switch (from) {
            case 1:
                TableUtils.createTableIfNotExists(connectionSource, User.class);
                break;
            default:
                createTable();
                break;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example 20
Source File: DatabaseHelper.java    From android-viewer-for-khan-academy with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onUpgrade(final SQLiteDatabase database,
		final ConnectionSource connectionSource, final int oldVersion, final int newVersion) {
	
	
	if (oldVersion < 111) {
		// Create new Caption table.
		try {
			TableUtils.createTableIfNotExists(connectionSource, Caption.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	if (oldVersion < 114) {
		// Could instead create an ORMLite pojo for topicvideo and do this with TableUtils.
		database.execSQL("CREATE TABLE IF NOT EXISTS `topicvideo` (`_id` INTEGER PRIMARY KEY AUTOINCREMENT, `topic_id` VARCHAR, `video_id` VARCHAR )");
	}
	
	if (newVersion >= 111) {
		
		// Played with the idea of putting this all into an AsyncTask, but
		//  1) Can crash the app. For example, in 114 upgrade we add topicvideo table; if it doesn't exist and user reaches a video list, we 
		//     crash. Worse, if we do crash here the app is broken. Db version has been bumped, so this code doesn't get another chance to run.
		//  2) To prevent crash, tried showing AlertDialog. The context we get here is "not an application context", so trying to show a window
		//     (or sometimes a toast) crashes us with a null window token error. I dug deep to try to pass in a useful context with no luck.
		//     See branch temp/pass_context_to_helper.
		//  3) It doesn't take very long (truncate / reload full video and topic tables is just a few seconds, not long enough for ANR on Fire HD)
		
		new DatabaseImporter(context).import_(DATABASE_RESOURCE_ID, "temp");
		SQLiteDatabase tempDb = new TempHelper(context).getReadableDatabase();
		
		if (oldVersion < 111) {
			do111Upgrade(database, tempDb);
		}
		
		if (oldVersion < 114 && newVersion >= 114) {
			// add topicvideo table
			do114Upgrade(database, tempDb);
		}
		
		if (oldVersion < 120 && newVersion >= 120) {
			// add video.dlm_id column
			do120Upgrade(database, tempDb);
		}
		
		tempDb.close();
		context.deleteDatabase(TempHelper.DB_NAME);
		
		// Update download status from storage. This recovers any lost during library updates in 1.1.3.
		database.beginTransaction();
		try {
			for (String yid : getAllDownloadedYoutubeIds()) {
				database.execSQL("update video set download_status=? where youtube_id=?", new Object[] {Video.DL_STATUS_COMPLETE, yid});
			}
			database.setTransactionSuccessful();
		} finally {
			database.endTransaction();
		}
				
	}
		
}