1.配置SQLite表信息参数:
public interface ConstantValue {
interface Staff{
String DBName = "db_staff";
int DBVersion = 1;
String Job = "tb_job";
}
}
2.构建SQLiteOpenHelper:
public class StaffDBHelper extends SQLiteOpenHelper{
public StaffDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table "+ ConstantValue.Staff.Job+"(" +
"_id integer primary key autoincrement," +
"jobId integer," +
"jobName text," +
"jobType text," +
"remark text)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
3.database初始化
StaffDBHelper staffDBHelper = new StaffDBHelper(this, ConstantValue.Staff.DBName, null, ConstantValue.Staff.DBVersion)
database = staffDBHelper.getReadableDatabase()
4.插入数据
for (int i = 0
LoginResult.MdBean.JobsBean jobsBean = jobs.get(i)
ContentValues values = new ContentValues()
values.put("jobId",jobsBean.getJobId())
values.put("jobName",jobsBean.getJobName())
values.put("jobType",jobsBean.getJobType())
values.put("remark",jobsBean.getRemark())
database.insert(ConstantValue.Staff.Job,null,values)
}
5.字段获取
Cursor cursor = database.query(ConstantValue.Staff.Job, null, null, null, null, null, null);
while (cursor.moveToNext()) {
int jobNameIndex = cursor.getColumnIndex("jobName");
String jobName = cursor.getString(jobNameIndex);
}