3

How can I execute a *.sql script on android programmatically? The script is located at /assert folder. My phone is not rooted.

0

1 Answer 1

6
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);
Sign up to request clarification or add additional context in comments.

2 Comments

This just executes the first sql statment. From documentation of execSQL, "Execute a single SQL statement that does not return any data."
Not only that, but it explicitly states it now SQLiteDatabase.execSQL(sql): "@param sql the SQL statement to be executed. Multiple statements separated by semicolons are not supported."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.