Dbhelper Class In Java
My solution was define a separate class called CreateTable which is called both from the OnCreate override and from the constructor after the. Db = getWritableDatabase.
importandroid.database.sqlite.SQLiteDatabase; |
importandroid.database.sqlite.SQLiteOpenHelper; |
publicclassDbHelperextendsSQLiteOpenHelper { |
publicDbHelper(Contextcontext) { |
super(context, Constants.DB_NAME, null, Constants.DB_VERSION); |
} |
@Override |
publicvoidonCreate(SQLiteDatabasedb) { |
onUpgrade(db, 0, Constants.DB_VERSION); |
} |
@Override |
publicvoidonUpgrade(SQLiteDatabasedb, intoldVersion, intnewVersion) { |
// sanity check |
if (oldVersion >= newVersion) return; |
// loop through incremental upgrades |
switch (oldVersion) { |
case0: |
// create version 1 of tables |
db.execSql('CREATE TABLE ...'); |
db.execSql('CREATE TABLE ...'); |
db.execSql('CREATE TABLE ...'); |
case1: |
// upgrade from version 1 to 2 |
db.execSql('ALTER TABLE ...'); |
db.execSql('ALTER TABLE ...'); |
case2: |
// upgrade from version 2 to 3 |
db.execSql('DROP TABLE ...'); |
case3: |
// upgrade from version 3 to 4 |
db.execSql('ALTER TABLE ...'); |
db.execSql('CREATE TABLE ...'); |
break; |
default: |
thrownewIllegalStateException('No upgrade specified for '+ oldVersion +' -> '+ newVersion); |
} |
} |
} |
/* |
What's going on here? |
- We're creating the DbHelper on lines 5-7 exactly as we did in the lecture. |
- On lines 10-12, we *don't* create our database tables. Instead, we tell Android that |
we'd like to upgrade from 'version 0' to whatever the current version is. This means |
that if someone installs your app from scratch (and therefore the database doesn't |
exist yet) they will create the initial tables from the first version of your app, |
and then go through every incremental upgrade you have specified to end up with the |
current version. In this case, 'version 0' means 'there isn't a database yet'. |
- On lines 15-46, we specify an incremental upgrade between each database version. The |
upgrade from 'version 0' to 'version 1' means creating the initial tables (3 of them, |
in this example). Each successive case specifies the upgrade from one version to the |
next (going from v1 to v2 we alter two tables; going from v2 to v3 we drop a table, |
and going from v3 to v4 we alter a table and create one more). |
- Note the lack of 'break' statements throughout the switch block. This allows the |
program flow to 'fall through' into the next case. If you run onUpgrade(db, 2, 4) to |
upgrade from version 2 to 4 (lets say the user hasn't used the app for a while between |
some of your upgrades), then it will skip case 0 and case 1, enter the flow at case 2, |
and then keep executing cases until the 'break'. This means that whatever the entry |
point, the program will always execute everything required to take the database to the |
latest version. |
- A final 'safety' measure is defined to throw an explanatory exception if the user tries |
to upgrade to a version that doesn't exist. |
Does this make sense? Awesome! Post any questions on the Slack channel. |
Mark :) |
*/ |
Document your code
Every project on GitHub comes with a version-controlled wiki to give your documentation the high level of care it deserves. It’s easy to create well-maintained, Markdown or rich text documentation alongside your code.
Sign up for free See pricing for teams and enterprisesThe Database Manager
DBhelper controls the SQLite Database. This database stores all the information about the messages. It is structured into two tables.
- Messages: Stores all the messages
- Tags: Stores the name of the tags created.
Each message row has:
- origin_mac : The mac that created the content
- last_mac : The last mac that forwarded the content
- user : The user that created the content
- message : The message
- origin_date : The date when the content were generated
- origin_time : The time when the content were generated
- last_date : The last date when the content were forwarded
- last_time : The last time when the content were forwarded
- devices : The MAC of the devices that received the content
- hits : The number of times we tried to send the content
- TTL : The number of succesfull hops that the content has
- isImage : Boolean that indicates if I have to process the filed 'messages' as text or as image
- isMine : Bolean that indicates if this content was generated by me (I could jut jeck the origin mac with my mac, but for the sake of laziness)
- Context : The Anchor Zone attached to that content, for instance a cell ID or coordinates or a WiFi Access Point.
This Class has this methods:
public static void newTag(SQLiteDatabase db, String tagname)
Creates a tag
public static void insertMessage(SQLiteDatabase db, BtMessage item)
Dbhelper Class In Java
Inserts a message on the database
public static ArrayList recoverMessagesByTag(SQLiteDatabase db, String Tag)
Recover messages with an specific Tag
public static ArrayList recoverLiveMessages(SQLiteDatabase db, int max)
Recover messages with les than the max TTL value
public static ArrayList recoverMessages(SQLiteDatabase db)
Recover all messages
public static Boolean isNew(SQLiteDatabase db, BtMessage item)
Returns true if the item is not on the database
public static void updateMessage(SQLiteDatabase db, BtMessage item)
Update the message info such that the last mac and the las date/time
public static void updateUsername(SQLiteDatabase db, String old_username, String new_username)
As the username is trivial choosen by the user (The MAC is the real Identifier of each device), we have a method to change the username on the database
public static void updateMessageDevices(SQLiteDatabase db, BtMessage item)
Updates de list of devices that has received the message
public static void updateMessageHits(SQLiteDatabase db, BtMessage item)
Updates the hits value
public static ArrayList getTags(SQLiteDatabase db)
Returns a list of Tags
public static int getNumberOfEntriesByTag(SQLiteDatabase db,String tag)
Returns the number of messages by tag
public static void deleteTag(SQLiteDatabase db, String tagname)
Removes a tag and all its messages
public static BtMessage getMessage(SQLiteDatabase db, String hash)
gets an specific message, giving its hash
public static void deleteMessage(SQLiteDatabase db,BtMessage item)
delete an specific message