我正在尝试创建两个SQLite表。一个持有“ToDoItem”,另一个持有“DetailItem”。在创建它们并记录它们时,我得到了这个:
I/todos: [ToDoItem [id=1, toDoItemName=1], ToDoItem [id=2,toDoItemName=2]]
I/detailitems: [ToDoItem [id=1, toDoItemName=["1.1","1.2","1.3"]], ToDoItem [id=2, toDoItemName=[]]]
正如您可能注意到的那样,两个表似乎都包含ToDoItems,但是其中一个表应该包含“DetailItem”。 这是我的DatabaseHelper的代码:
public class SQLiteHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "productDB.db";
public static final String TABLE_PRODUCTS = "products";
public static final String TABLE_DETAILS = "details";
//common columns
public static final String COLUMN_ID = "_id";
//todoitem columns
public static final String COLUMN_PRODUCTNAME = "productname";
//detailitem columns
public static final String COLUMN_DETAILID = "detail_id";
public static final String COLUMN_DETAILNAME = "detailname";
private static final String TODOTABLE = "CREATE TABLE " + TABLE_PRODUCTS + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_PRODUCTNAME + " TEXT " +
");";
private static final String DETAILTABLE = "CREATE TABLE " + TABLE_DETAILS + " (" +
COLUMN_DETAILID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_DETAILNAME + " TEXT " +
");";
//We need to pass database information along to superclass
public SQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TODOTABLE);
db.execSQL(DETAILTABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DETAILS);
onCreate(db);
}
//--------TODOITEM METHODS-------//
//Add a new row to the database
public void addProduct(ToDoItem product){
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.getToDoItemName());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_PRODUCTS, null, values);
db.close();
}
//Delete a product from the database
public void deleteProduct(String productName){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";");
}
/*public void deleteProduct(long id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_PRODUCTS, COLUMN_ID + " = ?",
new String[] { String.valueOf(id) });
}*/
public List getAllToDos() {
List toDoItems = new LinkedList();
String query = "SELECT * FROM " + TABLE_PRODUCTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// parse all results
ToDoItem toDoItem = null;
if (cursor.moveToFirst()) {
do {
toDoItem = new ToDoItem();
toDoItem.setId(Integer.parseInt(cursor.getString(0)));
toDoItem.setToDoItemName(cursor.getString(1));
toDoItems.add(toDoItem);
} while (cursor.moveToNext());
}
return toDoItems;
}
public int updateProduct(ToDoItem toDoItem, String v1) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, v1);
// updating row
int i = db.update(TABLE_PRODUCTS, values, COLUMN_ID + " = ?", new String[] { String.valueOf(toDoItem.getId()) });
db.close();
return i;
}
//----------DETAILITEM methods-----------//
public void addDetailItemName(DetailItem detailItem){
ContentValues values = new ContentValues();
values.put(COLUMN_DETAILNAME, detailItem.getDetailText());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_DETAILS, null, values);
db.close();
}
public void deleteDetailItem(String detailName){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_DETAILS + " WHERE " + COLUMN_DETAILNAME + "=\"" + detailName + "\";");
}
public List getAllDetailItems() {
List detailItems = new LinkedList();
String query = "SELECT * FROM " + TABLE_DETAILS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// parse all results
DetailItem detailItem = null;
if (cursor.moveToFirst()) {
do {
detailItem = new DetailItem();
detailItem.setId(Integer.parseInt(cursor.getString(0)));
detailItem.setDetailText(cursor.getString(1));
detailItems.add(detailItem);
} while (cursor.moveToNext());
}
return detailItems;
}
public int updateDetailItem(DetailItem detailItem, String v1) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_DETAILNAME, v1);
// updating row
int i = db.update(TABLE_DETAILS, values, COLUMN_DETAILID + " = ?", new String[] { String.valueOf(detailItem.getId()) });
db.close();
return i;
}
}
DetailItem的代码:
public class DetailItem {
public String _detailText;
private int _id;
public DetailItem(){}
public int getId() {
return _id;
}
public void setId(int id) {
this._id = id;
}
public DetailItem(String detailText){
this._detailText = detailText;
}
public String getDetailText() {
return _detailText;
}
public void setDetailText(String detailText) {
this._detailText = detailText;
}
@Override
public String toString() {
return "ToDoItem [id=" + _id + ", toDoItemName=" + _detailText + "]";
}
}
ToDoItem的代码:
public class ToDoItem {
private String _toDoItemName;
private int _id;
public ToDoItem(){}
public ToDoItem(String toDoItemName){
this._toDoItemName = toDoItemName;
}
public int getId() {
return _id;
}
public void setId(int id) {
this._id = id;
}
public void setToDoItemName(String toDoItemName) {
this._toDoItemName = toDoItemName;
}
public String getToDoItemName(){
return _toDoItemName;
}
@Override
public String toString() {
return "ToDoItem [id=" + _id + ", toDoItemName=" + _toDoItemName + "]";
}
}
答案 0 :(得分:1)
您的app/build.gradle
和ToDoItem
类似乎都在其DetailItem
方法中报告了ToDoItem
。尝试区分这些,然后检查您的日志输出是否准确反映了这一变化。