Ormlite-Lightweight Object Relational mapping (ORM)
If you need to use Ormlite in Android you need to go to the official website http://ormlite.com/download
After downloading the two packages, you will also need to configure the two packages in the corresponding project
Then you can start writing your database statements!!!
Let's take a step-by-step example to build a simple ormlite
What we want to achieve is to use Ormlite to render a list collection
First look at the layout file
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <ListViewAndroid:id= "@+id/list_show_person"Android:layout_weight= "1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > </ListView></LinearLayout>
Now that you're working on the database, you have to create the database
PackageCom.example.learn_ormlite;Importandroid.content.ContentValues;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper; Public classSqlitehelperextendsSqliteopenhelper {Private Final StaticString dbname="Test_person.db"; Private Static int version=1; PublicSqlitehelper (Context context) {Super(Context, DbName,NULL, version); } @Override Public voidonCreate (Sqlitedatabase db) {String SQL= "CREATE table person (ID integer primary key autoincrement,name text)"; Db.execsql (SQL); Contentvalues Con=Newcontentvalues (); Con.put ("Name", "Text1"); Db.insert ("Person",NULL, con); Db.insert ("Person",NULL, con); Db.insert ("Person",NULL, con); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { }}
And then we'll configure the protagonist,
PackageCom.example.learn_ormlite;Importjava.sql.SQLException;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;ImportCom.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;ImportCom.j256.ormlite.dao.Dao;ImportCom.j256.ormlite.support.ConnectionSource; Public classMyormlitehelperextendsOrmlitesqliteopenhelper {Private Final StaticString dbname="Test_person.db"; Private Static int version=1; PrivateDao<person,integer> person_db=NULL; PublicMyormlitehelper (Context context) {Super(Context, DbName,NULL, version); } @Override Public voidonCreate (sqlitedatabase arg0, Connectionsource arg1) {} PublicDao<person,integer>Getperson () {if(person_db==NULL) Try{person_db=getdao (person.class); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } returnperson_db; } @Override Public voidOnupgrade (sqlitedatabase arg0, Connectionsource arg1,intArg2,intArg3) {} @Override Public voidClose () {if(person_db!=NULL) person_db=NULL; Super. Close (); }}
We can see that the above is marked in red, indicating that Ormlite is automatically to find the database, that is, with the database name and version binding database,
But now we're just telling it that we're going to get a thing, but how we got the rules hasn't been set yet, all, and we have to define the rules.
PackageCom.example.learn_ormlite;ImportCom.j256.ormlite.field.DatabaseField;Importcom.j256.ormlite.table.DatabaseTable; @DatabaseTable (TableName= "Person") Public classPerson {@DatabaseField (id=true) PrivateInteger ID; @DatabaseField (ColumnName= "Name") PrivateString name; PublicPerson () {} PublicPerson (String name) { This. name=name; } Public voidSetUid (intuid) { This. ID =uid; } PublicInteger Getuid () {returnID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {returnname; }}
The rule we use is to add some attributes to the fields that need to be mapped, and of course those attributes must correspond to the relationships in the database.
And then we can use the watch.
PackageCom.example.learn_ormlite;Importjava.sql.SQLException;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView;ImportCom.j256.ormlite.dao.Dao; Public classMainactivityextendsActivity {PrivateListView listshow; PrivateList<person> tperson=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Sqlitehelper SQL=NewSqlitehelper ( This); Sqlitedatabase DB=sql.getwritabledatabase (); Db.close (); Listshow=(ListView) Findviewbyid (R.id.list_show_person); Myormlitehelper ml=NewMyormlitehelper ( This); Dao<Person,Integer> db_person=Ml.getperson (); Try{Tperson=Db_person.queryforall (); Listshow.setadapter (NewArrayadapter<person> (mainactivity. This, Android. R.layout.simple_list_item_1,tperson) {}); } Catch(SQLException e) {e.printstacktrace (); } }}
Apps for Android Ormlite