【问题标题】:SQLite Cursor doesn't work for imagesSQLite 光标不适用于图像
【发布时间】:2018-06-29 18:48:55
【问题描述】:

我有这个方法可以从数据库中读取具有我想要的确切 ID 的书籍对象, 但是当我尝试从光标程序中获取数据时,程序会在图像列上停止。我不完全明白为什么。我尝试了“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;
    }

}

【问题讨论】:

  • 感谢您的帮助,我刚刚更改了查询
  • 在数据库中存储 BLOB 是一种最糟糕的做法。而是存储文件的路径。
  • “不起作用”并不是任何人都可以帮助解决的问题描述。我看到您已经解决了您的问题,但因此这对其他人来说似乎没有用作为问题或答案。
  • @KlingKlang 你能链接一些例子吗?并感谢您的建议。

标签: database sqlite android-sqlite


【解决方案1】:

我刚刚将查询更改为如下所示

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


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

然后得到 cursor.getBlob() 并且它起作用了

【讨论】:

    猜你喜欢
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 2012-10-28
    • 2017-06-26
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多