android - 我正在尝试创建一个数据库和表。创建了数据库但没有创建表为什么?
问题描述
如何在 android pie 版本中创建 SQLite 数据库。我也知道如何在其他版本中创建数据库,但它不适合这个 android 版本。
解决方案
举这个例子来更好地理解。
我在下面向您展示我的代码:
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;
}
}
推荐阅读
- sql - 在 oracle 中查询范围的更好方法
- swift - 快速向 MFMailComposeViewController 展示来自 UIActivityViewController 的附件
- laravel - Vuetify(Vue + Laravel)路由器推送后未设置导航栏活动链接
- user-permissions - 由于致命错误 wp_get_current_user 无法访问网站或后台
- google-cloud-platform - Postman - 通过身份验证保护的访问 API:Google Identity Aware Proxy 背后的 Bearer Token
- nestjs - Nestjs 测试:升级到 v8 后无法使用 app.get?
- kendo-ui - 多 ”
剑道 UI 图表中的“属性转换”错误会破坏 pdf 导出 - c# - 使用一个大项目处理来自另一个解决方案的 XAML 按钮单击
- highcharts - 股票条形图多线系列的高图表问题
- javascript - 在 Gulp 4 中将 gulpfile.js 拆分为多个文件的问题