我有一个recyclereview,它从json(online)获取数据并建立一个sqlite数据库,以便在用户离线时也可以阅读。我想创建一列以显示此帖子是否在用户的收藏夹列表中。但是在我写完这些代码后,我的应用程序无法启动,并且崩溃了,并且在android监视器中,我有致命异常提示:没有这样的表:tbl_posts(code1),而编译时:SELECT * FROM tbl_post。这是怎么回事?这是我的数据库OpenHelper代码:
public class PostDatabseOpenHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseOpenHelper";
private static final String DATABASE_NAME="db_post";
private static final int DATABASE_VERSION=1;
private static final String POST_TABLE_NAME="tbl_posts";
private static final String COL_ID="col_id";
private static final String COL_CONTENT="col_content";
private static final String COL_FAV="0";
private static final String SQL_COMMAND_CREATE_POST_TABLE="CREATE TABLE IF
NOT EXISTS "+POST_TABLE_NAME+"("+
COL_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
COL_CONTENT+" TEXT, "+
COL_FAV+" TEXT);";
Context context;
public PostDatabseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(SQL_COMMAND_CREATE_POST_TABLE);
}catch (SQLException e){
Log.e(TAG, "onCreate: "+e.toString() );
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public boolean addPost(Post post){
ContentValues cv=new ContentValues();
cv.put(COL_ID,post.getId());
cv.put(COL_CONTENT,post.getContent());
cv.put(COL_FAV,post.getFav());
SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
long isInserted=sqLiteDatabase.insert(POST_TABLE_NAME,null,cv);
Log.i(TAG, "addPost: "+isInserted);
if (isInserted>0){
return true;
}else{
return false;
}
}
public void addPosts(List<Post> posts){
for (int i = 0; i < posts.size(); i++) {
if (!checkPostExists(posts.get(i).getId())) {
addPost(posts.get(i));
}
}
}
public List<Post> getPosts(){
List<Post>posts=new ArrayList<>();
SQLiteDatabase sqliteDatabase =this.getReadableDatabase();
Cursor cursor= sqliteDatabase.rawQuery("SELECT * FROM "+POST_TABLE_NAME,null);
cursor.moveToFirst();
if (cursor.getCount()>0){
while (!cursor.isAfterLast()){
Post post=new Post();
post.setId(cursor.getInt(0));
post.setContent(cursor.getString(1));
post.setFav(cursor.getString(2));
posts.add(post);
cursor.moveToNext();
}
}
cursor.close();
sqliteDatabase.close();
return posts;
}
public boolean checkPostExists(int postId){
SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
Cursor cursor=sqLiteDatabase.rawQuery("SELECT * FROM "
+POST_TABLE_NAME
+" WHERE "
+COL_ID
+" = ?",new String[]{String.valueOf(postId)});
return cursor.moveToFirst();
}
public boolean setBookFavoriteState(int postId, int state){
SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_FAV, state);
long result = sqLiteDatabase.update(
POST_TABLE_NAME , cv , "id = ?" , new String[] { String.valueOf(postId) }
);
if( result < 1 )
return false;
else
return true;
}
public int getBookFavoriteState( int id )
{SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
Cursor result = sqLiteDatabase.rawQuery(
"SELECT * FROM " + POST_TABLE_NAME + " WHERE id = '" + id + "'", null
);
result.moveToFirst();
return Integer.parseInt( result.getString( 2 ) );
}}
这是我在MainActivity中使用的代码:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
ImageButton btnlike;
PostDatabseOpenHelper daily;
int postid,state;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnlike = (ImageButton) findViewById(R.id.btnlike);
daily=new PostDatabseOpenHelper(this);
setupRecyclerView();
getPostsFromDatabase();
btnlike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean resulti= daily.setBookFavoriteState(postid,state);
if (resulti==true){
btnlike.setImageDrawable(ResourcesCompat.getDrawable(getResources(),R.drawable.nostar,null));
}else {
btnlike.setImageDrawable(ResourcesCompat.getDrawable(getResources(),R.drawable.star,null));
}
}
});}}