如何使用dbSqlite购物车?

时间:2019-07-31 12:41:16

标签: java android android-sqlite

我刚刚在项目中使用dbSqlite进行了Login和LoginUp。现在,我正在尝试使用dbSqlite购物车。当我尝试read从数据库中获取数据时,出现此错误: no such table: cart (code 1): , while compiling: select * from cart CartFragment.java中的以下代码会导致此错误:  Cursor rs = db.getData(); 当我使用DB Browser浏览数据库检查Sqlite时,购物车表不存在。我怎么解决这个问题?该错误是因为表不存在?

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context) {
        super(context, "Login.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
       db.execSQL("Create table user(email text primary key, password text)");
       db.execSQL("Create table cart(Image text, Title text, Cost text, TotalCost text)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      db.execSQL("drop table if exists user");
      db.execSQL("drop table if exists cart");
    }

    public boolean insert (String email, String password){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("email", email);
        contentValues.put("password", password);
        long ins = db.insert("user", null, contentValues);
        if (ins==-1) return false;
        else return true;
    }
    // checking if email exists
    public Boolean chkemail(String email){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("Select * from user where email=?", new String[]{email});
        if (cursor.getCount()>0) return false;
        else return true;
    }

    // checking the email and password
    public Boolean emailpassword(String email, String password){
         SQLiteDatabase db = this.getReadableDatabase();
         Cursor cursor = db.rawQuery("select * from user where email=? and password=?", new String[]{email,password});
         if (cursor.getCount()>0) return true;
         else return false;
    }

    public void AddToCart(String Image, String Title, String Cost, String TotalCost){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues data = new ContentValues();
        data.put("Image",Image);
        data.put("Title",Title);
        data.put("Cost",Cost);
        data.put("TotalCost", TotalCost);

        db.insert("cart",null,data);

    }
    public Cursor getData() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor rs =  db.rawQuery( "select * from cart",null);
        return rs;
    }
}

尝试在此向数据库添加数据。 ProductDetailAdapter.java

public class ProductDetailAdapter extends RecyclerView.Adapter<ProductDetailAdapter.ViewHolder> {
    private Context mContext;
    private List<Product> productList;
    private ImageView imgProductDetail;
    private TextView textProductDetailCost, textProductDetailMarket;
    private Button btnProductDetail;
    private CardView cardViewProductDetail;
    DatabaseHelper db;

    public ProductDetailAdapter(Context mContext, List<Product> productList) {
        this.mContext = mContext;
        this.productList = productList;
    }

    @NonNull
    @Override
    public ProductDetailAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_detail_item_view, parent, false);
        return new ViewHolder(mView);    }

    @Override
    public void onBindViewHolder(final ProductDetailAdapter.ViewHolder viewHolder, final int position) {

        Glide.with(mContext).load(productList.get(position).getProductMarketImage())
                .apply(RequestOptions.placeholderOf(R.drawable.ic_glide_img).error(R.drawable.ic_glide_warning)).
                into(imgProductDetail);
        textProductDetailCost.setText(productList.get(position).getProductCost());
     //   textProductDetailMarket.setText(productList.get(position).getProductMarket());
        cardViewProductDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        btnProductDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db = new DatabaseHelper(mContext);
                String img,cost,title,total;
                img = productList.get(position).getProductImage();
                cost = productList.get(position).getProductCost();
                title = productList.get(position).getProductName();
                total = "";
                Log.e("img","Image");
                Log.e("cost","Cost");
                Log.e("title","Title");

                db.AddToCart(img,title,cost,total);
                Toast.makeText(mContext,"Ürün sepetinize eklendi",Toast.LENGTH_SHORT);
            }
        });
    }

    @Override
    public int getItemCount() {
        return productList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        ViewHolder(View itemView){
            super(itemView);
            imgProductDetail = itemView.findViewById(R.id.imageView_product_detail);
            textProductDetailCost = itemView.findViewById(R.id.text_product_detail_cost);
           // textProductDetailMarket = itemView.findViewById(R.id.textView_product_detail_market);
            btnProductDetail = itemView.findViewById(R.id.buttonProductDetail);
            cardViewProductDetail = itemView.findViewById(R.id.cardViewProductDetail);
        }
    }
}

需要在此处从数据库加载数据。 CartFragment.java

public class CartFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private ImageView imageShare;
    List<Product> productList;
    List<Supermarket> supermarketList;
    Product product;
    Supermarket supermarket;
    DatabaseHelper db;
    private Toolbar toolbar;
    private RecyclerView recyclerViewCart;
    private OnFragmentInteractionListener mListener;

    public CartFragment() {
        // Required empty public constructor
    }

    public static CartFragment newInstance(String param1, String param2) {
        CartFragment fragment = new CartFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_cart, container, false);
        imageShare = v.findViewById(R.id.img_cart_share);
        recyclerViewCart = v.findViewById(R.id.recyclerview_cart);
        LinearLayoutManager verticalLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerViewCart.setLayoutManager(verticalLayoutManager);
        db = new DatabaseHelper(getContext());

        Cursor rs = db.getData(); 
        rs.moveToFirst();

        String img = rs.getString(rs.getColumnIndex("Image"));
        String title = rs.getString(rs.getColumnIndex("Title"));
        String cost = rs.getString(rs.getColumnIndex("Cost"));

}

0 个答案:

没有答案