无法读取SQLite数据库

时间:2018-01-31 19:11:28

标签: android sqlite

请帮忙! 按下按钮,始终传输参数0。

MainActivity.class

this.listResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            dbResult.read(RssActivity.this);
            RssData rssData = dbResult.select(id);
            Toast.makeText(getBaseContext(), valueOf(id), Toast.LENGTH_SHORT).show();

数据库

public class DBResult {
...
SQLiteDatabase database;

public DBResult(Context context){
    DBResult.ResultOpenHelper openHelper = new DBResult.ResultOpenHelper(context);
    database = openHelper.getWritableDatabase();
}
public long insert(RssData rssData) {
    ContentValues cv = new ContentValues();
    cv.put(RSS_NAME, rssData.getRssName());
    cv.put(RSS_TITLE, rssData.getTitle());
    cv.put(RSS_LINK, rssData.getLink());
    cv.put(RSS_DESCRIPTION, rssData.getDescription());
    return database.insert(TABLE_NAME, null, cv);
}
public int update(RssData rssData){
    ...
}
public RssData select(long id){
    Cursor cursor = database.query(TABLE_NAME,null, RSS_ID + " = ?", new String[]{String.valueOf(id)}, null, null, RSS_ID);
    if (cursor.moveToFirst()){
    String rssName = cursor.getString(NUM_RSS_NAME);
    String title = cursor.getString(NUM_RSS_TITLE);
    String link = cursor.getString(NUM_RSS_LINK);
    String description = cursor.getString(NUM_RSS_DESCRIPTION);
    new RssData (id, rssName, title, link, description);}
    while (cursor.moveToNext());
    return null;
}
...
private class ResultOpenHelper extends SQLiteOpenHelper {
    public ResultOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        database=db;
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
                RSS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                RSS_NAME + " TEXT, " +
                RSS_TITLE + " TEXT, " +
                RSS_DESCRIPTION + " TEXT, " +
                RSS_LINK + " TEXT ); ";
        db.execSQL(sql);
    }
    ...
}

我做错了什么?我需要一个链接在WebView中打开它。

错误:“在空对象引用上”

我将非常感谢任何帮助!

提前谢谢!

1 个答案:

答案 0 :(得分:0)

以下内容将始终返回 null

public RssData select(long id){
    Cursor cursor = database.query(TABLE_NAME,null, RSS_ID + " = ?", new String[]{String.valueOf(id)}, null, null, RSS_ID);
    if (cursor.moveToFirst()){
    String rssName = cursor.getString(NUM_RSS_NAME);
    String title = cursor.getString(NUM_RSS_TITLE);
    String link = cursor.getString(NUM_RSS_LINK);
    String description = cursor.getString(NUM_RSS_DESCRIPTION);
    new RssData (id, rssName, title, link, description);}
    while (cursor.moveToNext());
    return null;
}

使用: -

public RssData select(long id){
    Rssdata rv;
    Cursor cursor = database.query(TABLE_NAME,
        null, 
        RSS_ID + " = ?", 
        new String[]{String.valueOf(id)}, 
        null, null, RSS_ID
    );
    if (cursor.moveToFirst()){
        String rssName = cursor.getString(NUM_RSS_NAME);
        String title = cursor.getString(NUM_RSS_TITLE);
        String link = cursor.getString(NUM_RSS_LINK);
        String description = cursor.getString(NUM_RSS_DESCRIPTION);
    }
    cursor.close(); //<<<< Should always close Cursors when done with them.
    return rv;
}

但是,如果没有找到行,那么它仍将返回null,因此您应该相应地编码以返回null,例如: -

    this.listResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            dbResult.read(RssActivity.this);
            RssData rssData = dbResult.select(id);
            if (rssData == null) {
                Toast.maketext(getBaseContext(),"Ooops!!! no such row in DB",Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(getBaseContext(), valueOf(id), Toast.LENGTH_SHORT).show();

以上假设适配器是CursorAdapter或自定义适配器,它正确地将 id 传递给onItemClick方法。如果适配器不是这样的适配器,那么<​​strong> id 可能不是相应行的 id ,而是它将是所选项目的位置。