简体   繁体   English

将文本文件作为Blob数据插入SQLite数据库

[英]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. 我在sqlite中使用表USER_DETAILS(USER_ID varchar,USER_NAME varchar,USER_INFO blob)创建了一个db。我尝试创建一个文件,将其转换为byteArray并将其作为blob存储在表中.db和表已创建但未创建价值。 Here is my code .Can somebody pl guide on how to insert the blob data into the table? 这是我的代码。有人可以指导如何将Blob数据插入表吗?

create file :`String fileNameDb = fileName + mTextDocNo++; 创建文件:`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方法:

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 当我尝试创建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.674:WARN / System.err(613):java.io.IOException:设备03-14 17:07:07.694:WARN / System.err(613):组织中没有剩余空间。 apache.harmony.luni.platform.OSFileSystem.write(本机方法)

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):在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.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 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) 03月14日17:07:07.715:WARN / System.err(613):位于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 ) 您正在打开文件输出流,但变量名具有误导性( fis
  • You then write an empty array out to this stream, 1024 times 然后,将空数组写出到该流中,执行1024次
  • 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. 然后,您创建一个ObjectOutputStream ,将其链接到FileOutputStream并写出一些随机对象,该对象的定义未在代码示例中显示。
  • 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? mDbHelper方法的第二个参数,您是否真的打算将输入对象与输出文件名连接起来?

Fix all those errors, then edit your question if you still have problems after that. 修复所有这些错误,然后再编辑问题是否仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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