换了一个项目后,需要把大量数据保存到本地,所以在选用数据库上有了小小的纠结,比较了SQLite,GreenDao,OrmLite后决定使用一个轻量级的数据库,说实话之前数据库这边用到的并不多,也就是存个登陆密码用户名什么的,用Shareprefrence就足够了。在网上看了一些资料后,决定用OrmLite这个数据库。如果对SQL的语言不熟悉的话,使用OrmLite这个库是很合适的。
使用起来也很方便,下面讲解一下ormlite在android studio中的使用
1)在工程中导入jar包
如图:就是红线标起来的这两个,没有的话,可以在网上搜,本章下面会给出源代码,源码里也有。
2)然后在你的项目的build.gradle里引用这两个jar包
compile files('libs/ormlite-android-4.48.jar')
compile files('libs/ormlite-core-4.48.jar')
package printertestdemo.test.com.textormlite;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
/**
* Created by admin on 2017/4/11.
*/
@DatabaseTable(tableName = "BookEntity")
public class BookEntity {
@DatabaseField(generatedId = true)//generatedId 表示id为主键且自动生成
private int id;
@DatabaseField(columnName = "ProductCodeLevel1")
private String ProductCodeLevel1;
@DatabaseField(columnName = "ProductNameLevel1")
private String ProductNameLevel1;
@DatabaseField(columnName = "ProductCodeLevel2")
private String ProductCodeLevel2;
@DatabaseField(columnName = "ProductNameLevel2")
private String ProductNameLevel2;
@DatabaseField(columnName = "ProductCodeLevel3")
private String ProductCodeLevel3;
@DatabaseField(columnName = "ProductNameLevel3")
private String ProductNameLevel3;
@DatabaseField(columnName = "ProductCodeLevel4")
private String ProductCodeLevel4;
@DatabaseField(columnName = "ProductName")
private String ProductName;
@DatabaseField(columnName = "LeftCount")
private String LeftCount;
@DatabaseField(columnName = "Price")
private String Price;
@DatabaseField(columnName = "Agio")
private String Agio;
@DatabaseField(columnName = "Score")
private String Score;
@DatabaseField(columnName = "Intrudoction")
private String Intrudoction;
public BookEntity() {
}
public BookEntity( String productCodeLevel1, String productNameLevel1, String productCodeLevel2, String productNameLevel2, String productCodeLevel3, String productNameLevel3, String productCodeLevel4, String productName, String leftCount, String price, String agio, String score, String intrudoction) {
ProductCodeLevel1 = productCodeLevel1;
ProductNameLevel1 = productNameLevel1;
ProductCodeLevel2 = productCodeLevel2;
ProductNameLevel2 = productNameLevel2;
ProductCodeLevel3 = productCodeLevel3;
ProductNameLevel3 = productNameLevel3;
ProductCodeLevel4 = productCodeLevel4;
ProductName = productName;
LeftCount = leftCount;
Price = price;
Agio = agio;
Score = score;
Intrudoction = intrudoction;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProductCodeLevel1() {
return ProductCodeLevel1;
}
public void setProductCodeLevel1(String productCodeLevel1) {
ProductCodeLevel1 = productCodeLevel1;
}
public String getProductNameLevel1() {
return ProductNameLevel1;
}
public void setProductNameLevel1(String productNameLevel1) {
ProductNameLevel1 = productNameLevel1;
}
public String getProductCodeLevel2() {
return ProductCodeLevel2;
}
public void setProductCodeLevel2(String productCodeLevel2) {
ProductCodeLevel2 = productCodeLevel2;
}
public String getProductNameLevel2() {
return ProductNameLevel2;
}
public void setProductNameLevel2(String productNameLevel2) {
ProductNameLevel2 = productNameLevel2;
}
public String getProductCodeLevel3() {
return ProductCodeLevel3;
}
public void setProductCodeLevel3(String productCodeLevel3) {
ProductCodeLevel3 = productCodeLevel3;
}
public String getProductNameLevel3() {
return ProductNameLevel3;
}
public void setProductNameLevel3(String productNameLevel3) {
ProductNameLevel3 = productNameLevel3;
}
public String getProductCodeLevel4() {
return ProductCodeLevel4;
}
public void setProductCodeLevel4(String productCodeLevel4) {
ProductCodeLevel4 = productCodeLevel4;
}
public String getProductName() {
return ProductName;
}
public void setProductName(String productName) {
ProductName = productName;
}
public String getLeftCount() {
return LeftCount;
}
public void setLeftCount(String leftCount) {
LeftCount = leftCount;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
public String getAgio() {
return Agio;
}
public void setAgio(String agio) {
Agio = agio;
}
public String getScore() {
return Score;
}
public void setScore(String score) {
Score = score;
}
public String getIntrudoction() {
return Intrudoction;
}
public void setIntrudoction(String intrudoction) {
Intrudoction = intrudoction;
}
}
4)建库
package printertestdemo.test.com.textormlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
/**
* Created by admin on 2017/4/11.
*/
public class DBhelper extends OrmLiteSqliteOpenHelper {
private static final String TABLE_NAME = "Book.db";
private static DBhelper instance;
public DBhelper(Context context) {
super(context, TABLE_NAME, null, 1);
// TODO Auto-generated constructor stub
}
/**
* 单例获取该Helper
*
* @param context
* @return
*/
public static synchronized DBhelper getHelper(Context context) {
if (instance == null) {
synchronized (DBhelper.class) {
if (instance == null)
instance = new DBhelper(context);
}
}
return instance;
}
@Override
public void onCreate(SQLiteDatabase database,
ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, BookEntity.class);//书的实体类
Log.d("DBhelper", "创建表成功");
} catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase database,
ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
TableUtils.createTable(connectionSource, BookEntity.class);
} catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
onCreate(database, connectionSource);
}
}
package printertestdemo.test.com.textormlite;
import android.content.Context;
import android.util.Log;
import com.j256.ormlite.dao.Dao;
import java.sql.SQLException;
import java.util.List;
/**
* Created by admin on 2017/4/11.
* 书的操作表
*/
public class BookDao {
Context con;
// private DBShophelps helper;
private DBhelper helper;
private Dao personDao;
// 构造函数
public BookDao(Context con) {
helper = DBhelper.getHelper(con);
this.con = con;
}
// 每个表一般我们都会单独写个Dao用于操作
public Dao getPersonDao() throws java.sql.SQLException {
if (personDao == null) {
personDao = helper.getDao(BookEntity.class);
}
return personDao;
}
// 根据ID查询
public BookEntity selectPerson(int i) {
try {
BookEntity p = getPersonDao().queryForId(i);
return p;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// 添加某条数据---添加数据
public void addPerson(BookEntity p) {
try {
getPersonDao().create(p);
} catch (SQLException e) {
}
}
// 删除某条数据
public void deletePerson(BookEntity p) {
try {
Log.d("TAG", "删除ID为" + p.getId() );
getPersonDao().deleteById(p.getId());
} catch (SQLException e) {
}
}
//删除所有数据
public void deleteAll(List listdata){
try {
getPersonDao().delete(listdata);
} catch (SQLException e) {
e.printStackTrace();
}
}
// // 更新某条数据
// public void updatePerson(BookEntity p, String name) {
// try {
//
// p.setName(name);
// getPersonDao().update(p);
// Log.d("TAG", "修改数据后姓名为:" + p.getName());
// } catch (SQLException e) {
// }
// }
// 查询所有数据
public List showPersonAll() {
try {
List list = getPersonDao().queryForAll();
Log.d("2TAG", "查询所有数据条数:" + list.size());
// for (int i = 0; i < list.size(); i++) {
// Log.d("2TAG", "...." + list.get(i).getName());
// }
return list;
} catch (SQLException e) {
}
return null;
}
public List SelectStatus(String ss,String type_id ){
List entity=null;
try {
entity= getPersonDao().queryForEq(ss,type_id);//字段,字段的值
return entity;
} catch (SQLException e) {
e.printStackTrace();
}
return entity;
}
}
package printertestdemo.test.com.textormlite;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Listlist=new ArrayList<>();
private Button but1,but2;
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化数据
initData();
//数据插入成功,可以进行查询了
initView();//初始化控件
}
private void initView() {
but1= (Button) findViewById(R.id.button);
but2= (Button) findViewById(R.id.button1);
listview= (ListView) findViewById(R.id.listview);
final ListViewAdapter adapter=new ListViewAdapter(this);
listview.setAdapter(adapter);
final BookDao dao=new BookDao(this);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//查询所有数据
List list1=dao.showPersonAll();
if(list1.size()!=0){
adapter.clearDeviceList();
adapter.setDeviceList(list1);
}
}
});
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//查询ProductNameLevel2为小说的
List list1=dao.SelectStatus("ProductNameLevel2","小说");
if(list1.size()!=0){
adapter.clearDeviceList();
adapter.setDeviceList(list1);
}
}
});
}
private void initData() {
//这里我就存入五个
BookEntity book1=new BookEntity("1","书籍","1","小说","1","言情","11111111","我爱的人","10","100","0.8","5","XXXYYYY");
list.add(book1);
BookEntity book2=new BookEntity("1","书籍","1","小说","2","悬疑","1121122","破案专辑","10","200","0.9","5","XXXYYYY");
list.add(book2);
BookEntity book3=new BookEntity("1","书籍","1","小说","3","历史","1131133","近代历史","10","150","0.8","5","XXXYYYY");
list.add(book3);
BookEntity book4=new BookEntity("1","书籍","2","工具书","1","字典","1211214","新华字典","10","10","0.9","5","XXXYYYY");
list.add(book4);
BookEntity book5=new BookEntity("1","书籍","2","工具书","2","生活百科","1211225","百科全书","10","80","0.7","5","XXXYYYY");
list.add(book5);
//向Book.db里插入数据
BookDao dao=new BookDao(this);
for(BookEntity entity:list){
dao.addPerson(entity);
}
}
}
下面给出该项目的源码下载地址,有需要的可以下载一下看看,有什么不足,请及时@我,大家一起进步
http://download.csdn.net/detail/shihuiyun/9813965