【问题标题】:I can not insert (blob data) in Sqlite Android Application我无法在 Sqlite Android 应用程序中插入(blob 数据)
【发布时间】:2012-07-27 18:23:04
【问题描述】:

我在 sqlite 中设计一个数据库我遵循了这个guide for db 访问具有三个元素(id、文本、blob 数据)的表,bolb 是一个字节数组,但不幸的是,当使用此代码时:

long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null, values);

insertId总是返回-1,那就有一个我看不懂的错误!

表格是这样的:

private static final String DATABASE_CREATE = "create table "
        + TABLE_COMMENTS + "(" + COLUMN_ID
        + " integer primary key autoincrement, " + COLUMN_COMMENT
        + " text not null,"+ MOVE + "blob" + ");";

允许我将文件中的 txt 文件转换为要插入表中的 blob 字节的算法是:( txt --> byte [] array --> blob )

public byte[] ConvertByte () 
{
    byte[] b = new byte[1024];
    int bytesRead =0;
    byte[] dataToSave = null;
    ByteArrayOutputStream bos = null;
    try {
        InputStream is = new FileInputStream(FileName); 
        bos = new ByteArrayOutputStream();
        while ((bytesRead = is.read(b)) != -1) {
          bos.write(b, 0, bytesRead);
        }
      byte[] bytes = bos.toByteArray();
      dataToSave = Base64.encode(bytes, Base64.DEFAULT);
    } catch (IOException e) {
         Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }
 return dataToSave;
}

【问题讨论】:

    标签: android database sqlite


    【解决方案1】:

    您是否考虑过将纯 String 作为 blob 数据插入的选项?

    Base64.encode() 函数有另一个返回 String 值的函数。我还将 blob 数据插入到表中,并且我使用了这种替代方法,它就像一个魅力。

    【讨论】:

      猜你喜欢
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 2016-04-17
      • 2012-03-12
      • 2013-06-18
      相关资源
      最近更新 更多