我无法从Sq-lite

时间:2016-10-01 11:41:23

标签: android android-sqlite

我已创建数据库以执行添加到购物车功能。 这是我的数据库助手类。



public class SQLiteHandler extends SQLiteOpenHelper {

    private static final String TAG = SQLiteHandler.class.getSimpleName();
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "soulcluster";

    // User table name
    private static final String TABLE_USER = "user";

    private static final String TABLE_PRODUCT = "product";

    private static final String TABLE_ADD_TO_CART = "addtocart";

    // User Table Columns names
    private static final String KEY_NAME = "name";
    private static final String KEY_EMAIL = "email";
    private static final String KEY_PASSWORD = "password";

    private static final String KEY_PRODUCT_ID = "id";
    private static final String KEY_PRODUCT_IMAGE = "image";
    private static final String KEY_PRODUCT_NAME = "productname";
    private static final String KEY_PRODUCT_PRICE = "productprice";
    private static final String KEY_PRODUCT_DESCRIPTION = "productdescription";


public SQLiteHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_USER_TABLE = "CREATE TABLE "
                + TABLE_USER + "("
                + KEY_NAME + " TEXT,"
                + KEY_EMAIL + " TEXT UNIQUE,"
                + KEY_PASSWORD + " TEXT" + ")";

        String CREATE_PRODUCT_TABLE = "CREATE TABLE "
                + TABLE_PRODUCT + "("
                + KEY_PRODUCT_ID + " TEXT PRIMARY KEY,"
                + KEY_EMAIL + " TEXT,"
                + KEY_PRODUCT_IMAGE + " BLOB NOT NULL,"
                + KEY_PRODUCT_NAME + " TEXT NOT NULL,"
                + KEY_PRODUCT_PRICE + " TEXT NOT NULL,"
                + KEY_PRODUCT_DESCRIPTION + " TEXT NOT NULL" + ")";

        String CREATE_ADD_TO_CART_TABLE = "CREATE TABLE "
                + TABLE_ADD_TO_CART + "("
                + KEY_PRODUCT_ID + " TEXT PRIMARY KEY,"
                + KEY_PRODUCT_IMAGE + " BLOB NOT NULL,"
                + KEY_PRODUCT_NAME + " TEXT NOT NULL,"
                + KEY_PRODUCT_PRICE + " TEXT NOT NULL,"
                + KEY_PRODUCT_DESCRIPTION + " TEXT NOT NULL" + ")";

        db.execSQL(CREATE_USER_TABLE);
        db.execSQL(CREATE_PRODUCT_TABLE);
        db.execSQL(CREATE_ADD_TO_CART_TABLE);
        Log.d(TAG, "Database tables created" + CREATE_USER_TABLE + " " + CREATE_PRODUCT_TABLE + " " + CREATE_ADD_TO_CART_TABLE);
    }
public void addToCart(String product_id, byte [] image, String productName, String productPrice, String productDescription){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_PRODUCT_ID, product_id);
        contentValues.put(KEY_PRODUCT_IMAGE, image);
        contentValues.put(KEY_PRODUCT_NAME, productName);
        contentValues.put(KEY_PRODUCT_PRICE, productPrice);
        contentValues.put(KEY_PRODUCT_DESCRIPTION, productDescription);

        long id = db.insert(TABLE_ADD_TO_CART, null, contentValues);
        db.close();

        Log.d(TAG, "New product added into Add to cart " + product_id + " " + image + " " + productName + " " + productPrice + " " + productDescription + " " +id);
    }

    public void deleteAddToCartItem(String id){
        try {
            SQLiteDatabase db = this.getWritableDatabase();
            String query = "delete from " + TABLE_ADD_TO_CART + " where id=\'" + id + "'";
            Log.d("", "Id is here " + id);
            db.execSQL(query);
            db.close();
        }catch (Exception e){
            Log.d("", " Something is wrong");
        }

    }
public ArrayList<AddToCartItems> fetchProduct()
    {
        ArrayList<AddToCartItems> product = new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();

        String query = "select * from " + TABLE_ADD_TO_CART  ;
        Cursor cursor =  db.rawQuery(query, null);

        if(cursor.moveToFirst()){

            do{
                AddToCartItems addToCartItems = new AddToCartItems();
                addToCartItems.setId(cursor.getString(0));
                addToCartItems.setImageArray(cursor.getBlob(1));
                addToCartItems.setName(cursor.getString(2));
                addToCartItems.setPrice(cursor.getString(3));
                addToCartItems.setDescription(cursor.getString(4));

                product.add(addToCartItems);
                Log.d("", "check -----" + cursor.getString(cursor.getColumnIndex("productname")) + " " + cursor.getString(cursor.getColumnIndex("id")));
                cursor.moveToNext();
                Log.d("", "Inside fetching method" + " " + cursor.getCount() + " " + product.size() );
                System.out.print(cursor.getCount());
            }while (cursor.moveToNext());


        /*    for (int a = 0; a < cursor.getCount(); a++){

            }*/
        }
        Log.d("", "Checkimh product size " + product.size());
        db.close();
        return product;
    }
&#13;
&#13;
&#13;

我刚发布了必需的方法。 在我的适配器类中,单击添加到购物车存储桶,产品将添加到数据库中。

&#13;
&#13;
holder.add_to_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), items.getItem_Image());
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] bitmapdata = stream.toByteArray();

                Date d = new Date();
                SimpleDateFormat sm = new SimpleDateFormat("yyyymmddhhmmss");
                String productID = sm.format(d);

                db.addToCart(productID, bitmapdata, items.getItem_name(), items.getItem_price(), items.getDescription());
                //AppController.getInstance().addToCartItemses(items.getItem_Image(), items.getItem_name(), items.getItem_price(), items.getDescription());
                Log.d(""," ShopAdapter " + " " + bitmapdata + " " + productID + " " + items.getItem_Image() + " "+ items.getItem_name()+ " "+ items.getItem_price() +" " +items.getDescription());
            }
        });
&#13;
&#13;
&#13;

这是方法。我使用了图像作为按钮,并在点击图像插入查询时触发。

这是下一个活动适配器类

中的删除方法

&#13;
&#13;
holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                db.deleteAddToCartItem(addToCartItemses.get(position).getId());
                addToCartItemses.remove(position);
                notifyDataSetChanged();
            }
        });
&#13;
&#13;
&#13;

为什么我的查询没有运行。

1 个答案:

答案 0 :(得分:1)

不必专门使用任何内容作为用户唯一ID,因为为每个登录用户分配了唯一ID ,您可以像这样检索它:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // Name, email address, and profile photo Url
    String name = user.getDisplayName();
    String email = user.getEmail();
    Uri photoUrl = user.getPhotoUrl();

    // The user's ID, unique to the Firebase project. Do NOT use this value to
    // authenticate with your backend server, if you have one. Use
    // FirebaseUser.getToken() instead.
    String uid = user.getUid();
}

此代码段可在官方firebase文档here

中找到

如果此信息可以帮助您,请告诉我。