簡體   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