我有一个名为'products3'的表,其中包含列'id','type','width','length','thickness','quantity'..(按顺序)而不是'type'所有其他列都是'int' 我希望选择数量并通过过滤其他列上的数据来返回它。 方法'findproduct'中的游标在if条件中返回false或NULL,即使数据存在于表中我使用DB浏览器浏览SQLite !!请帮忙
public class MyDBHandler extends SQLiteOpenHelper {
int s;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "productDB3.db";
private static final String TABLE_PRODUCTS = "products3";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TYPE = "type";
public static final String COLUMN_QUANTITY = "quantity";
public static final String COLUMN_WIDTH = "width";
public static final String COLUMN_LENGTH = "length";
public static final String COLUMN_THICK = "thick";
public MyDBHandler(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +
TABLE_PRODUCTS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY autoincrement," + COLUMN_TYPE
+ " TEXT," + COLUMN_WIDTH + " INTEGER," + COLUMN_LENGTH + " INTEGER," + COLUMN_THICK + " INTEGER,"
+ COLUMN_QUANTITY + " INTEGER" + ")";
db.execSQL(CREATE_PRODUCTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
public void addProduct(String type,int width,int length,int thick,int quant) {
ContentValues values = new ContentValues();
values.put(COLUMN_TYPE,type);
values.put(COLUMN_WIDTH,width);
values.put(COLUMN_LENGTH,length);
values.put(COLUMN_THICK,thick);
values.put(COLUMN_QUANTITY,quant);
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_PRODUCTS, null, values);
db.close();
}
public int findProduct(String type,int width,int length,int thick) {
String query = "SELECT quantity FROM products3 WHERE type='" + type +"' AND width=" + width + " AND length=" +
length+" AND thick=" + thick + ";";
SQLiteDatabase db = this.getWritableDatabase();
if (cursor.moveToFirst()) {
cursor.moveToFirst();
s=cursor.getInt(0);
cursor.close();
return s;
}
db.close();
return 0;
}
public boolean deleteProduct(String type,int width,int length,int thick) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM products3 WHERE type='" + type +"' and width=" + width + " and length=" +
length+" and thick=" + thick );
db.close();
}
}
答案 0 :(得分:0)
如果您在调试SQL查询时遇到问题,Android会提供一个方便的方法DatabaseUtils.dumpCursorToString(),将整个Cursor格式化为String。然后,您可以将转储输出到LogCat并查看查询结果。