我尝试过 sqlite 数据库表无法获取
notification
表。我没有显示通知表 SQLite 数据库。我不知道我在哪里做错了。如果您知道错误,请告诉我。图片点击显示->
public class DbHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "db_recipes_favorite";
private static final String TABLE_NAME = "tbl_recipes_favorite";
private static final String TABLE_NOTIFICATION = "notification";
private static final String KEY_ID = "id";
private static final String KEY_CAT_NAME = "category_name";
private static final String KEY_RECIPE_ID = "recipe_id";
private static final String KEY_RECIPE_TITLE = "recipes_title";
private static final String KEY_RECIPE_TIME = "recipe_time";
private static final String KEY_RECIPE_IMAGE = "recipe_image";
private static final String KEY_RECIPE_DESCRIPTION = "recipe_description";
private static final String KEY_VIDEO_URL = "video_url";
private static final String KEY_VIDEO_ID = "video_id";
private static final String KEY_CONTENT_TYPE = "content_type";
private static final String KEY_FEATURED = "featured";
private static final String KEY_TAGS = "tags";
private static final String KEY_TOTAL_VIEWS = "total_views";
private static final String unique_id = "unique_id";
private static final String title = "title";
private static final String message = "message";
private static final String big_image = "big_image";
private static final String link = "link";
private static final String post_id = "post_id";
public DbHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_CAT_NAME + " TEXT,"
+ KEY_RECIPE_ID + " TEXT,"
+ KEY_RECIPE_TITLE + " TEXT,"
+ KEY_RECIPE_TIME + " TEXT,"
+ KEY_RECIPE_IMAGE + " TEXT,"
+ KEY_RECIPE_DESCRIPTION + " TEXT,"
+ KEY_VIDEO_URL + " TEXT,"
+ KEY_VIDEO_ID + " TEXT,"
+ KEY_CONTENT_TYPE + " TEXT,"
+ KEY_FEATURED + " TEXT,"
+ KEY_TAGS + " TEXT,"
+ KEY_TOTAL_VIEWS + " TEXT"
+ ")";
db.execSQL(CREATE_CONTACTS_TABLE);
String CREATE_CONTACTS_NOTIFICATION = "CREATE TABLE " + TABLE_NOTIFICATION + "("
+ unique_id + " INTEGER PRIMARY KEY,"
+ title + " TEXT,"
+ message + " TEXT,"
+ big_image + " TEXT,"
+ link + " TEXT,"
+ post_id + " TEXT"
+ ")";
db.execSQL(CREATE_CONTACTS_NOTIFICATION);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTIFICATION);
// Create tables again
onCreate(db);
}
//Adding Record in Database
public void AddtoFavorite(Recipe p) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CAT_NAME, p.getCategory_name());
values.put(KEY_RECIPE_ID, p.getRecipe_id());
values.put(KEY_RECIPE_TITLE, p.getRecipe_title());
values.put(KEY_RECIPE_TIME, p.getRecipe_time());
values.put(KEY_RECIPE_IMAGE, p.getRecipe_image());
values.put(KEY_RECIPE_DESCRIPTION, p.getRecipe_description());
values.put(KEY_VIDEO_URL, p.getVideo_url());
values.put(KEY_VIDEO_ID, p.getVideo_id());
values.put(KEY_CONTENT_TYPE, p.getContent_type());
values.put(KEY_FEATURED, p.getFeatured());
values.put(KEY_TAGS, p.getTags());
values.put(KEY_TOTAL_VIEWS, p.getTotal_views());
// Inserting Row
db.insert(TABLE_NAME, null, values);
db.close(); // Closing database connection
}
// Getting All Data
public List<Recipe> getAllData() {
List<Recipe> dataList = new ArrayList<Recipe>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME + " ORDER BY id DESC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Recipe values = new Recipe();
values.setId(Integer.parseInt(cursor.getString(0)));
values.setCategory_name(cursor.getString(1));
values.setRecipe_id(cursor.getString(2));
values.setRecipe_title(cursor.getString(3));
values.setRecipe_time(cursor.getString(4));
values.setRecipe_image(cursor.getString(5));
values.setRecipe_description(cursor.getString(6));
values.setVideo_url(cursor.getString(7));
values.setVideo_id(cursor.getString(8));
values.setContent_type(cursor.getString(9));
values.setFeatured(cursor.getString(10));
values.setTags(cursor.getString(11));
values.setTotal_views(cursor.getLong(12));
// Adding contact to list
dataList.add(values);
} while (cursor.moveToNext());
}
// return contact list
return dataList;
}
//getting single row
public List<Recipe> getFavRow(String id) {
List<Recipe> dataList = new ArrayList<Recipe>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE recipe_id=" + id;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Recipe values = new Recipe();
values.setId(Integer.parseInt(cursor.getString(0)));
values.setCategory_name(cursor.getString(1));
values.setRecipe_id(cursor.getString(2));
values.setRecipe_title(cursor.getString(3));
values.setRecipe_time(cursor.getString(4));
values.setRecipe_image(cursor.getString(5));
values.setRecipe_description(cursor.getString(6));
values.setVideo_url(cursor.getString(7));
values.setVideo_id(cursor.getString(8));
values.setContent_type(cursor.getString(9));
values.setFeatured(cursor.getString(10));
values.setTags(cursor.getString(11));
values.setTotal_views(cursor.getLong(12));
// Adding contact to list
dataList.add(values);
} while (cursor.moveToNext());
}
// return contact list
return dataList;
}
//for remove favorite
public void RemoveFav(Recipe contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_RECIPE_ID + " = ?",
new String[]{String.valueOf(contact.getRecipe_id())});
db.close();
}
public void notifcation(long unique_id, String title, String message, String big_image, String link, long post_id) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("unique_id", unique_id);
values.put("title", title);
values.put("message", message);
values.put("big_image", big_image);
values.put("link", link);
values.put("post_id", post_id);
// Inserting Row
db.insert(TABLE_NOTIFICATION, null, values);
db.close(); // Closing database connection
}
public enum DatabaseManager {
INSTANCE;
private SQLiteDatabase db;
private boolean isDbClosed = true;
DbHandler dbHelper;
public void init(Context context) {
dbHelper = new DbHandler(context);
if (isDbClosed) {
isDbClosed = false;
this.db = dbHelper.getWritableDatabase();
}
}
public boolean isDatabaseClosed() {
return isDbClosed;
}
public void closeDatabase() {
if (!isDbClosed && db != null) {
isDbClosed = true;
db.close();
dbHelper.close();
}
}
}
}
在
onCreate()
中添加通知表创建之前,您是否运行过应用程序?
开发数据库应用程序时的一个常见问题是,如果您增量添加表,但不更新数据库版本,则
onCreate()
不会再次运行,onUpgrade()
也不会。
要测试该理论,请将
DATABASE_VERSION
更改为 2
并重新运行。如果这是问题所在,您应该创建该表。请注意,您的 onCreate()
函数中有两次 onUpgrade()
运行 - 第二次可能会失败,因为 tbl_recipes_favorite
已经存在 - 我建议删除第一个表。
如果有效,请将您的
DATABASE_VERSION
更改回 1
。我还建议卸载该应用程序,然后再次运行。
请注意,您也可以在添加新表时卸载,下次运行将从头开始创建数据库。
仅供参考 - 您可能想研究一下 Room - 它确实简化了数据库管理。您所拥有的一切都可以发挥作用,但需要维护的工作还有很多。 Room 还会在未更新版本的情况下警告您数据库已更改。