SQLite Android-MODE_WORLD_WRITEABLE无法解析为变量 [英] SQLite Android - MODE_WORLD_WRITEABLE cannot be resolved to a variable

查看:116
本文介绍了SQLite Android-MODE_WORLD_WRITEABLE无法解析为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是一个音板应用程序,但是类别中有很多声音。每个类别在不同的活动中运行。因此,当用户选择类别时,第一个活动将调用其他活动。

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.

该应用程序具有200多种声音,我开始制作一个SQLite数据库,以便用户可以选择

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.

当我将sqlite代码放在另一个类中的方法中时,它已经可以工作了,但是代码在活动中(如果要更改某些内容,将有20个类别,这将使我的工作更加轻松),它为我提供了 MODE_WORLD_WRITEABLE 的无法解析为变量。

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.

这里是方法:

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
    }
}

我尝试使用(String titulo,int id,Context cont)和 cont.CODE_WORLD_WRITEABLE ,但是随后出现错误,如 openOrCreateDatabase没有为banco类型定义(banco是类名)

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).

如何使用 openOrCreateDatabase 在另一个不是活动的类中?

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

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

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

zrgiu解决了这个问题,下面是我在banco类中的代码:

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

public class banco {

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();
    }
}

}

在活动中我使用:

banco.cria(somestring,someint,this);

banco.cria(somestring,someint, this);

再次感谢您,zrgiu。

Again, thank you zrgiu.

推荐答案

您将需要上下文来创建/打开本地化到您应用程序的数据库。对您来说,最简单的方法就是将上下文实例传递给函数或类,以确保您不泄漏它。

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.

另一种解决方案是将sqlite数据库存储在外部存储中,该存储不需要Context,但还可以将数据库公开给所有其他应用和用户。

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.

这篇关于SQLite Android-MODE_WORLD_WRITEABLE无法解析为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆