How to Use OrmLite database, ormlite Database
Step 1: import the rack Package 1. Put the two support packages of the orm into the lib directory of your project in the project view (both JAR packages are available online and the latest on GitHub) 2. Add dependency: select your APP in the project structure under the file directory, select the depedence directory, click the plus sign, select the second library, select the lib directory, and click Add dependency. 3. After the JAR package is imported, the two jar files can be clicked, this indicates that the database is successfully added. Step 2: Use ormList to create a database. 1. First, generate an attribute class that stores your various attributes. adding annotations at the beginning of the class indicates that this is a table, the name is cardImg, and each attribute should be annotated, indicating that each column in the form is here, even if the attribute class is perfect, the get and set methods for each attribute are omitted, as well as the construction methods with and without parameters. 2. Then, the database will be created. Here there is no big difference with the basic SQList database creation methods, only the code is attached here for your understanding.
Public class DataBaseHelper extends OrmLiteSqliteOpenHelper {
Private static final String DB_NAME = "biying. db ";
Private static final int DB_VERSON = 1;
Private DataBaseHelper (Context mContext ){
Super (mContext, DB_NAME, null, DB_VERSON );
}
/**
* Basic Singleton mode:
* 1. privatize the constructor first
* 2. provide external static methods
* 3. Determine in the method that no longer creates if it already exists. Create again if it does not exist.
* This ensures that there will always be only one DataBaseHelper object
* 4. To ensure thread security, a thread security keyword synchronized must be provided before the method.
* When one call is made, the other cannot be called.
*/
Private static DataBaseHelper dataBaseHelper;
Public synchronized static DataBaseHelper getInstance (Context mContext ){
If (dataBaseHelper = null ){
DataBaseHelper = new DataBaseHelper (mContext );
}
Return dataBaseHelper;
}
@ Override
Public void onCreate (SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource ){
// Create a table
Try {
// CardImg
TableUtils. createTableIfNotExists (connectionSource, CardImg. class );
} Catch (SQLException e ){
E. printStackTrace ();
}
}
@ Override
Public void onUpgrade (SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int I, int i1 ){
// Delete a table
Try {
TableUtils. dropTable (connectionSource, CardImg. class, true );
} Catch (SQLException e ){
E. printStackTrace ();
}
}
}
3. After creating an attribute class and a database, you need to write a database operation object for this attribute class based on the attribute class. Remember, when using the orm, it is best to have a form corresponding to a database operation object, so that convenient operations will not be confused. Here we will only attach the code for your understanding.
Package com. jereh. biyingapplication. dao;
Import android. content. Context;
Import com. j256.ormlite. dao. Dao;
Import com. j256.ormlite. stmt. DeleteBuilder;
Import com. jereh. biyingapplication. db. DataBaseHelper;
Import com. jereh. biyingapplication. entity. CardImg;
Import com. jereh. biyingapplication. entity. Images;
Import java. SQL. SQLException;
Import java. util. List;
/**
* Created by zhangdi on 2016/8/31.
*/
Public class CardImgDao {
Private Dao <CardImg, Integer> cardImgDao;
Public CardImgDao (Context mContext ){
DataBaseHelper dataBaseHelper = DataBaseHelper. getInstance (mContext );
Try {
CardImgDao = dataBaseHelper. getDao (CardImg. class );
} Catch (SQLException e ){
E. printStackTrace ();
}
}
/**
* Add a piece of data, an object
* @ Param cardImg
* @ Return
*/
Public long addCardImg (CardImg cardImg ){
Int id = 0;
Try {
Id = cardImgDao. create (cardImg );
} Catch (SQLException e ){
E. printStackTrace ();
}
Return id;
}
Public void addAll (List <CardImg> images ){
For (CardImg img: images ){
AddCardImg (img );
}
}
/**
* Query all attributes in a table
* @ Return table set
*/
Public List <CardImg> findAll (){
Try {
Return cardImgDao. queryForAll ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Return null;
}
/**
* Delete a data entry based on the object
* @ Param cardImg
*/
Public void delete (CardImg cardImg ){
DeleteBuilder deleteBuilder = cardImgDao. deleteBuilder ();
Try {
DeleteBuilder. where (). eq ("img", cardImg. getImg ());
DeleteBuilder. delete ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
/**
* Delete all data
* @ Param images
*/
Public void removeAll (List <CardImg> images ){
For (CardImg img: images ){
Delete (img );
}
}
}
4. The Method of Using orm to create a database is basically successful, when calling an object class, you only need to give the database operation object, that is, the dao you wrote, to the new one. Then, you can use the database operation object to add, delete, modify, and query this form in the database.. Here is an introduction to the use of ormlite. I hope it will help you!