简体   繁体   中英

Insert a Text file as Blob data into SQLite Database

I created a db in sqlite with a table USER_DETAILS(USER_ID varchar,USER_NAME varchar,USER_INFO blob).I tried creating a file convert it into a byteArray and store it as a blob in the table.The db and the table got created but not the values. Here is my code .Can somebody pl guide on how to insert the blob data into the table?

create file :`String fileNameDb = fileName + mTextDocNo++;

        FileOutputStream fis = (FileOutputStream) openFileOutput(
                fileNameDb, Context.MODE_PRIVATE);
        System.out.println("Create File " + fis);

        byte[] textFileArray = new byte[1024];
        int i = 0;
        while (i < textFileArray.length) {
            fis.write(textFileArray);
        }
        System.out.println("Created the byteArray " + textFileArray);

        ObjectOutputStream oos = new ObjectOutputStream(fis);
        oos.writeChars(inputData);

        System.out.println("Calling the createNoteMethod------------>>>");
        mDbHelper.createNote(fileNameDb, inputData + fileNameDb,
                textFileArray);
        oos.flush();
        oos.close();
        fis.close();

Insert stmt method :

stmt = mDb.compileStatement("INSERT INTO USER_DETAILS VALUES(?,?,?)");
stmt.bindString(1, user_id);
stmt.bindString(2, user_name);
stmt.bindBlob(3, user_info);
stmt.executeInsert();

I get the below exception when i try creating the byteArray

03-14 17:07:07.674: WARN/System.err(613): java.io.IOException: No space left on device 03-14 17:07:07.694: WARN/System.err(613): at org.apache.harmony.luni.platform.OSFileSystem.write(Native Method)

03-14 17:07:07.704: WARN/System.err(613): at dalvik.system.BlockGuard$WrappedFileSystem.write(BlockGuard.java:171)

03-14 17:07:07.704: WARN/System.err(613): at java.io.FileOutputStream.write(FileOutputStream.java:300)

03-14 17:07:07.715: WARN/System.err(613): at java.io.FileOutputStream.write(FileOutputStream.java:256)

03-14 17:07:07.715: WARN/System.err(613): at com.example.FileSharing.FileSharingActivity.createFile(FileSharingActivity.java:51)

You have many problems with your code:

  • You are opening a file output stream but your variable name is misleading ( fis )
  • You then write an empty array out to this stream, 1024 times
  • You then create an ObjectOutputStream , chain it to the FileOutputStream and write out some random object which the definition of is not shown in your code sample.
  • You invoke a method prior to closing your streams, this is a potential bug especially if you are (I wager a guess), reopening the streams in the method and performing operations on them.
  • The second parameter to your mDbHelper method, did you really intend on concatenating your input object with the output file name?

Fix all those errors, then edit your question if you still have problems after that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM