Ich habe eine Tabelle mit dem Namen 'products3' mit den Spalten 'id', 'type', 'width', 'length', 'thickness', ' menge '.. (in der Reihenfolge) und anders als' type 'alle anderen Spalten sind' int ' Ich möchte die Menge auswählen und sie durch Filtern von Daten in anderen Spalten zurückgeben. Cursor in Methode 'findproduct' gibt false oder NULL in der if-Bedingung zurück, obwohl die Daten in der Tabelle vorhanden sind Ich habe die Tabelle mit dem DB-Browser für SQLite durchsucht !! bittecursor.moveToFirst() gibt false zurück, obwohl Daten in der Tabelle vorhanden sind
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();
}
}
fügen Sie das Fehlerprotokoll hinzu. –
Stack-Trace bitte – Shahar
@Suhail Jain versuchen Sie diese SQLiteDatabase db = this.getReadableDatabase(); –