一、最近在求職發現好多公司都有詢問“你有技術博客麼?”這個問題,因此,本人爲了以後發展在此寫下第一篇博客,關於Ormlite的介紹,話不多說。
1. 依賴
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'com.j256.ormlite:ormlite-core:4.48'
2. Bean 等同於 表信息
@DatabaseTable(tableName = "product")
public class Product {
@DatabaseField(generatedId = true)
public int id;
@DatabaseField(columnName = "productId", unique = true)
public int productId;
@DatabaseField(columnName = "name")
public String name;
public Product() {
}
public Product(int id, int productId, String name) {
this.id = id;
this.productId = productId;
this.name = name;
}
//假數據
public Product(int productId){
this.productId = productId;
this.name = "product"+productId;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", produceId=" + productId +
", name='" + name + '\'' +
'}';
}
}
3. 列表內容
public class OrmDBOpenHelper extends OrmLiteSqliteOpenHelper {
private static OrmDBOpenHelper ormDBOpenHelper;
public OrmDBOpenHelper(Context context) {
super(context, "user.db", null, 1);
}
public static synchronized OrmDBOpenHelper getInstance(Context context){
if (ormDBOpenHelper == null){
synchronized (OrmDBOpenHelper.class){
if (ormDBOpenHelper == null){
ormDBOpenHelper = new OrmDBOpenHelper(context);
}
}
}
return ormDBOpenHelper;
}
/**
* 創建表,可創建多個表
* @param database
* @param connectionSource
*/
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, Product.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 更新表,可更新多個表
* @param database
* @param connectionSource
* @param oldVersion
* @param newVersion
*/
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
TableUtils.dropTable(connectionSource, Product.class, true);
} catch (SQLException e) {
e.printStackTrace();
}
}
//獲取操作數據庫的DAO
public Dao<Product, Integer> producDao;
public Dao<Product, Integer> getProductDao() throws SQLException {
if (producDao == null){
producDao = getDao(Product.class);
}
return producDao;
}
/**
* 關閉操作數據庫的DAO
*/
@Override
public void close() {
super.close();
producDao = null;
}
}
4、佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ADD"/>
<Button
android:id="@+id/btn_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Query"/>
<Button
android:id="@+id/btn_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Update"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Delete"/>
</LinearLayout>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
5、主方法
public class MainActivity extends AppCompatActivity {
@Bind(R.id.btn_add)
Button btnAdd;
@Bind(R.id.btn_query)
Button btnQuery;
@Bind(R.id.btn_update)
Button btnUpdate;
@Bind(R.id.btn_delete)
Button btnDelete;
@Bind(R.id.tv_show)
TextView tvShow;
private Dao<Product, Integer> mProductDao;
private List<Product> mProducts;
private OrmDBOpenHelper mOrmDBOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
init();
}
private void init() {
mOrmDBOpenHelper = OrmDBOpenHelper.getInstance(this);
try {
mProductDao = mOrmDBOpenHelper.getProductDao();
} catch (SQLException e) {
e.printStackTrace();
}
}
@OnClick({R.id.btn_add, R.id.btn_query, R.id.btn_update, R.id.btn_delete})
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_add:
add();
break;
case R.id.btn_query:
query();
break;
case R.id.btn_update:
update();
break;
case R.id.btn_delete:
delete();
break;
}
}
private void add() {
for (int i = 0; i < 5; i++) {
try {
mProductDao.create(new Product(100 + i));
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
private void query() {
try {
mProducts = mProductDao.queryForAll();
showResult(mProducts.toString());
} catch (SQLException e) {
e.printStackTrace();
}
}
private void update() {
if (mProducts != null && mProducts.size() > 0) {
Product product = mProducts.get(0);
product.name = "我被修改了 (:";
try {
mProductDao.update(product);
query();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void delete() {
if (mProducts != null && mProducts.size() > 0) {
Product product = mProducts.get(0);
try {
mProductDao.delete(product);
query();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void showResult(String s) {
tvShow.setText(s);
}
@Override
protected void onDestroy() {
mOrmDBOpenHelper.close();
super.onDestroy();
}
}
總結:實現起來其實很簡單,我這裏寫了數據庫的僞代碼。
模擬器結果: