2016-01-21 72 views
0

在我的项目中,我正确使用库https://www.zetetic.net/sqlcipher/。现在我需要获取数据库的名称。在经典SQLite中,有一个方法getDatabaseName()而不是密码似乎没有它,我该怎么办?你有好主意吗?Android sqlcipher getDatabaseName()

enter image description here

import net.sqlcipher.database.SQLiteDatabase; 
import net.sqlcipher.database.SQLiteOpenHelper; 

    public class DbCrypt extends SQLiteOpenHelper { 

private static final String DB_NAME = "data.db"; 
private static final int DB_VERSION = 1; 

private static SQLiteDatabase db = null; 


public DbCrypt(Context context) { 
    super(context, DB_NAME, null, DB_VERSION); 

} 
@Override 
public void onCreate(SQLiteDatabase db) { 
    DbCrypt.db = db; 
... 
... 
} 


@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
} 

编辑 enter image description here

回答

0

在你的SQLiteOpenHelper SQLCipher版的子类,实现getDatabaseName()方法,返回你的数据库名称。你也可以在构造函数中使用它,这样你就不必再重复数据库名称值:

import net.sqlcipher.database.SQLiteDatabase; 
import net.sqlcipher.database.SQLiteOpenHelper; 

    public class DbCrypt extends SQLiteOpenHelper { 

private static final String DB_NAME = "data.db"; 
private static final int DB_VERSION = 1; 

private static SQLiteDatabase db = null; 


public DbCrypt(Context context) { 
    super(context, getDatabaseName(), null, DB_VERSION); 

} 
@Override 
public void onCreate(SQLiteDatabase db) { 
    DbCrypt.db = db; 
... 
... 
} 


@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
} 

public String getDatabaseName() { 
    return(DB_NAME); 
} 
+0

我编辑我的职务 – user2847219

+0

我已经尝试插入方法getDatabaseName(),但我仍然有同样的错误,该方法未被调用。查看新图片,我编辑了我的帖子。 – user2847219

+0

@ user2847219:您需要在'get()'上更改您的方法签名以获取'DbCrypt'对象,而不是'SQLiteOpenHelper'类。 – CommonsWare