Created
July 23, 2017 16:36
-
-
Save chyrta/1348f329dec4ebd9f77204a0386580c0 to your computer and use it in GitHub Desktop.
MyDatabase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; | |
import com.j256.ormlite.dao.Dao; | |
import com.j256.ormlite.support.ConnectionSource; | |
import com.j256.ormlite.table.TableUtils; | |
import java.sql.SQLException; | |
import javax.inject.Inject; | |
import javax.inject.Singleton; | |
@Singleton | |
public class MyDatabase extends OrmLiteSqliteOpenHelper { | |
private static final String DATABASE_NAME = "MyDatabase.db"; | |
private static final int DATABASE_VERSION = 2; | |
private Dao<TokenTable, String> token_dao; | |
@Inject public MyDatabase(Context context) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
@Override public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) { | |
try { | |
TableUtils.createTable(connectionSource, TokenTable.class); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override public void onUpgrade(SQLiteDatabase database, | |
ConnectionSource connectionSource, | |
int oldVersion, int newVersion) { | |
try { | |
TableUtils.dropTable(connectionSource, TokenTable.class, true); | |
TableUtils.createTable(connectionSource, TokenTable.class); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public Dao<TokenTable, String> getTokenTableDao() { | |
try { | |
if (token_dao == null) { | |
token_dao = getDao(TokenTable.class); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return api_token_dao; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment