首页 > 解决方案 > SQLite db 无法识别 ContentValue android studio 的值

问题描述

每当我尝试从图库中添加图像并将其存储在 sqlite 数据库中时,它都会显示以下错误:
unrecognized token: "[B@4a9a0f, 'ثعلب', 'Animals')" (code 1): , while compiling: Insert Into Images (Images, Name, Category) Values (null=[B@4a9a0f, 'ثعلب', 'Animals')

我想将图像存储为 Blob。

这是完整的代码:

       `if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
        Uri imageUri = data.getData();
        try {
            bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        tst.setImageBitmap(bmp);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG,100,bos);
        bte=bos.toByteArray();
        cv.put(null,bte);
    }

}

public void AddWord(View view) {
    editword = edtwrd.getText().toString();
    editcategory = edtcat.getText().toString();
    //db.insertword(editword, editcategory);
    db.insertImage(cv,editword,editcategory);
    Toast.makeText(getApplicationContext(), "Data is added successfully", Toast.LENGTH_LONG).show();
}`

为什么 ContentValue 将名称标签添加到 byte[] 值以及如何从 byte[] 值中删除 null=[

标签: androidandroid-studioandroid-sqlite

解决方案


您的问题是您通过使用提供null作为列名cv.put(null,bte);

所以你应该使用cv.put("Images",bte);

但是,我怀疑您随后也没有按预期使用 ContentValues,而是尝试构建 SQL,而不是使用方便的SQLiteDatabase insert方法,该方法处理 ContentValues 并代表您构建 SQL。该示例使用此方法。

这是一个非常简单的插入示例(图像为硬编码字节[]):-

public class MainActivity extends AppCompatActivity {

    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = (new MyDatabase(this)).getWritableDatabase();

        byte[] bte = new byte[]{0,1,2,3,4,5,6,7};
        insertImage(bte,"ثعلب","Animals");
        insertImageWrong(bte,"ثعلب","Animals");
    }

    long insertImage(byte[] image, String name, String category) {
        ContentValues cv = new ContentValues();
        cv.put("Images",image);
        cv.put("Name",name);
        cv.put("Category",category);
        return db.insert("Images" /* The Table Name */, null, cv /* The content values with the column name value pairs */);
    }

    long insertImageWrong(byte[] image, String name, String category) {
        ContentValues cv = new ContentValues();
        cv.put(null,image);
        cv.put("Name",name);
        cv.put("Category",category);
        return db.insert("Images",null,cv);
    }
}

第一个按预期工作,第二个(insertImageWrong)失败:-

2021-07-04 07:19:55.099 9266-9266/a.a.so68239336nullcontentvalues E/SQLiteLog: (1) near "null": syntax error
2021-07-04 07:19:55.101 9266-9266/a.a.so68239336nullcontentvalues E/SQLiteDatabase: Error inserting null=[B@be55dc1 Category=Animals Name=ثعلب
    android.database.sqlite.SQLiteException: near "null": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO Images(null,Category,Name) VALUES (?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1562)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
        at a.a.so68239336nullcontentvalues.MainActivity.insertImageWrong(MainActivity.java:37)
        at a.a.so68239336nullcontentvalues.MainActivity.onCreate(MainActivity.java:21)

以下(来自 Android Studio 的 Database Inspector 的快照)显示第一次尝试将该行插入到数据库中:-

在此处输入图像描述

我建议您像上面那样调整insertImage方法并使用它。

警告

尝试将图像存储在数据库中可能会出现问题。如果图像很小(100-200kb),那很好。但是,如果更大,您可能会遇到速度问题。如果图像大于 1Mb,那么您可能会遇到崩溃。如果任何图像大于 4Mb,那么您无疑会遇到崩溃,除非您将图像分割成块。


推荐阅读