【问题标题】:Does SQLCipher allow to access Database by other app.(if DB in SD-CARD)SQLCipher 是否允许其他应用程序访问数据库。(如果数据库在 SD-CARD 中)
【发布时间】:2013-01-14 06:07:51
【问题描述】:

我正在 SQLCiper 中尝试一件事。

这东西在SD-CARD的SQLite数据库中是成功的

1- 创建了一个应用程序firstApp。数据库在SD-CARD

2- 创建了第二个应用secondApp。

我正在尝试在我的第二个应用程序中从 SD-CARD 读取数据。

编辑:- 我在 SDCARD 中的数据库。

public class SdcardCipherDataBase extends SQLiteOpenHelper
{

    public static final String  DATABASE_FILE_PATH = "/sdcard";
    public static final String  DATABASE_NAME = "sdCipherDatabase";
    public final static String NAME ="name";
    public final static String ADDRESS ="address";
    public final static String CITY ="city";

    public SdcardCipherDataBase(final Context context) {
        super(context,Environment.getExternalStorageDirectory()+File.separator+DATABASE_FILE_PATH + File.separator + DATABASE_NAME, null, 1);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        try
        {  
            db.execSQL( "CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, address TEXT,city TEXT);");

        }

        catch (SQLiteException ex)
        {
            Log.e("Hello", "error -- " + ex.getMessage(), ex);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP Table mylistdata");
        onCreate(db);
    }
}

我在同一个应用程序中使用这个数据库:-

public void databasCollection()
    {
        SQLiteDatabase.loadLibs(this);
        File file = getDatabasePath("/mnt/sdcard/sdcard/sdCipherDatabase");
        file.mkdir();
        file.delete();
        db = SQLiteDatabase.openOrCreateDatabase(file, "myapp", null);
        sdb = new SdcardCipherDataBase(this);

        sdb.getWritableDatabase("myapp");
        ContentValues c = new ContentValues();
        c.put(SdcardCipherDataBase.NAME,"monty");
        c.put(SdcardCipherDataBase.ADDRESS,"BTM");
        c.put(SdcardCipherDataBase.CITY,"Bangalore");
        db.insert("information", SdcardCipherDataBase.NAME, c);
        db.close();
    }

但我也想在其他应用程序中使用相同的数据库。

我正在尝试做这样的事情

public void databaseFromOtherApp()
{
    SQLiteDatabase.loadLibs(this);
    File file = getDatabasePath("/mnt/sdcard/sdcard/sdCipherDatabase");
    db = SQLiteDatabase.openOrCreateDatabase(file, "myapp", null);
    dh = new DatabaseHelper(this);

    Cursor cursor = db.rawQuery("select * from information", null);
    cursor.moveToFirst();
    String s = cursor.getString(cursor.getColumnIndex(NAME));
    Toast.makeText(this,"Name is "
            +s, Toast.LENGTH_LONG).show();

    cursor.close();
    db.close();
}

但显示错误。

 Caused by: net.sqlcipher.database.SQLiteException: no such table: information: , while compiling: select * from information

【问题讨论】:

    标签: android sqlcipher


    【解决方案1】:

    您的第二个应用程序中的这一行是错误的:

    dh = new DatabaseHelper(this);
    

    您的第二个应用程序中也需要一个SdcardCipherDataBase 的实例,因为该类知道表和表名。目前您使用的是一个普通的DatabaseHelper,它不知道要查找什么。

    更新:
    即使您可以做您需要的事情,这也不是明智的做法。如您所见,您需要将所有与数据库相关的逻辑复制到使用它的任何其他应用程序。如果您需要第三个应用程序来做同样的事情怎么办?再次复制所有课程?如果您需要修改表以添加新列怎么办?您必须修改所有三个应用程序中的类。通常,您会希望将数据库访问层保留在第一个应用程序中,并通过内容提供程序提供对其他应用程序的访问。

    【讨论】:

    • 我怎样才能把那个类的实例导致它在其他应用程序中。
    • 我应该使用内容提供者。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 2018-05-22
    • 2018-03-31
    • 2017-10-29
    • 1970-01-01
    • 2015-02-18
    相关资源
    最近更新 更多