一、导入jar包
导入ormlite-android-4.48.jar与ormlite-core-4.48.jar到libs
二、定义数据库
定义数据库文件存放位置,数据库版本号
public class DBHelper extends OrmLiteSqliteOpenHelper {
//定义数据库存放位置,便于以后查看
public static final String DATABASE_PATH = Environment.getExternalStorageDirectory() + "/Android/data/clothes.db";
//定义数据库的版本号,当数据库需要升级时进行更改
public static final int DATABASE_VERSION = 1;
//创建DetailDataOpenHelper实例
private static DBHelper instance;
public static DBHelper getInstance(Context mContext) {
if (instance == null) {
synchronized (DBHelper.class) {
if (instance == null) {
instance = new DBHelper(mContext);
}
}
}
return instance;
}
public DBHelper(Context context) {
super(context, DATABASE_PATH, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, DropDownInfo.class);
Log.d("/","DropDownInfo建表成功");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
TableUtils.createTable(connectionSource, DropDownInfo.class);
TableUtils.dropTable(connectionSource, DropDownInfo.class, false);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
建表,定义表格字段信息
@DatabaseTable(tableName = "t_product")
public class DropDownInfo implements Serializable {
@DatabaseField(columnName = "id", generatedId = true)
public int id;
@DatabaseField(columnName = "company")
private String company;
@DatabaseField(columnName = "inventoryType")
private String inventoryType;
}
public class DropDownDao {
private Dao<DropDownInfo, Integer> dropDownInfoDao;
public Dao<DropDownInfo, Integer> getDropDownInfoDao(Context mContext) {
if (dropDownInfoDao == null) {
dropDownInfoDao = createDropDownDao(mContext);
}
return dropDownInfoDao;
}
private Dao createDropDownDao(Context mContext) {
try {
return DBHelper.getInstance(mContext).getDao(DropDownInfo.class);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
建库,实现new
public class BaseOperationTask {
public DBHelper detailDataOpenHelper;
public AndroidDatabaseConnection androidDatabaseConnection;
private static BaseOperationTask instance;
public BaseOperationTask(Context mContext) {
if (detailDataOpenHelper == null) {
detailDataOpenHelper = DBHelper.getInstance(mContext);
androidDatabaseConnection = new AndroidDatabaseConnection(detailDataOpenHelper.getWritableDatabase(), true);
}
}
public static BaseOperationTask getInstance(Context mContext) {
if (instance == null) {
synchronized (BaseOperationTask.class) {
if (instance == null) {
instance = new BaseOperationTask(mContext);
}
}
}
return instance;
}
}
建立功能抽象类,我写了增删改查的功能
public interface IDropDownOperation {
//增加
boolean insertDropDownData(DropDownInfo dropDownInfo);
//更新
boolean updateDropDownData(DropDownInfo dropDownInfo);
//查询
ArrayList<DropDownInfo> queryDropDownData();
//删除
boolean deleteAll();
}
实现上面的抽象类
public class DropDownInfoOperationTask extends BaseOperationTask implements IDropDownOperation {
private Dao<DropDownInfo, Integer> dropDownInfoDao;
private ArrayList<DropDownInfo> mAllDropDownData;
public DropDownInfoOperationTask(Context mContext) {
super(mContext);
dropDownInfoDao = new DropDownDao().getDropDownInfoDao(mContext);
}
@Override
public boolean insertDropDownData(DropDownInfo dropDownInfo) {
Savepoint savepoint = null;
try {
savepoint = androidDatabaseConnection.setSavePoint("start");
androidDatabaseConnection.setAutoCommit(false);
int startResult = dropDownInfoDao.create(dropDownInfo);
androidDatabaseConnection.commit(savepoint);
if (startResult != -1) {
return true;
}
return false;
} catch (SQLException e) {
e.printStackTrace();
try {
androidDatabaseConnection.rollback(savepoint);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
return false;
}
@Override
public boolean updateDropDownData(DropDownInfo dropDownInfo) {
Savepoint savepoint = null;
try {
savepoint = androidDatabaseConnection.setSavePoint("start");
androidDatabaseConnection.setAutoCommit(false);
int startResult = dropDownInfoDao.update(dropDownInfo);
androidDatabaseConnection.commit(savepoint);
if (startResult != -1) {
return true;
}
return false;
} catch (SQLException e) {
e.printStackTrace();
try {
androidDatabaseConnection.rollback(savepoint);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
return false;
}
@Override
public ArrayList<DropDownInfo> queryDropDownData() {
mAllDropDownData = new ArrayList<>();
Savepoint savepoint = null;
try {
savepoint = androidDatabaseConnection.setSavePoint("start");
androidDatabaseConnection.setAutoCommit(false);
mAllDropDownData = (ArrayList<DropDownInfo>) dropDownInfoDao.queryBuilder().orderBy("id", false).query();
androidDatabaseConnection.commit(savepoint);
return mAllDropDownData;
} catch (SQLException e) {
e.printStackTrace();
try {
androidDatabaseConnection.rollback(savepoint);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
return null;
}
@Override
public boolean deleteAll() {
Savepoint savepoint = null;
try {
savepoint = androidDatabaseConnection.setSavePoint("start");
androidDatabaseConnection.setAutoCommit(false);
mAllDropDownData = (ArrayList<DropDownInfo>) dropDownInfoDao.queryBuilder().query();
//int delete = dropDownInfoDao.delete(mAllDropDownData);
//循环删除,避免一次删除过多的数据,导致失败
int delete = 0;
for (DropDownInfo info:mAllDropDownData ){
delete = dropDownInfoDao.delete(info);
}
androidDatabaseConnection.commit(savepoint);
if (delete != -1) {
return true;
}
return false;
} catch (SQLException e) {
e.printStackTrace();
try {
androidDatabaseConnection.rollback(savepoint);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
return false;
}
}
三、数据库的使用
数据库定义好了,接着就是使用了。
使用方法比较简单,先实例化,然后调用对应的函数即可
DropDownInfoOperationTask dropDownInfoOperation = new DropDownInfoOperationTask(this);
//查询
ArrayList<DropDownInfo> dropDownInfos = dropDownInfoOperation .queryDropDownData();
//删除
dropDownInfoOperation.deleteAll()
本文只写了最基础的增删改查的功能,如需要按条件查询,自行添加。
基本上到这里就结束了