SQLite Android - MODE_WORLD_WRITEABLE cannot be re

2019-08-03 08:18发布

问题:

My application is a soundboard app, but there is a lot of sounds in categories. Each category is running in a different activity. So, the first activity call the others when the user select the category.

The app have more than 200 sounds and I started to make a SQLite DB so the user can select the favorite sounds and show them in a separate activity.

It is already working but the code is in the activity, when I put the sqlite code in a method inside another class (if I want to change something it will make my work easier because there is 20 categories) it gave me the 'cannot be resolved to a variable' for MODE_WORLD_WRITEABLE.

Here's the method:

public void cria(String titulo,int id){
    SQLiteDatabase sampleDB = null;

    try {
        sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, CODE_WORLD_WRITEABLE, null);

        sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                SAMPLE_TABLE_NAME +
                " (titulo VARCHAR, id INT);");

        sampleDB.execSQL("INSERT INTO " +
                SAMPLE_TABLE_NAME +
                " Values ('" +
                titulo +
                "'," +
                id +
                ");");

    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    } finally {
        //nothin to do
    }
}

I have tried to use (String titulo,int id, Context cont) and cont.CODE_WORLD_WRITEABLE but then it gives error like openOrCreateDatabase is undefined for the type banco (banco is the class name).

How can I use the openOrCreateDatabase in another class that is not an activity ? If you guys can show me a snippet.

=========EDIT===========

As zrgiu solved the problem, here goes my code in the banco class:

public class banco{

private final static String SAMPLE_DB_NAME = "myFriendsDb";
private final static String SAMPLE_TABLE_NAME = "friends";



public static void cria(String titulo,int id, Context cont){
    SQLiteDatabase sampleDB = null;

    try {
        sampleDB =  cont.openOrCreateDatabase(SAMPLE_DB_NAME, Context.MODE_WORLD_WRITEABLE, null);

        sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                SAMPLE_TABLE_NAME +
                " (titulo VARCHAR, id INT);");

        sampleDB.execSQL("INSERT INTO " +
                SAMPLE_TABLE_NAME +
                " Values ('" +
                titulo +
                "'," +
                id +
                ");");

    } catch (SQLiteException se ) {
        Log.e(cont.getClass().getSimpleName(), "Could not create or Open the database");
    } finally {
        sampleDB.close();
    }
}

}

And in the activity I use:

banco.cria(somestring,someint, this);

Again, thank you zrgiu.

回答1:

You will need a context to create/open databases localized to your apps. The easiest way for you is just to pass a context instance to your function or class, making sure you don't leak it.

The other solution would be to store your sqlite database on the external storage, which doesn't require a Context but also exposes your database to all other apps and the user.