繁体   English   中英

如何在SQLite Android中存储.txt文件

[英]How to store the .txt file in SQLite Android

我创建了读写文件的应用程序。 当我单击按钮时,我的文件被写入,并且在单击按钮时也读取了该文件。我想在写入文件时将该文件保存到SQLite中。我正在网上冲浪很多,但是可以找不到合适的来源或想法。将文件与输入String(如“ code”)进行比较。 如果这个input_String与存储在SQLite数据库中的文件相匹配,如果匹配的用户无法继续进行进一步的处理,我想知道是否有任何人可以给我一些建议,我希望其他人们也觉得这很有帮助。这是我的代码。感谢Advanced。

活动课

public class Write extends Activity {
    /** Called when the activity is first created. */
    EditText myText;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.abc);
        myText = (EditText) findViewById(R.id.myText);
        Button createButton = (Button) findViewById(R.id.btnCreate);
        Button readButton = (Button) findViewById(R.id.btnRead);
        createButton.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {

                createFile(myText.getText().toString());
                myText.setText("");
                readFile();
            }
        });


    }

    private void createFile(String Text)
    {

        FileOutputStream fos = null;
        try
        {
            fos = openFileOutput("mynote.txt", MODE_PRIVATE);
            fos.write(Text.getBytes());
            Toast.makeText(getApplicationContext(), "File created succesfully",
                    Toast.LENGTH_SHORT).show();
        } 
        catch (FileNotFoundException e) 
        {
            Log.e("CreateFile", e.getLocalizedMessage());
        } 
        catch (IOException e) 
        {
            Log.e("CreateFile", e.getLocalizedMessage());
        }

        finally
        {
            if (fos != null) 
            {
                try 
                {
                    // drain the stream
                    fos.flush();
                    fos.close();
                }
                catch (IOException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }






And the DataBase_Adapter code 

//Table Name
    public static final String TABLE_NAME_CODE="code_table";

  //Colum,n Names
    public static final String KEY_CODE_ID="ID";
    public static final String KEY_CODE_NAME="USERNAME";


  //Table Create Statement 
    public static final String DATABASE_CREATE_CODE = "CREATE TABLE "+TABLE_NAME_CODE+" ("+KEY_CODE_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+KEY_CODE_NAME+"TEXT)";

    //Insert Code in Database code_table
    public void saveCode(String strKey_Code)
    {
       ContentValues newValues = new ContentValues();
        // Assign values for each row.
        newValues.put(KEY_CODE_NAME , strKey_Code);


        // Insert the row into your table
        db.insert(TABLE_NAME_CODE, null, newValues);

    }

尝试将txt文件转换为byte [],然后将其保存在blob data sqlite中。

InputStream is = new FileInputStream("txt file path"); 
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
while ((int bytesRead = is.read(b)) != -1) {
   bos.write(b, 0, bytesRead);
}
byte[] bytes = bos.toByteArray();
byte[] dataToSave = Base64.encode(bytes, Base64.DEFAULT);

保存此dataToSave byte []到sqlite DB的blob数据列。

希望此链接对您有所帮助

您采用的方法可能会有所改进,考虑只将文件名存储在数据库中,您已经保存了文件,对吗? 为什么要再次将其内容存储在数据库中?

暂无
暂无

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

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