Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有byte[]WHERE子句的SQLite查询_Java_Android_Sql_Blob - Fatal编程技术网

Java 带有byte[]WHERE子句的SQLite查询

Java 带有byte[]WHERE子句的SQLite查询,java,android,sql,blob,Java,Android,Sql,Blob,从Android SQLite数据库的角度来看,我有一个表,其中有一个BLOB类型的字段,然后我想根据这个BLOB字段查询这个表的内容 我可以使用ContentValues插入我的BLOB字段,并使用以下方法检索它: cursor.getBlob(0)// index 我就是不知道如何基于这个BLOB字段查询这个表的内容,也没有发现关于这个问题的任何信息。你不能查询BLOB的(文本?二进制?其他?)内容 如果您查看,您将看到内容是十六进制的: 示例:X'53514C697465' 建议: 创建

从Android SQLite数据库的角度来看,我有一个表,其中有一个BLOB类型的字段,然后我想根据这个BLOB字段查询这个表的内容

我可以使用
ContentValues
插入我的BLOB字段,并使用以下方法检索它:

cursor.getBlob(0)// index
我就是不知道如何基于这个BLOB字段查询这个表的内容,也没有发现关于这个问题的任何信息。

你不能查询BLOB的(文本?二进制?其他?)内容

如果您查看,您将看到内容是十六进制的:

示例:X'53514C697465'

建议:

创建一个新的文本列,例如“blob_索引”。您可以在“index”列上搜索,然后获取blob


或者,只需将数据存储为“文本”。

我发现您可以查询blob。需要对查询使用hex()函数

例如,我在数据库行中使用UUID作为唯一密钥,我可以在本地生成该密钥,并且仍然可以确保服务器上的唯一性

插入数据时,以下操作有效:

给定以下形式的查询URI:

查询变成:

UuidFactory
(其中还包含生成新uuid的代码)中,定义了以下静态函数:

为了完整性:

CREATE TABLE example (_ID INTEGER PRIMARY KEY AUTOINCREMENT,
                      uuid BLOB NON NULL UNIQUE,
                      ...)
final ContentValues values = new ContentValues(4);
values.put(Contract.Line.COL_UUID,
           UuidFactory.toBlob(uuid));
content://package.example.com/example/uuid/11112222-3333-0444-0555-666677778888
final SQLiteDatabase db = mHelper.getReadableDatabase();
return db.query(table, projection,
                "hex(uuid) = ?",
                new String[] { UuidFactory.toHex(uri.getLastPathSegment()) },
                null, null, null, null);
@NonNull
public static String toHex(@NonNull final UUID uuid) {
    return String.format("%016X%016X",
                        uuid.getMostSignificantBits(),
                        uuid.getLeastSignificantBits());
}

@NonNull
public static String toHex(@NonNull final String uuid) {
    return toHex(UUID.fromString(uuid));
}

@NonNull
public static byte[] toBlob(@NonNull final UUID uuid) {
    final ByteBuffer buf = ByteBuffer.allocate(16);
    buf.putLong(uuid.getMostSignificantBits());
    buf.putLong(uuid.getLeastSignificantBits());
    return buf.array();
}
@NonNull
public static UUID fromBlob(@NonNull final byte[] array) {
    final ByteBuffer buf = ByteBuffer.allocate(16);
    buf.mark();
    buf.put(array);
    buf.reset();
    final long msb = buf.getLong();
    final long lsb = buf.getLong();
    return new UUID(msb, lsb);
}