-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apie tai, kaip susikurti ORM Lite duomenų bazės `Helper` klasę galima paskaityti/pažiūrėti štai čia: * [ORM use with Android](http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_4.html#Use-With-Android) * [ORM Android pavyzdžiai](http://ormlite.com/android/examples/) * [ORM Android pavyzdys (GitHub'e)](https://github.com/j256/ormlite-examples/blob/master/android/HelloAndroid/src/com/example/helloandroid/DatabaseHelper.java) Pasinaudojant `Helper` klase išsaugoti įrašą duomenų bazėje.
- Loading branch information
1 parent
a3d19b7
commit e8f589d
Showing
6 changed files
with
285 additions
and
14 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
package lt.andro.maistobankas; | ||
|
||
import android.os.Bundle; | ||
import android.support.v7.app.ActionBarActivity; | ||
import android.util.Log; | ||
|
||
import lt.andro.maistobankas.db.DatabaseHelper; | ||
import lt.andro.maistobankas.db.OrmLiteBaseActivity; | ||
|
||
/** | ||
* @author Vilius Kraujutis [email protected] | ||
* @since 2014-02-08 12:23 | ||
*/ | ||
public class BaseActivity extends ActionBarActivity { | ||
public class BaseActivity extends OrmLiteBaseActivity<DatabaseHelper> { | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
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
99 changes: 99 additions & 0 deletions
99
app/src/main/java/lt/andro/maistobankas/db/DatabaseHelper.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package lt.andro.maistobankas.db; | ||
|
||
import android.content.Context; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.util.Log; | ||
|
||
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; | ||
|
||
/** | ||
* Database helper class used to manage the creation and upgrading of your database. This class also usually provides | ||
* the DAOs used by the other classes. | ||
*/ | ||
public class DatabaseHelper extends OrmLiteSqliteOpenHelper { | ||
|
||
// name of the database file for your application -- change to something appropriate for your app | ||
private static final String DATABASE_NAME = "helloAndroid.db"; | ||
// any time you make changes to your database objects, you may have to increase the database version | ||
private static final int DATABASE_VERSION = 1; | ||
|
||
// the DAO object we use to access the SimpleData table | ||
private Dao<Item, Integer> itemDao = null; | ||
private Dao<ScannedItem, Integer> scannedItemDao = null; | ||
|
||
public DatabaseHelper(Context context) { | ||
// TODO create a config file super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config); | ||
super(context, DATABASE_NAME, null, DATABASE_VERSION); | ||
} | ||
|
||
/** | ||
* This is called when the database is first created. Usually you should call createTable statements here to create | ||
* the tables that will store your data. | ||
*/ | ||
@Override | ||
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) { | ||
try { | ||
Log.i(DatabaseHelper.class.getName(), "onCreate"); | ||
TableUtils.createTable(connectionSource, Item.class); | ||
TableUtils.createTable(connectionSource, ScannedItem.class); | ||
} catch (SQLException e) { | ||
Log.e(DatabaseHelper.class.getName(), "Can't create database", e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* This is called when your application is upgraded and it has a higher version number. This allows you to adjust | ||
* the various data to match the new version number. | ||
*/ | ||
@Override | ||
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) { | ||
try { | ||
Log.i(DatabaseHelper.class.getName(), "onUpgrade"); | ||
TableUtils.dropTable(connectionSource, Item.class, true); | ||
TableUtils.dropTable(connectionSource, ScannedItem.class, true); | ||
// after we drop the old databases, we create the new ones | ||
onCreate(db, connectionSource); | ||
} catch (SQLException e) { | ||
Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached | ||
* value. | ||
*/ | ||
public Dao<Item, Integer> getItemDao() throws SQLException { | ||
if (itemDao == null) { | ||
itemDao = getDao(Item.class); | ||
} | ||
return itemDao; | ||
} | ||
|
||
/** | ||
* Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached | ||
* value. | ||
*/ | ||
public Dao<ScannedItem, Integer> getScannedItemDao() throws SQLException { | ||
if (scannedItemDao == null) { | ||
scannedItemDao = getDao(ScannedItem.class); | ||
} | ||
return scannedItemDao; | ||
} | ||
|
||
/** | ||
* Close the database connections and clear any cached DAOs. | ||
*/ | ||
@Override | ||
public void close() { | ||
super.close(); | ||
itemDao = null; | ||
scannedItemDao = null; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
app/src/main/java/lt/andro/maistobankas/db/OrmLiteBaseActivity.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package lt.andro.maistobankas.db; | ||
|
||
import android.content.Context; | ||
import android.os.Bundle; | ||
import android.support.v7.app.ActionBarActivity; | ||
|
||
import com.j256.ormlite.android.apptools.OpenHelperManager; | ||
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; | ||
import com.j256.ormlite.logger.Logger; | ||
import com.j256.ormlite.logger.LoggerFactory; | ||
import com.j256.ormlite.support.ConnectionSource; | ||
|
||
/** | ||
* Base class to use for activities in Android. | ||
* <p/> | ||
* You can simply call {@link #getHelper()} to get your helper class, or {@link #getConnectionSource()} to get a | ||
* {@link ConnectionSource}. | ||
* <p/> | ||
* The method {@link #getHelper()} assumes you are using the default helper factory -- see {@link OpenHelperManager}. If | ||
* not, you'll need to provide your own helper instances which will need to implement a reference counting scheme. This | ||
* method will only be called if you use the database, and only called once for this activity's life-cycle. 'close' will | ||
* also be called once for each call to createInstance. | ||
* | ||
* @author graywatson, kevingalligan | ||
*/ | ||
public abstract class OrmLiteBaseActivity<H extends OrmLiteSqliteOpenHelper> extends ActionBarActivity { | ||
|
||
private volatile H helper; | ||
private volatile boolean created = false; | ||
private volatile boolean destroyed = false; | ||
private static Logger logger = LoggerFactory.getLogger(OrmLiteBaseActivity.class); | ||
|
||
/** | ||
* Get a helper for this action. | ||
*/ | ||
public H getHelper() { | ||
if (helper == null) { | ||
if (!created) { | ||
throw new IllegalStateException("A call has not been made to onCreate() yet so the helper is null"); | ||
} else if (destroyed) { | ||
throw new IllegalStateException( | ||
"A call to onDestroy has already been made and the helper cannot be used after that point"); | ||
} else { | ||
throw new IllegalStateException("Helper is null for some unknown reason"); | ||
} | ||
} else { | ||
return helper; | ||
} | ||
} | ||
|
||
/** | ||
* Get a connection source for this action. | ||
*/ | ||
public ConnectionSource getConnectionSource() { | ||
return getHelper().getConnectionSource(); | ||
} | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
if (helper == null) { | ||
helper = getHelperInternal(this); | ||
created = true; | ||
} | ||
super.onCreate(savedInstanceState); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
releaseHelper(helper); | ||
destroyed = true; | ||
} | ||
|
||
/** | ||
* This is called internally by the class to populate the helper object instance. This should not be called directly | ||
* by client code unless you know what you are doing. Use {@link #getHelper()} to get a helper instance. If you are | ||
* managing your own helper creation, override this method to supply this activity with a helper instance. | ||
* <p/> | ||
* <p> | ||
* <b> NOTE: </b> If you override this method, you most likely will need to override the | ||
* {@link #releaseHelper(OrmLiteSqliteOpenHelper)} method as well. | ||
* </p> | ||
*/ | ||
protected H getHelperInternal(Context context) { | ||
@SuppressWarnings({"unchecked", "deprecation"}) | ||
H newHelper = (H) OpenHelperManager.getHelper(context); | ||
logger.trace("{}: got new helper {} from OpenHelperManager", this, newHelper); | ||
return newHelper; | ||
} | ||
|
||
/** | ||
* Release the helper instance created in {@link #getHelperInternal(Context)}. You most likely will not need to call | ||
* this directly since {@link #onDestroy()} does it for you. | ||
* <p/> | ||
* <p> | ||
* <b> NOTE: </b> If you override this method, you most likely will need to override the | ||
* {@link #getHelperInternal(Context)} method as well. | ||
* </p> | ||
*/ | ||
protected void releaseHelper(H helper) { | ||
OpenHelperManager.releaseHelper(); | ||
logger.trace("{}: helper {} was released, set to null", this, helper); | ||
this.helper = null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + "@" + Integer.toHexString(super.hashCode()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,20 +3,22 @@ | |
import com.j256.ormlite.field.DatabaseField; | ||
import com.j256.ormlite.table.DatabaseTable; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* @author Vilius Kraujutis [email protected] | ||
* @since 2014-02-08 14:50 | ||
*/ | ||
@DatabaseTable(tableName = "scanned_item") | ||
public class ScannedItem { | ||
|
||
@DatabaseField(id = true) | ||
@DatabaseField(generatedId = true) | ||
private long id; | ||
|
||
@DatabaseField(canBeNull = false) | ||
private String barcode; | ||
@DatabaseField(canBeNull = false) | ||
private long timestamp; | ||
private Date time; | ||
private String place; | ||
private String volunteer; | ||
|
||
|
@@ -36,12 +38,12 @@ public void setBarcode(String barcode) { | |
this.barcode = barcode; | ||
} | ||
|
||
public long getTimestamp() { | ||
return timestamp; | ||
public Date getTime() { | ||
return time; | ||
} | ||
|
||
public void setTimestamp(long timestamp) { | ||
this.timestamp = timestamp; | ||
public void setTime(Date time) { | ||
this.time = time; | ||
} | ||
|
||
public String getPlace() { | ||
|
@@ -65,7 +67,7 @@ public String toString() { | |
return "ScannedItem{" + | ||
"id=" + id + | ||
", barcode='" + barcode + '\'' + | ||
", timestamp=" + timestamp + | ||
", time=" + time + | ||
", place='" + place + '\'' + | ||
", volunteer='" + volunteer + '\'' + | ||
'}'; | ||
|