Android:如何编写我们自己的类而不是使用sugar ORM
我有五个类,每个类都有不同的字段名。我正在通过扩展SugarRecord来存储和检索所有值。但是我想扩展我的课程而不是SugarRecord。我不知道如何为所有模态类编写公共类。我需要一些从哪里开始编写代码的指导。在这里,我发布了试图插入和检索的模态类。请帮帮我。提前谢谢 作业班Android:如何编写我们自己的类而不是使用sugar ORM,android,Android,我有五个类,每个类都有不同的字段名。我正在通过扩展SugarRecord来存储和检索所有值。但是我想扩展我的课程而不是SugarRecord。我不知道如何为所有模态类编写公共类。我需要一些从哪里开始编写代码的指导。在这里,我发布了试图插入和检索的模态类。请帮帮我。提前谢谢 作业班 public class assignments extends SugarRecord<assignments> { public String valid; public String pid; pub
public class assignments extends SugarRecord<assignments> {
public String valid;
public String pid;
public String event_added;
public String eid;
public String nature_of_contact;
public String assignment_date;
public String status;
public String aid;
public assignments(Context arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
}
public class Requirement extends SugarRecord<Requirement> {
public String rid;
public String pid;
public String wid;
public String name;
public Requirement(Context arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.name;
}
}
在DBHelper类中,我插入DayExpensesDB类的所有值。
这是我的DBHelper类
public class DBHelper {
SQLiteDatabase db;
DayExpensesDB dedb;
public DBHelper(Context context)
{
dedb = new DayExpensesDB(context);
}
public SQLiteDatabase open()
{
return dedb.getWritableDatabase();
}
public void close()
{
dedb.close();
}
public void insertValues(double enter, double food, double medicine, double travel, double total, String date)
{
db = this.open();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
date = dateFormat.format(new Date());
ContentValues values = new ContentValues();
values.put(DayExpensesDB.ENTERTAINMENT, enter);
values.put(DayExpensesDB.FOOD, food);
values.put(DayExpensesDB.MEDICINE, medicine);
values.put(DayExpensesDB.TRAVEL, travel);
values.put(DayExpensesDB.TOTAL, total);
values.put(DayExpensesDB.DATEVALUE, date);
db.insert(DayExpensesDB.TABLE_NAME, null, values);
this.close();
}
public ArrayList<String> getValues()
{
ArrayList<String> names = new ArrayList<String>();
db = this.open();
String query = "select * from "+DayExpensesDB.TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
if(cursor.moveToFirst())
{
do{
names.add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.ENTERTAINMENT)));
names.add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.FOOD)));
names.add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.MEDICINE)));
names.add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.TRAVEL)));
names.add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.DATEVALUE)));;
names.add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.TOTAL)));;
names.add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.TOTAL)));;
//names.add(cursor.get)
}while(cursor.moveToNext());
}
return names;
}
公共类DBHelper{
sqlitedb数据库;
日费用d b d b;
公共DBHelper(上下文)
{
dedb=新的日支出SDB(上下文);
}
公共SQLiteDatabase open()
{
返回dedb.getWritableDatabase();
}
公众假期结束()
{
dedb.close();
}
public void insertValues(双输入、双食物、双药物、双旅行、双总计、字符串日期)
{
db=this.open();
SimpleDataFormat dateFormat=新SimpleDataFormat(“yyyy-MM-dd”);
date=dateFormat.format(新日期());
ContentValues=新的ContentValues();
value.put(DayExpensesDB.ENTERTAINMENT,输入);
价值。投入(日支出b.食品、食品);
价值。投入(日支出b.医药、医药);
价值。投入(日支出b.差旅、差旅);
价值。投入(日支出总额、总额);
value.put(daycexpensesdb.DATEVALUE,date);
db.insert(daycexpensesdb.TABLE_NAME,null,value);
这个。关闭();
}
公共ArrayList getValues()
{
ArrayList name=新的ArrayList();
db=this.open();
String query=“select*from”+daycexpensesdb.TABLE\u名称;
Cursor Cursor=db.rawQuery(查询,空);
if(cursor.moveToFirst())
{
做{
add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.ENTERTAINMENT));
add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.FOOD));
add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.MEDICINE));
add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.TRAVEL));
add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.DATEVALUE));;
add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.TOTAL));;
add(cursor.getString(cursor.getColumnIndex(DayExpensesDB.TOTAL));;
//name.add(cursor.get)
}while(cursor.moveToNext());
}
返回姓名;
}
因此,如果我遵循上述编码,那么我需要为每个模式类(如分配类和需求类)编写单独的DBHelper类。但我想编写一个通用类,该类应该执行插入、保存和检索操作。不确定我是否理解,但下面是我要做的。
我会创建一个非常好的结构
首先,我首先会从你的类DayExpensesDB中删除你放在那里的所有东西,成员。相反,我会用脚本创建我的数据库,所有东西都是OOP风格的。 例如,您为包含有关列的内容的列创建了一个类,它是主键,是或不是?它是唯一的还是不唯一的?
编辑
添加了从pastebin中删除的代码 //类,该类描述数据库列。
public class DatabaseColumnImpl implements DatabaseColumn {
private boolean uniqueField = false;
private boolean autoIncrement = false;
private boolean isForeignKey = false;
private boolean primaryKey = false;
private String rowName;
private RowType dataRowType;
public GenericColumnImpl(boolean primaryKey, boolean autoIncrement, boolean uniqueField, boolean isForeignKey, String fieldName, @NonNull RowType type) {
this.uniqueField = uniqueField;
this.primaryKey = primaryKey;
this.autoIncrement = autoIncrement;
this.dataRowType = type;
this.rowName = fieldName;
this.isForeignKey = isForeignKey;
}
public DatabaseColumnImpl(boolean primaryKey, boolean autoIncrement, String fieldName, @NonNull RowType type) {
this(primaryKey, autoIncrement, false, false, fieldName, type);
}
//... setters and getters
}
接下来,我将定义我的数据库契约和要插入的行。例如,您的表要求有4-5列,但尚未知道行(请参见下图)。//每行的数据库列。
public enum DatabaseTable{
DATA(new DatabaseColumnImpl(true, true, DataContract.X._ID, SqliteDataType.Integer),
new DatabaseColumnImpl(DataContract.X.TIMEDURATION, SqliteDataType.Integer),
new DatabaseColumnImpl(DataContract.X.DESCRIPTION, SqliteDataType.String),
new DatabaseColumnImpl(DataContract.X.NAME, SqliteDataType.String));
//much more data can be added here
private DatabaseColumnImpl[] genericColumns;
private DatabaseTable(DatabaseColumnImpl... columns) {
this.genericColumns = columns;
}
public DatabaseColumnImpl[] getColumns() {
return genericColumns;
}
public String getName() {
return this.name();
}
}
完成这些之后,我将创建数据库契约类
public class DatabaseContract {
public static final String CONTENT_STRING = "content://";
public static final String CONTENT_AUTHORITY = "com.org.data";
public static final String VND_AUTHORITY = "vnd.org.data";
public static final Uri BASE_CONTENT_URI = Uri.parse(CONTENT_STRING + CONTENT_AUTHORITY);
public static final class XData implements BaseColumns {
public static final String TABLE_NAME = "xdata";
//content://com.org.data/xdata
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(TABLE_NAME).build();
//vnd.android.cursor.dir/vnd.org.data
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + File.separator + VND_AUTHORITY;
//vnd.android.cursor.item/vnd.org.data
public static final String CONTENT_ITEMTYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + File.separator + VND_AUTHORITY;
public static final String NAME = "name";
public static final String DESCRIPTION = "description";
public static final String TIMEDURATION = "time_duration";
//more data
public static Uri buildXdataUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
}
}
设置好前一个结构后,查询如下所示:
public class DatabaseOpenHelper extends SQLiteOpenHelper {
private static final String DROP_START_QUERY = "DROP TABLE IF EXISTS ";
/**
* database name
*/
public static final String DATABASE_NAME = "data.db";
// default statements used for different queries
public static final String PRIMARY_KEY_STATEMENT = " PRIMARY KEY ";
public static final String PRIMARY_KEY_AUTOINCREMENT_STATEMENT = " PRIMARY KEY AUTOINCREMENT ";
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ";
private static final String LEFT_PARENTHESES = "( \n";
private static final String SPACE_CHARACTER = " ";
private static final String CONFLICT_REPLACE_TEXT = ") ON CONFLICT IGNORE ";
public static final String UNIQUE_KEY_STATEMENT = " UNIQUE (";
// end of default queries
private static final int DATABASE_VERSION = 19;
public static final String COMMA_STRING = ",";
public static final String EMPTY_STRING = "";
public static final String TAB_STRING = "\n";
public DatabaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
createTables(db);
}
private void createTables(SQLiteDatabase db) {
Collection<String> scripts = createScripts();
for (String query : scripts) {
db.execSQL(query);
}
}
private Collection<String> createScripts() {
Collection<String> scripts = new ArrayList<>();
for (DatabaseTable table : DatabaseTable.values()) {
String sqlQuery = CREATE_TABLE + table.getName() + LEFT_PARENTHESES;
String primaryKeyQuery = getQueryScript(table);
sqlQuery += primaryKeyQuery;
sqlQuery += ")\n";
scripts.add(sqlQuery);
}
return scripts;
}
private String getQueryScript(DatabaseTable table) {
String query = "";
for (int k = 0; k < table.getColumns().length; k++) {
DatabaseColumn column = table.getColumns()[k];
query += column.getFieldName() + SPACE_CHARACTER + column.getDataType().getRowType();
if (column.isPrimaryKey() && column.isAutoIncremented()) {
query += PRIMARY_KEY_AUTOINCREMENT_STATEMENT;
} else if (column.isPrimaryKey()) {
query += PRIMARY_KEY_STATEMENT;
} else if (column.isUnique()) {
query += UNIQUE_KEY_STATEMENT + column.getFieldName() + CONFLICT_REPLACE_TEXT;
}
query += (k == table.getColumns().length - 1 ? EMPTY_STRING : COMMA_STRING) + TAB_STRING;
}
return query;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//drop tables
List<String> dropScripts = createDropScripts();
for (String query : dropScripts) {
db.execSQL(query);
}
}
/**
* creates a list of all the drop table queries and update them
* to a list so we can delete them in a simple way.
*
* @return array list containing the drop table queries.
*/
private List<String> createDropScripts() {
List<String> dropScripts = new ArrayList<>();
DatabaseTable[] tables = DatabaseTable.values();
for (DatabaseTable table : tables) {
String query_script = DROP_START_QUERY + table.getName() + " ;";
dropScripts.add(query_script);
}
return dropScripts;
}
}
不确定我是否理解,但以下是我将要做的。
我会创建一个非常好的结构
首先,我首先会从你的类DayExpensesDB中删除你放在那里的所有东西,成员。相反,我会用脚本创建我的数据库,所有东西都是OOP风格的。 例如,您为包含有关列的内容的列创建了一个类,它是主键,是或不是?它是唯一的还是不唯一的?
编辑
添加了从pastebin中删除的代码 //类,该类描述数据库列。
public class DatabaseColumnImpl implements DatabaseColumn {
private boolean uniqueField = false;
private boolean autoIncrement = false;
private boolean isForeignKey = false;
private boolean primaryKey = false;
private String rowName;
private RowType dataRowType;
public GenericColumnImpl(boolean primaryKey, boolean autoIncrement, boolean uniqueField, boolean isForeignKey, String fieldName, @NonNull RowType type) {
this.uniqueField = uniqueField;
this.primaryKey = primaryKey;
this.autoIncrement = autoIncrement;
this.dataRowType = type;
this.rowName = fieldName;
this.isForeignKey = isForeignKey;
}
public DatabaseColumnImpl(boolean primaryKey, boolean autoIncrement, String fieldName, @NonNull RowType type) {
this(primaryKey, autoIncrement, false, false, fieldName, type);
}
//... setters and getters
}
接下来,我将定义我的数据库契约和要插入的行。例如,您的表要求有4-5列,但尚未知道行(请参见下图)。//每行的数据库列。
public enum DatabaseTable{
DATA(new DatabaseColumnImpl(true, true, DataContract.X._ID, SqliteDataType.Integer),
new DatabaseColumnImpl(DataContract.X.TIMEDURATION, SqliteDataType.Integer),
new DatabaseColumnImpl(DataContract.X.DESCRIPTION, SqliteDataType.String),
new DatabaseColumnImpl(DataContract.X.NAME, SqliteDataType.String));
//much more data can be added here
private DatabaseColumnImpl[] genericColumns;
private DatabaseTable(DatabaseColumnImpl... columns) {
this.genericColumns = columns;
}
public DatabaseColumnImpl[] getColumns() {
return genericColumns;
}
public String getName() {
return this.name();
}
}
完成这些之后,我将创建数据库契约类
public class DatabaseContract {
public static final String CONTENT_STRING = "content://";
public static final String CONTENT_AUTHORITY = "com.org.data";
public static final String VND_AUTHORITY = "vnd.org.data";
public static final Uri BASE_CONTENT_URI = Uri.parse(CONTENT_STRING + CONTENT_AUTHORITY);
public static final class XData implements BaseColumns {
public static final String TABLE_NAME = "xdata";
//content://com.org.data/xdata
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(TABLE_NAME).build();
//vnd.android.cursor.dir/vnd.org.data
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + File.separator + VND_AUTHORITY;
//vnd.android.cursor.item/vnd.org.data
public static final String CONTENT_ITEMTYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + File.separator + VND_AUTHORITY;
public static final String NAME = "name";
public static final String DESCRIPTION = "description";
public static final String TIMEDURATION = "time_duration";
//more data
public static Uri buildXdataUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
}
}
设置好前一个结构后,查询如下所示:
public class DatabaseOpenHelper extends SQLiteOpenHelper {
private static final String DROP_START_QUERY = "DROP TABLE IF EXISTS ";
/**
* database name
*/
public static final String DATABASE_NAME = "data.db";
// default statements used for different queries
public static final String PRIMARY_KEY_STATEMENT = " PRIMARY KEY ";
public static final String PRIMARY_KEY_AUTOINCREMENT_STATEMENT = " PRIMARY KEY AUTOINCREMENT ";
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ";
private static final String LEFT_PARENTHESES = "( \n";
private static final String SPACE_CHARACTER = " ";
private static final String CONFLICT_REPLACE_TEXT = ") ON CONFLICT IGNORE ";
public static final String UNIQUE_KEY_STATEMENT = " UNIQUE (";
// end of default queries
private static final int DATABASE_VERSION = 19;
public static final String COMMA_STRING = ",";
public static final String EMPTY_STRING = "";
public static final String TAB_STRING = "\n";
public DatabaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
createTables(db);
}
private void createTables(SQLiteDatabase db) {
Collection<String> scripts = createScripts();
for (String query : scripts) {
db.execSQL(query);
}
}
private Collection<String> createScripts() {
Collection<String> scripts = new ArrayList<>();
for (DatabaseTable table : DatabaseTable.values()) {
String sqlQuery = CREATE_TABLE + table.getName() + LEFT_PARENTHESES;
String primaryKeyQuery = getQueryScript(table);
sqlQuery += primaryKeyQuery;
sqlQuery += ")\n";
scripts.add(sqlQuery);
}
return scripts;
}
private String getQueryScript(DatabaseTable table) {
String query = "";
for (int k = 0; k < table.getColumns().length; k++) {
DatabaseColumn column = table.getColumns()[k];
query += column.getFieldName() + SPACE_CHARACTER + column.getDataType().getRowType();
if (column.isPrimaryKey() && column.isAutoIncremented()) {
query += PRIMARY_KEY_AUTOINCREMENT_STATEMENT;
} else if (column.isPrimaryKey()) {
query += PRIMARY_KEY_STATEMENT;
} else if (column.isUnique()) {
query += UNIQUE_KEY_STATEMENT + column.getFieldName() + CONFLICT_REPLACE_TEXT;
}
query += (k == table.getColumns().length - 1 ? EMPTY_STRING : COMMA_STRING) + TAB_STRING;
}
return query;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//drop tables
List<String> dropScripts = createDropScripts();
for (String query : dropScripts) {
db.execSQL(query);
}
}
/**
* creates a list of all the drop table queries and update them
* to a list so we can delete them in a simple way.
*
* @return array list containing the drop table queries.
*/
private List<String> createDropScripts() {
List<String> dropScripts = new ArrayList<>();
DatabaseTable[] tables = DatabaseTable.values();
for (DatabaseTable table : tables) {
String query_script = DROP_START_QUERY + table.getName() + " ;";
dropScripts.add(query_script);
}
return dropScripts;
}
}
不确定我是否理解,但以下是我将要做的。
我会创建一个非常好的结构
首先,我首先会从你的类DayExpensesDB中删除你放在那里的所有东西,成员。相反,我会用脚本创建我的数据库,所有东西都是OOP风格的。 例如,您为包含有关列的内容的列创建了一个类,它是主键,是或不是?它是唯一的还是不唯一的?
编辑
添加了从pastebin中删除的代码 //类,该类描述数据库列。
public class DatabaseColumnImpl implements DatabaseColumn {
private boolean uniqueField = false;
private boolean autoIncrement = false;
private boolean isForeignKey = false;
private boolean primaryKey = false;
private String rowName;
private RowType dataRowType;
public GenericColumnImpl(boolean primaryKey, boolean autoIncrement, boolean uniqueField, boolean isForeignKey, String fieldName, @NonNull RowType type) {
this.uniqueField = uniqueField;
this.primaryKey = primaryKey;
this.autoIncrement = autoIncrement;
this.dataRowType = type;
this.rowName = fieldName;
this.isForeignKey = isForeignKey;
}
public DatabaseColumnImpl(boolean primaryKey, boolean autoIncrement, String fieldName, @NonNull RowType type) {
this(primaryKey, autoIncrement, false, false, fieldName, type);
}
//... setters and getters
}
接下来,我将定义我的数据库契约和要插入的行。例如,您的表要求有4-5列,但尚未知道行(请参见下图)。//每行的数据库列。
public enum DatabaseTable{
DATA(new DatabaseColumnImpl(true, true, DataContract.X._ID, SqliteDataType.Integer),
new DatabaseColumnImpl(DataContract.X.TIMEDURATION, SqliteDataType.Integer),
new DatabaseColumnImpl(DataContract.X.DESCRIPTION, SqliteDataType.String),
new DatabaseColumnImpl(DataContract.X.NAME, SqliteDataType.String));
//much more data can be added here
private DatabaseColumnImpl[] genericColumns;
private DatabaseTable(DatabaseColumnImpl... columns) {
this.genericColumns = columns;
}
public DatabaseColumnImpl[] getColumns() {
return genericColumns;
}
public String getName() {
return this.name();
}
}
完成这些之后,我将创建数据库契约类
public class DatabaseContract {
public static final String CONTENT_STRING = "content://";
public static final String CONTENT_AUTHORITY = "com.org.data";
public static final String VND_AUTHORITY = "vnd.org.data";
public static final Uri BASE_CONTENT_URI = Uri.parse(CONTENT_STRING + CONTENT_AUTHORITY);
public static final class XData implements BaseColumns {
public static final String TABLE_NAME = "xdata";
//content://com.org.data/xdata
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(TABLE_NAME).build();
//vnd.android.cursor.dir/vnd.org.data
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + File.separator + VND_AUTHORITY;
//vnd.android.cursor.item/vnd.org.data
public static final String CONTENT_ITEMTYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + File.separator + VND_AUTHORITY;
public static final String NAME = "name";
public static final String DESCRIPTION = "description";
public static final String TIMEDURATION = "time_duration";
//more data
public static Uri buildXdataUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
}
}
设置好前一个结构后,查询如下所示:
public class DatabaseOpenHelper extends SQLiteOpenHelper {
private static final String DROP_START_QUERY = "DROP TABLE IF EXISTS ";
/**
* database name
*/
public static final String DATABASE_NAME = "data.db";
// default statements used for different queries
public static final String PRIMARY_KEY_STATEMENT = " PRIMARY KEY ";
public static final String PRIMARY_KEY_AUTOINCREMENT_STATEMENT = " PRIMARY KEY AUTOINCREMENT ";
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ";
private static final String LEFT_PARENTHESES = "( \n";
private static final String SPACE_CHARACTER = " ";
private static final String CONFLICT_REPLACE_TEXT = ") ON CONFLICT IGNORE ";
public static final String UNIQUE_KEY_STATEMENT = " UNIQUE (";
// end of default queries
private static final int DATABASE_VERSION = 19;
public static final String COMMA_STRING = ",";
public static final String EMPTY_STRING = "";
public static final String TAB_STRING = "\n";
public DatabaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
createTables(db);
}
private void createTables(SQLiteDatabase db) {
Collection<String> scripts = createScripts();
for (String query : scripts) {
db.execSQL(query);
}
}
private Collection<String> createScripts() {
Collection<String> scripts = new ArrayList<>();
for (DatabaseTable table : DatabaseTable.values()) {
String sqlQuery = CREATE_TABLE + table.getName() + LEFT_PARENTHESES;
String primaryKeyQuery = getQueryScript(table);
sqlQuery += primaryKeyQuery;
sqlQuery += ")\n";
scripts.add(sqlQuery);
}
return scripts;
}
private String getQueryScript(DatabaseTable table) {
String query = "";
for (int k = 0; k < table.getColumns().length; k++) {
DatabaseColumn column = table.getColumns()[k];
query += column.getFieldName() + SPACE_CHARACTER + column.getDataType().getRowType();
if (column.isPrimaryKey() && column.isAutoIncremented()) {
query += PRIMARY_KEY_AUTOINCREMENT_STATEMENT;
} else if (column.isPrimaryKey()) {
query += PRIMARY_KEY_STATEMENT;
} else if (column.isUnique()) {
query += UNIQUE_KEY_STATEMENT + column.getFieldName() + CONFLICT_REPLACE_TEXT;
}
query += (k == table.getColumns().length - 1 ? EMPTY_STRING : COMMA_STRING) + TAB_STRING;
}
return query;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//drop tables
List<String> dropScripts = createDropScripts();
for (String query : dropScripts) {
db.execSQL(query);
}
}
/**
* creates a list of all the drop table queries and update them
* to a list so we can delete them in a simple way.
*
* @return array list containing the drop table queries.
*/
private List<String> createDropScripts() {
List<String> dropScripts = new ArrayList<>();
DatabaseTable[] tables = DatabaseTable.values();
for (DatabaseTable table : tables) {
String query_script = DROP_START_QUERY + table.getName() + " ;";
dropScripts.add(query_script);
}
return dropScripts;
}
}
不确定我是否理解,但以下是我将要做的。
我会创建一个非常好的结构
首先,我首先会从你的类DayExpensesDB中删除你放在那里的所有东西,成员。相反,我会用脚本创建我的数据库,所有东西都是OOP风格的。 例如,您创建了一个