我正在尝试创建数据库和表。创建数据库但未创建表为什么?

时间:2019-07-08 10:40:46

标签: android

如何在android pie版本中创建SQLite数据库。我也知道如何在其他版本中创建数据库,但是不适用于此android版本。

1 个答案:

答案 0 :(得分:0)

以该示例为例,以更好地理解

我在下面向您显示我的代码:

public class DatabaseClass extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "DBName";

    // TABLE FOR PRODUCT
    private static final String TABLE_NAME = "Table_MyCart";
    private static final String AutoID = "autoId";
    private static final int Auto_ID = 0;
    private static final String ThumbnailImage = "thumbnailImage";
    private static final String ActualImage = "actualImage";
    private static final String ProductId = "productId";
    private static final String ProductName = "productName";
    private static final String ProductDescription = "productDescription";
    private static final String ProductCost = "productCost";
    private static final String CategoryId = "categoryId";
    private static final String ProductCode = "productCode";
    private static final String NumberOfItems = "numberOfItems";
    private static final String RemoveStatus = "removeStatus";

    SQLiteDatabase db;

    /**
     * @param context : represents the reference of activity.
     * @desc : This method is used to create the instance of the class.
     */

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

    /**
     * @param db : represents the reference of SQLiteDatabase
     * @desc : This method is used to create the tables in local database.
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_FRIEND_TABLE = "CREATE TABLE " + TABLE_NAME + "("
                + AutoID + " INTEGER PRIMARY KEY AUTOINCREMENT ," + ThumbnailImage + " TEXT ,"
                + ActualImage + " TEXT ," + ProductId + " TEXT ,"
                + ProductName + " TEXT," + ProductDescription + " TEXT,"
                + ProductCost + " TEXT ," + CategoryId + " TEXT ,"
                + ProductCode + " TEXT, " + NumberOfItems
                + " REAL, " + RemoveStatus + " INTEGER DEFAULT 0" + ")";

        db.execSQL(CREATE_FRIEND_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

        db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);

        onCreate(db);
    }

    /**
     * @param myCart : represents the reference of My cart class.
     * @desc : This method is used to insert product in local database.
     */
    public long insertMyCart(MyCart myCart) {

        ContentValues cv = null;
        db = this.getWritableDatabase();
        try {
            cv = new ContentValues();
            cv.put(ThumbnailImage, myCart.getThumbnailImage());
            cv.put(ActualImage, myCart.getActualImage());
            cv.put(ProductId, myCart.getProductId());
            cv.put(ProductName, myCart.getProductName());
            cv.put(ProductDescription, myCart.getProductDescription());
            cv.put(ProductCost, myCart.getProductCost());
            cv.put(CategoryId, myCart.getCategoryId());
            cv.put(ProductCode, myCart.getProductCode());
            cv.put(NumberOfItems, myCart.getNumberOfItems());
            cv.put(RemoveStatus, myCart.isRemoveStatus());
            return db.insert(TABLE_NAME, null, cv);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
        return -1;
    }

    /**
     * @param myCart : represents the reference of My cart class.
     * @desc : This method is used to update the existed product in local database.
     */
    public void updateMyCart(MyCart myCart) {
        try {
            db = this.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(ThumbnailImage, myCart.getThumbnailImage());
            cv.put(ActualImage, myCart.getActualImage());
            cv.put(ProductId, myCart.getProductId());
            cv.put(ProductName, myCart.getProductName());
            cv.put(ProductDescription, myCart.getProductDescription());
            cv.put(ProductCost, myCart.getProductCost());
            cv.put(CategoryId, myCart.getCategoryId());
            cv.put(ProductCode, myCart.getProductCode());
            cv.put(NumberOfItems, myCart.getNumberOfItems());
            cv.put(RemoveStatus, myCart.isRemoveStatus());

            long value = db.update(TABLE_NAME, cv, ProductId + " = ?", new String[]{String.valueOf(myCart.getProductId())});
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
    }

    /**
     * @param myCart : represents the reference of My cart class.
     * @desc : This method is used to deleting the existed product in local database.
     */
    public void deleteMyCart(MyCart myCart) {
        try {
            db = this.getWritableDatabase();
            db.delete(TABLE_NAME, AutoID + " = ?",
                    new String[]{String.valueOf(myCart.getAutoId())});

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
    }

    /**
     * @desc : This method is used to return all the products from local database.
     */
    public List<MyCart> getMyCartList() {
        List<MyCart> myCartList = new ArrayList<MyCart>();
        db = this.getReadableDatabase();
        String sql12 = "SELECT * from " + TABLE_NAME;
        try {
            @SuppressLint("Recycle") Cursor cursor = db.rawQuery(sql12, null);
            if (cursor.moveToFirst()) {
                do {

                    MyCart friend = new MyCart();
                    friend.setAutoId(cursor.getString(0));
                    friend.setThumbnailImage(cursor.getString(1));
                    friend.setActualImage(cursor.getString(2));
                    friend.setProductId(cursor.getString(3));
                    friend.setProductName(cursor.getString(4));
                    friend.setProductDescription(cursor.getString(5));
                    friend.setProductCost(cursor.getString(6));
                    friend.setCategoryId(cursor.getString(7));
                    friend.setProductCode(cursor.getString(8));
                    friend.setNumberOfItems(cursor.getInt(9));

                    friend.setRemoveStatus(cursor.getInt(cursor.getColumnIndex("removeStatus")) == 1);
                    // Adding contact to list
                    myCartList.add(friend);
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
        // return contact list
        return myCartList;

    }

    /**
     * @param productId : represents the id of every product which is present in local database.
     * @desc : This method is used to check product is already exist or not..
     */
    public boolean checkIdExist(String productId) throws SQLException {
        int count = -1;
        Cursor c = null;
        db = this.getReadableDatabase();
        try {
            String query = "SELECT COUNT(*) FROM "
                    + TABLE_NAME + " WHERE " + ProductId + " = ?";
            c = db.rawQuery(query, new String[]{productId});
            if (c.moveToFirst()) {
                count = c.getInt(0);
            }
            return count > 0;
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    /**
     * @desc : This method is used to get the counts of My Cart table.
     */
    public long getTableSize() {
        long count = 0;
        try {
            db = this.getReadableDatabase();
            count = DatabaseUtils.queryNumEntries(db, TABLE_NAME);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
        return count;
    }

    /**
     * @desc : This method is used to clear My Cart table.
     */
    public void clearMyCartTable() {
        try {
            db = this.getWritableDatabase();
            db.execSQL("delete from " + TABLE_NAME);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
    }
}

MyCart.java

public class MyCart {

    private String autoId;
    private String thumbnailImage;
    private String actualImage;
    private String productId;
    private String productName;
    private String productDescription;
    private String productCost;
    private String categoryId;
    private String productCode;
    private int numberOfItems;
    private boolean removeStatus;

    public MyCart() {

    }

    public MyCart(String autoId, String thumbnailImage, String actualImage, String productId,
                  String productName, String productDescription, String productCost,
                  String categoryId, String productCode, int numberOfItems, boolean removeStatus) {
        this.autoId = autoId;
        this.thumbnailImage = thumbnailImage;
        this.actualImage = actualImage;
        this.productId = productId;
        this.productName = productName;
        this.productDescription = productDescription;
        this.productCost = productCost;
        this.categoryId = categoryId;
        this.productCode = productCode;
        this.numberOfItems = numberOfItems;
        this.removeStatus = removeStatus;
    }

    public String getAutoId() {
        return autoId;
    }

    public void setAutoId(String autoId) {
        this.autoId = autoId;
    }

    public String getThumbnailImage() {
        return thumbnailImage;
    }

    public void setThumbnailImage(String thumbnailImage) {
        this.thumbnailImage = thumbnailImage;
    }

    public String getActualImage() {
        return actualImage;
    }

    public void setActualImage(String actualImage) {
        this.actualImage = actualImage;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductDescription() {
        return productDescription;
    }

    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    public String getProductCost() {
        return productCost;
    }

    public void setProductCost(String productCost) {
        this.productCost = productCost;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    public String getProductCode() {
        return productCode;
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    public int getNumberOfItems() {
        return numberOfItems;
    }

    public void setNumberOfItems(int numberOfItems) {
        this.numberOfItems = numberOfItems;
    }

    public boolean isRemoveStatus() {
        return removeStatus;
    }

    public void setRemoveStatus(boolean removeStatus) {
        this.removeStatus = removeStatus;
    }

    @Override
    public String toString() {
        return "" + numberOfItems;
    }
}