带有sqlite数据库

时间:2016-12-16 13:28:08

标签: android sqlite android-sqlite

我正在开发一个Android应用程序,其中我必须使用sqlite数据库。我已经实现了几个代码但没有工作。所以,请给我一个基于Sqlite的示例应用程序的示例代码。

1 个答案:

答案 0 :(得分:1)

I am explaining step by step follow this step.

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

上方时隐藏网址

步骤2:创建新的数据库文件。

步骤3:将此文件复制到android studio中的assets文件夹。

步骤4:在使用任何数据库操作之前,将此代码写入您的启动活动或任何活动。

 public void copyAssets() {

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

        } else
        {
            f.mkdir();
        }


        String dbDir = Environment.getExternalStorageDirectory() + "/" + 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);
        }
    }

//创建一个活动并编写以下代码以将数据插入表中。

 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()+"/"+getResources().getString(R.string.folderName);
            SQLiteDatabase mydb=(getActivity().openOrCreateDatabase(dbDir + "/" + 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);
        }


    }

//将此内容写入string.xml  注意:LimaDatabase是您在SQLite管理器中创建的数据库的名称

<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" />