【问题标题】:How to store BLOB to the sqlite database如何将 BLOB 存储到 sqlite 数据库
【发布时间】:2014-04-07 03:38:50
【问题描述】:

请帮忙,我是 android 新手,我不懂很多基础知识。 我正在使用此代码将我的 powerpoint 文件存储为 BLOB。但是当我运行它时。即使文件名作为列表,数据库也不会显示任何内容。如何在不使用 DBHELPER 的情况下正确存储 blob。因为我开始不使用 DBHELPER 并且我现在离我的项目已经很远了。只是想通过这个项目。

String filename = FilesInFolder.get(position);
FileInputStream fis = new FileInputStream("/sdcard/" + filename);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            byte[] buffer =new byte[1024];
            int read;
            while ((read = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, read);
            }
            baos.flush();
            byte[] fileByteArray = baos.toByteArray();

            notesDB.execSQL("INSERT INTO " +
                    PPT_TABLE_NAME + " (pptName,pptFile)" + 
                    " Values ('"+filename+"','"+fileByteArray+"');");

这就是我编写数据库的方式

notesDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                PPT_TABLE_NAME + 
                " ( " + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,pptName VARCHAR,pptFile BLOB);");

这就是我尝试将其视为列表的方式

Cursor c = notesDB.rawQuery("SELECT * FROM " +
            PPT_TABLE_NAME, null);



    if (c != null ) {
        if  (c.moveToFirst()) {
            do {
                String pptid = c.getString(c.getColumnIndex(BaseColumns._ID.toString()));
                String pptname = c.getString(c.getColumnIndex("pptName"));

                results.add(pptid + " " + pptname);
            }while (c.moveToNext());
        } 
    }

    this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));

请帮助我还是 android 新手..

【问题讨论】:

    标签: database sqlite storage blob


    【解决方案1】:

    您不能轻易地将字节数组直接插入到字符串中。

    要插入 blob,您应该使用可以处理字节数组的函数,例如 insert method:

    ContentValues cv = new ContentValues();
    cv.put("pptName", filename);
    cv.put("pptFile", fileByteArray);
    notesDB.insert(PPT_TABLE_NAME, null, cv);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-22
      • 2011-02-14
      • 2010-12-15
      • 1970-01-01
      • 2012-07-17
      • 2021-09-18
      • 1970-01-01
      相关资源
      最近更新 更多