Search code examples
androidandroid-studiosql-scripts

how to use SQL Script in android?


enter image description here there are many thing in android studio we don't know or don't use its features ,how to user this feature? sql script


Solution

  • Finally i got ans
    The script is located at assert folder -scripts developed by SQL Developer

    InputStream is = getResources().getAssets().open("SQLScript.sql");
    String sql= convertStreamToString(is);
    
    public static String convertStreamToString(InputStream is)
                throws IOException {
                Writer writer = new StringWriter();
            char[] buffer = new char[2048];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is,
                        "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            String text = writer.toString();
            return text;
    }
    
    
    SQLiteDatabase db;
    db = openOrCreateDatabase("MyDatabase.db", SQLiteDatabase.CREATE_IF_NECESSARY, null );
    db.execSQL(sql);