Android:sqlite只保存一个项目

时间:2016-05-11 06:10:53

标签: java android sqlite

我试图保存一份书籍清单但是当我读到这张桌子时,我只买了一本书。

DatabaseHelper.java

    ...

 @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(
                "CREATE TABLE IF NOT EXISTS "+ TABLE_NAME +" (id TEXT PRIMARY KEY,title TEXT, description TEXT)"
        );
    }

public boolean insertBook  (String id, String title, String publisher, String format,
                                String description, String creator, String category, String language, String date)
    {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("id", id);
        contentValues.put("title", title);
        contentValues.put("description", description);
        db.insert(TABLE_NAME, null, contentValues);
        db.close();
        return true;
    }

public List<Book> getBooks()
    {
        Book book = new Book();
        List<Book> bookList = new ArrayList<Book>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor=  db.rawQuery( "select * from tblbooks", null );

        if (cursor.moveToLast()) {
            do {
                book.setId(cursor.getString(cursor.getColumnIndex("id")));
                book.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                book.setDescription(cursor.getString(cursor.getColumnIndex("description")));

                bookList.add(book);

            } while (cursor.moveToNext());
        }
        return bookList;
    }
...

MainActivity.java

在这里,我有一个for循环可以改变书中的值,在这个循环中我已经把这个代码放到了每本新书中,这本书将被保存:

...
//Save books localy
dbHelper.insertBook(    //the value of book changes, I've tested it so this is not the problem
  book.getId(),
  book.getTitle(),
  book.getDescription()
);

之后我使用这段代码从数据库中获取所有书籍,这是我只得到一本书...

...

//Get bookshelf of local database
    for (int i = 0; i < dbHelper.getBooks().size(); i++) {
          bookList.add(dbHelper.getBooks().get(i)); <-- should put all books in this list.
     }

    ...

输出总是只有1本书......

5 个答案:

答案 0 :(得分:2)

你应该移动

 Book book = new Book();

内循环

    if (cursor.moveToFirst()) {
        do {
            Book book = new Book();//here

            book.setId(cursor.getString(cursor.getColumnIndex("id")));
            book.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            book.setDescription(cursor.getString(cursor.getColumnIndex("description")));

            bookList.add(book);

        } while (cursor.moveToNext());
    }

并检查cursor.moveToFirst();

答案 1 :(得分:1)

你可以这样做:

cursor.moveToLast()更改为cursor.moveToFirst()并始终在循环内创建新的 Book 对象。

原因:您要求光标获取最后一项

答案 2 :(得分:1)

试试这个,

替换

cursor.moveToLast()到cursor.moveToFirst()

答案 3 :(得分:1)

您的代码中存在两个问题。

  1. 您需要在循环内创建Book对象。

  2. 首先将光标移动到第一个位置&amp;然后获取记录。

  3.   

    修改后的代码: -

    public List<Book> getBooks()
        {
            List<Book> bookList = new ArrayList<Book>();
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor=  db.rawQuery( "select * from tblbooks", null );
    
           if (cursor.moveToFirst()) {  // change here
                        do {
           Book book = new Book();  // need to add here
    
        book.setId(cursor.getString(cursor.getColumnIndex("id")));
                            book.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                            book.setDescription(cursor.getString(cursor.getColumnIndex("description")));
    
                            bookList.add(book);
    
                        } while (cursor.moveToNext());
            cursor.close();
                    }
            }
            return bookList;
        }
    

答案 4 :(得分:0)

您需要修改两个地方

  1. 移动Book book = new Book();内圈;
  2. cursor.moveToLast()更改为cursor.moveToFirst()
  3. 像这样:

    public List<Book> getBooks() {
            List<Book> bookList = new ArrayList();
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor=  db.rawQuery( "select * from "+TABLE_NAME, null);
            if (cursor.moveToFirst()) {
                do {
                    Book book = new Book();
                    book.setId(cursor.getString(cursor.getColumnIndex("id")));
                    book.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                    book.setDescription(cursor.getString(cursor.getColumnIndex("description")));
                    bookList.add(book);
                } while (cursor.moveToNext());
            }
            return bookList;
        }
    

    顺便说一句,你不应该得到像这样的所有书籍

    for (int i = 0; i < dbHelper.getBooks().size(); i++)
    

    但是应该喜欢这个

    List<Book> bookList = dbHelper.getBooks();
    for(int i = 0, count = bookList.size(); i < count; i++){
       ...
    }