[Solved] Android app with sqlite database [closed]


I am explaining step by step follow this step.

Step 1: Download SQLite browser from http://sqlitebrowser.org/

Step 2: Create a new Database file.

Step 3: Copy this file to your assets folder in android studio.

Step 4: write this code to your splash activity or any activity before using any database operation.

 public void copyAssets() {

        File f = new File(Environment.getExternalStorageDirectory() + "https://stackoverflow.com/" + getResources().getString(R.string.folderName));
        Log.d("Home", "copyAssets: " + f.exists());
        if (f.exists()) {

        } else
        {
            f.mkdir();
        }


        String dbDir = Environment.getExternalStorageDirectory() + "https://stackoverflow.com/" + getResources().getString(R.string.folderName);
        AssetManager assetManager = getAssets();

        InputStream in = null;
        OutputStream out = null;
        String filename = getResources().getString(R.string.dbName);
        try {
            in = assetManager.open(filename);
            File outFile = new File(dbDir, filename);
            out = new FileOutputStream(outFile);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
            Log.e("tag", "copied asset file: " + filename);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }

    }

    public void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }

// Create an activity and write following code for insert data into table.

 class Add_product_internal extends AsyncTask<String, Void, Integer>
    {

        @Override
        protected void onPreExecute()
        {

            super.onPreExecute();
        }
        @Override
        protected Integer doInBackground(String... params) {
            // TODO Auto-generated method stub

            String dbDir= Environment.getExternalStorageDirectory()+"https://stackoverflow.com/"+getResources().getString(R.string.folderName);
            SQLiteDatabase mydb=(getActivity().openOrCreateDatabase(dbDir + "https://stackoverflow.com/" + getResources().getString(R.string.dbName), Context.MODE_PRIVATE, null));

            try {

                    ContentValues values = new ContentValues();
                    values.put("column name","value");    // column name should be same as column created in table.
                    mydb.insert("products", null, values);  // Here  products is the table name.
             }
 catch (Exception e) {
                e.printStackTrace();
                // TODO: handle exception
            }
            return null;
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub

              super.onPostExecute(result);
        }


    }

// write this into string.xml
Note: LimaDatabase is the name of database which you made in SQLite manager

<string name="folderName">LimaApp</string><string name="dbName">LimaDatabase</string>   


// Give Permission in AndroidManifext.xml

     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2

solved Android app with sqlite database [closed]