前言
对于Android SQLite数据库而言,自身拥有一些操作方法,只是在做项目的过程中不适应我们APP研发的效率问题,程序与也不能光是为代码而代码。代码不是越写越多好而是越写月少、越写越效率好。对于Android SQLite数据库而言,自身拥有一些操作方法,只是在做项目的过程中不适应我们APP研发的效率问题,程序与也不能光是为代码而代码。代码不是越写越多好而是越写月少、越写越效率好。通常我们项目中数据库无外乎是对象的操作、数据表的操作、数据库的升级、表的曾删改查。在网络上无外乎也有了很多的数据库工具,但是回想一下你用到的功能又有几个呢,很多都是Java注解来写的,本人不提倡什么都用注解,注解本省会消耗已经的性能和时间,虽然时间可以忽略,但是处女座的我就是很看不下去,所以我自己开始来写数据库方便的工具。
使用
-
jar配置方法:
jar文件下载后,AndroidStudio中复制到app/libs/, jniLibs文件夹直接复制到app/src/main/,values中文件 -
arr配置方法: arr文件复制到app/libs/,在app/build.gradle 中android里面配置
repositories {
flatDir {
dirs 'libs'
}
}
dependencies里面配置:
dependencies {
compile(name: 'SQLiteHelper', ext: 'aar')//这里你arr是什么名字就写什么名字
}
- 创建数据库
import android.content.Context;
/**
1. Created by Relin
2. on 2018-09-19.<br/>
3. 继承SQLiteHelper重写onCreate和onUpgrade两个方法
4. 在onCreate方法里创建自己的表
5. 在onUpgrade方法里面更新自己的表<br/>
6. inherit the SQLiteHelper to rewrite the onCreate and onUpgrade methods
7. create your own table in the onCreate method
8. update your table in the onUpgrade method
*/
public class ISQLite extends SQLiteHelper {
public ISQLite(Context context) {
super(context);
}
public ISQLite(Context context, String databaseName, int databaseVersion) {
super(context, databaseName, databaseVersion);
}
@Override
public void onCreate(SQLiteDatabase db) {
super.onCreate(db);
//create your tables
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
//upgrade your tables
}
}
- 创建表(ISQLite 类)
@Override
public void onCreate(SQLiteDatabase db) {
super.onCreate(db);
//(1)更具表名、字段创建
createTable(db,"TableName",new String[]{"id","name"});
//(2)根据对象创建
User user = new User();
user.setId("1");
user.setName("name");
createTable(db,user);
}
- 升级数据库
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
if (newVersion > oldVersion) {
db.execSQL("ALTER TABLE " + "\"TableName\"" + "RENAME TO " + "\"RE_TABLE_NAME\"" + ";");
}
}
- 查询数据
/**
* 查询数据
* query from databases and find you what you are gong to finding.
*
* @param sql 数据库语句
* @return
*/
public List<Map<String, String>> query(String sql)
/**
* 查询数据
*
* @param cls 实体类
* @param sql sql语句
* @param <T> 实体类,例如:User.class
* @return 实体列表
*/
public <T> List<T> query(Class<T> cls, String sql)
- 插入数据
/**
* 插入数据
* insert you want what information.
*
* @param sql sql语句
* @return
*/
public void insert(String sql)
/**
* 插入数据
* insert you want what information.
*
* @param table
* @param contentValues
* @return
*/
public long insert(String table, ContentValues contentValues)
/**
* 插入对象数据
*
* @param obj
* @return 插入数据
*/
public long insert(Object obj)
- 修改操作
/**
* 更新数据
* update from your table in database.
*
* @param table
* @param contentValues you is gonging to update values
* @param whereClause for example "name = ?"
* @param whereArgs for example new String[]{"Marry"}
* @return
*/
public long update(String table, ContentValues contentValues, String whereClause, String[] whereArgs)
/**
* 更新数据
* update from your table in database.
*
* @param obj data object
* @param whereClause for example "name = ?"
* @param whereArgs for example new String[]{"Marry"}
* @return
*/
public long update(Object obj, String whereClause, String[] whereArgs)
/**
* 更新数据
* update from your table in database.
* for example update user set user_name = 'Jerry' where user_name = 'Marry'
*
* @param sql
* @return
*/
public void update(String sql)
- 删除操作
/**
* 删除表中的数据
*
* @param table
*/
public void deleteTable(Class table)
/**
* 删除表
* drop table
*
* @param table 数据表
*/
public void dropTable(String table)
/**
* 清除表中数据
* truncate table
*
* @param table 数据表
*/
public void deleteTable(String table)
/**
* 删除数据库
* delete database
*/
public void dropDatabase()