SQLite 数据库访问架构 Android
SQLite Database Access Architecture Android
希望一切都好。我正在构建一个数据库大小不断增加的应用程序。 Increase
根据 Data
和 Tables and associated columns
。我已经制作了一个动态结构来将 Models
发送到 DatabaseHelper
,但是在 DatabaseHelper
中我必须放置像 instanceOf
这样的代码来制作适当的 ContentValues
。这让我很生气。 Dynamic
现在进入了某种 if else if
的状态,这当然是我不想要的。请参阅以下代码片段以获得更好的想法。
* 插入人物的通用函数,可以是老师或学生 *
public void insertPersons(ArrayList<Person> persons) {
for(Person person : persons) {
insertOrUpdatePerson(person);
}
}
* 把 instanceof. Dynamics现在在哪里??? *
public void insertOrUpdatePerson(Person person) {
if(person instanceof Teacher)
insertOrUpdateTeacher(person);
else {
ClassStudent student = (ClassStudent) person;
insertOrUpdateStudent(student);
}
}
public void insertOrUpdateStudent(ClassStudent student) {
try {
if(student.getPk_id() > 0) {
updateRecord(getWritableDatabase(), TABLE_STUDENT_DATA, createContentValues(student), student);
} else {
int row_id = insertRecord(getWritableDatabase(), TABLE_STUDENT_DATA, createContentValues(student), student);
student.setPk_id(row_id);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void insertOrUpdateTeacher(Person person) {
try {
Teacher teacher = (Teacher) person;
Cursor cursor = getWritableDatabase().rawQuery("SELECT * FROM " + TABLE_TEACHERS + " WHERE " +
teacher_id + " = " + teacher.getPerson_id(), null);
if(cursor != null && cursor.moveToFirst()) {
teacher.setPk_id(cursor.getInt(cursor.getColumnIndexOrThrow(pk_id)));
}
if(teacher.getPk_id() > 0) {
updateRecord(getWritableDatabase(), TABLE_TEACHERS, createContentValues(teacher), teacher);
} else {
int row_id = insertRecord(getWritableDatabase(), TABLE_TEACHERS, createContentValues(teacher), teacher);
teacher.setPk_id(row_id);
}
} catch (Exception e) {
e.printStackTrace();
}
}
* 多个表和多个内容值,代码让我感觉很糟糕 *
private ContentValues createContentValues(ClassStudent student) {
ContentValues cv = new ContentValues();
cv.put(school_id, student.getSchool_id());
cv.put(student_id, student.getPerson_id());
return cv;
}
private ContentValues createContentValues(Teacher person) {
ContentValues cv = new ContentValues();
cv.put(teacher_id, person.getPerson_id());
cv.put(name, person.getPerson_name());
return cv;
}
What could be the other way to a situation like this? Should Model be
responsible for making contentValues? OR what other architecture
should i follow? I don't want to use ORMs
对于android中的SQLite DB结构,我一般使用BoD Content provider generator。它非常易于使用,您只需传递 table 列的 json 值,它就会自动为您生成内容提供程序 class。对于每个新的 table,您都可以拥有单独的内容提供程序,这样可以提高代码效率。
此外,您可以只使用命令行命令生成结构,而无需在 gradle 文件中导入任何内容。请仔细阅读 link,其中包含您需要的所有语法信息。谢谢
我建议你必须使用接口来解决这种情况。
使用 insert() 和 getContentValues() 创建接口并在 Base 类 上实现它们。
示例:
public interface PersonInterface {
void insert(ContentValues values);
void getContentValues();
}
public class Person implements PersonInterface {
void insert(ContentValues values) {}
void getContentValues() {}
}
public class Teacher extends Person {
void insert(ContentValues values) {
// your Teacher record insert code goes here
}
void getContentValues() {
// return Teacher content values
}
}
public class Student extends Person {
void insert(ContentValues values) {
// your Student record insert code goes here
}
void getContentValues() {
// return Student content values
}
}
public class DBHelper {
public void insertPersons(ArrayList<Person> persons) {
for(Person person : persons) {
insertOrUpdatePerson(person);
}
public void insertOrUpdatePerson(Person person) {
person.insert(person.getContentValues());
}
}
希望一切都好。我正在构建一个数据库大小不断增加的应用程序。 Increase
根据 Data
和 Tables and associated columns
。我已经制作了一个动态结构来将 Models
发送到 DatabaseHelper
,但是在 DatabaseHelper
中我必须放置像 instanceOf
这样的代码来制作适当的 ContentValues
。这让我很生气。 Dynamic
现在进入了某种 if else if
的状态,这当然是我不想要的。请参阅以下代码片段以获得更好的想法。
* 插入人物的通用函数,可以是老师或学生 *
public void insertPersons(ArrayList<Person> persons) {
for(Person person : persons) {
insertOrUpdatePerson(person);
}
}
* 把 instanceof. Dynamics现在在哪里??? *
public void insertOrUpdatePerson(Person person) {
if(person instanceof Teacher)
insertOrUpdateTeacher(person);
else {
ClassStudent student = (ClassStudent) person;
insertOrUpdateStudent(student);
}
}
public void insertOrUpdateStudent(ClassStudent student) {
try {
if(student.getPk_id() > 0) {
updateRecord(getWritableDatabase(), TABLE_STUDENT_DATA, createContentValues(student), student);
} else {
int row_id = insertRecord(getWritableDatabase(), TABLE_STUDENT_DATA, createContentValues(student), student);
student.setPk_id(row_id);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void insertOrUpdateTeacher(Person person) {
try {
Teacher teacher = (Teacher) person;
Cursor cursor = getWritableDatabase().rawQuery("SELECT * FROM " + TABLE_TEACHERS + " WHERE " +
teacher_id + " = " + teacher.getPerson_id(), null);
if(cursor != null && cursor.moveToFirst()) {
teacher.setPk_id(cursor.getInt(cursor.getColumnIndexOrThrow(pk_id)));
}
if(teacher.getPk_id() > 0) {
updateRecord(getWritableDatabase(), TABLE_TEACHERS, createContentValues(teacher), teacher);
} else {
int row_id = insertRecord(getWritableDatabase(), TABLE_TEACHERS, createContentValues(teacher), teacher);
teacher.setPk_id(row_id);
}
} catch (Exception e) {
e.printStackTrace();
}
}
* 多个表和多个内容值,代码让我感觉很糟糕 *
private ContentValues createContentValues(ClassStudent student) {
ContentValues cv = new ContentValues();
cv.put(school_id, student.getSchool_id());
cv.put(student_id, student.getPerson_id());
return cv;
}
private ContentValues createContentValues(Teacher person) {
ContentValues cv = new ContentValues();
cv.put(teacher_id, person.getPerson_id());
cv.put(name, person.getPerson_name());
return cv;
}
What could be the other way to a situation like this? Should Model be responsible for making contentValues? OR what other architecture should i follow? I don't want to use ORMs
对于android中的SQLite DB结构,我一般使用BoD Content provider generator。它非常易于使用,您只需传递 table 列的 json 值,它就会自动为您生成内容提供程序 class。对于每个新的 table,您都可以拥有单独的内容提供程序,这样可以提高代码效率。
此外,您可以只使用命令行命令生成结构,而无需在 gradle 文件中导入任何内容。请仔细阅读 link,其中包含您需要的所有语法信息。谢谢
我建议你必须使用接口来解决这种情况。
使用 insert() 和 getContentValues() 创建接口并在 Base 类 上实现它们。
示例:
public interface PersonInterface {
void insert(ContentValues values);
void getContentValues();
}
public class Person implements PersonInterface {
void insert(ContentValues values) {}
void getContentValues() {}
}
public class Teacher extends Person {
void insert(ContentValues values) {
// your Teacher record insert code goes here
}
void getContentValues() {
// return Teacher content values
}
}
public class Student extends Person {
void insert(ContentValues values) {
// your Student record insert code goes here
}
void getContentValues() {
// return Student content values
}
}
public class DBHelper {
public void insertPersons(ArrayList<Person> persons) {
for(Person person : persons) {
insertOrUpdatePerson(person);
}
public void insertOrUpdatePerson(Person person) {
person.insert(person.getContentValues());
}
}