Курсор SQLite не работает для изображений - PullRequest
0 голосов
/ 29 июня 2018

У меня есть этот метод для чтения объекта книги с тем же идентификатором, который я хочу из базы данных, но когда я пытаюсь получить данные из курсора, программа останавливается на столбце изображения. Я не совсем понимаю, почему. Я попытался "getBlob", "getString", а затем преобразовать в байтовый массив, но он просто не хотел работать. Может быть, вы можете посоветовать, что я должен изменить в запросе или, может быть, в классе.

public Book readBook(int id){
            SQLiteDatabase db = this.getWritableDatabase();
            String query = "SELECT  * FROM " + table_BOOKS;
            Blob blob = new Blob;
            Cursor cursor = db.query(table_BOOKS, COLUMNS, " id = ?", new String[]{String.valueOf(id)},null,null,null, null);


        if (cursor != null)
            cursor.moveToFirst();

        Book book = new Book();
        if (cursor != null) {
            book.setId(Integer.parseInt(cursor.getString(0)));
            book.setTitle(cursor.getString(1));
            book.setAuthor(cursor.getString(2));
            book.setGenre(cursor.getString(3));
            book.setDescription(cursor.getString(4));
            book.setImage(cursor.getBlob(5));
        }
        return book;
    }

И это мой класс данных для Book, он довольно простой, а для изображения он идет с байтовым массивом.

public class Book {
    private int id;
    private String title;
    private String author;
    private String genre;
    private String description;
    private byte[] image;

    public Book(){}

    public Book(String title, String author, String genre,String description, byte[] image){
        super();
        this.title = title;
        this.author = author;
        this.genre = genre;
        this.description = description;
        this.image = image;
    }

    public int getId(){
        return  id;
    }

    public void setId(int id){
        this.id = id;
    }

    public String getTitle(){
        return title;
    }

    public void setTitle(String title){
        this.title = title;
    }

    public String getAuthor(){
        return author;
    }

    public void setAuthor(String author){
        this.author = author;
    }

    public String getGenre(){
        return genre;
    }

    public void setGenre(String genre){
        this.genre = genre;
    }

    public String getDescription(){
        return description;
    }

    public void setDescription(String description){
        this.description = description;
    }

    public byte[] getImage(){
        return image;
    }

    public void setImage(byte[] image){

        this.image = image;
    }

}

1 Ответ

0 голосов
/ 29 июня 2018

Я только что изменил запрос, чтобы он выглядел так

String query = "SELECT  * FROM " + table_BOOKS + " WHERE id ='" + id +"'";


        Cursor cursor = db.rawQuery(query,null);

, а затем получить cursor.getBlob () и все заработало

...