Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SQLiteOpenHelper无法打开数据库文件_Android_File_Sqlite - Fatal编程技术网

Android SQLiteOpenHelper无法打开数据库文件

Android SQLiteOpenHelper无法打开数据库文件,android,file,sqlite,Android,File,Sqlite,我知道这个问题以前讨论过。 但是我找不到解决问题的办法,所以我要再问一次。 我在创建数据库时遇到问题。 我复制了SQLiteOpenHelper的实现: public class DBCreator extends SQLiteOpenHelper { private static final String DATABASE_NAME = "servision.db"; private static final int DATABASE_VERSION = 1; p

我知道这个问题以前讨论过。 但是我找不到解决问题的办法,所以我要再问一次。 我在创建数据库时遇到问题。 我复制了SQLiteOpenHelper的实现:

public class DBCreator extends SQLiteOpenHelper 
{
    private static final String DATABASE_NAME = "servision.db";
    private static final int DATABASE_VERSION = 1;


    private static final String INFO_TABLE ="create table INFO(key text,value text);";
    public static final String GATEWAYS_TABLE = "GATEWAYS";

    // Table name
    public static final String TABLE = "events";

    // Columns
    public static final String TIME = "time";
    public static final String TITLE = "title";

    public DBCreator(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        try
        {
            String gatewaytablescript ="create table" +GATEWAYS_TABLE+"(" +
            "id int primary key, " +
            "host text not null," +
            "port int not null," +
            "username text," +
            "password text,"+
            "useproxy int," +
            "proxyHost text," +
            "proxyPort port," +
            "desc text," +
            "secondaryip text," +
            "secondaryport int," +
            "timezone int," +
            "encryption int," +
            "customkey text" +
            ");";

            Log.d("DBCreator", "Creating " + GATEWAYS_TABLE + "table");
            db.execSQL(gatewaytablescript);

            String infotablescript ="create table " + INFO_TABLE + "(key text,value text);";
            Log.d("DBCreator", "Creating " + INFO_TABLE + "table");
            db.execSQL(infotablescript);

        }
        catch(SQLException ex)
        {
            Log.d("DBCreator", "onCreate exception " +ex.getMessage()); 
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        Log.d("EventsData", "onUpgrade");
        if (oldVersion >= newVersion)
            return;


    }
}
我从主活动onCreate方法创建它,如下所示

DBCreator db =new DBCreator(this);
db.getReadableDatabase();
在logcat中,我收到以下信息:

06-01 17:31:15.523: INFO/global(2470): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
06-01 17:31:15.652: INFO/Database(4160): sqlite returned: error code = 14, msg = cannot open file at source line 25467
06-01 17:31:15.652: ERROR/Database(4160): sqlite3_open_v2("/data/data/com.servision.svclient/databases/servision.db", &handle, 6, NULL) failed
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): Couldn't open servision.db for writing (will try read-only):
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): android.database.sqlite.SQLiteException: unable to open database file
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1921)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:883)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:960)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:953)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:602)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at com.servision.svclient.Main.onCreate(Main.java:52)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.os.Looper.loop(Looper.java:123)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at java.lang.reflect.Method.invokeNative(Native Method)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at java.lang.reflect.Method.invoke(Method.java:521)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at dalvik.system.NativeStart.main(Native Method)
06-01 17:31:15.738: INFO/Database(4160): sqlite returned: error code = 14, msg = cannot open file at source line 25467
06-01 17:31:15.738: ERROR/Database(4160): sqlite3_open_v2("/data/data/com.servision.svclient/databases/servision.db", &handle, 1, NULL) failed
06-01 17:31:15.523:INFO/global(2470):BufferedInputStream构造函数中使用的默认缓冲区大小。如果需要8k缓冲区,最好是显式的。
06-01 17:31:15.652:INFO/Database(4160):sqlite返回:错误代码=14,消息=无法打开源代码行25467处的文件
06-01 17:31:15.652:错误/数据库(4160):sqlite3_open_v2(“/data/data/com.servision.svclient/databases/servision.db”,&handle,6,NULL)失败
06-01 17:31:15.722:错误/SQLiteOpenHelper(4160):无法打开servision.db进行写入(将尝试只读):
06-01 17:31:15.722:错误/SQLiteOpenHelper(4160):android.database.sqlite.SQLiteException:无法打开数据库文件
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.database.sqlite.SQLiteDatabase.dbopen(本机方法)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1921)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:883)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:960)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:953)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:602)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.database.sqlite.SQLiteOpenHelper.getwriteabledatabase(SQLiteOpenHelper.java:98)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于com.servision.svclient.Main.onCreate(Main.java:52)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.os.Handler.dispatchMessage(Handler.java:99)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.os.Looper.loop(Looper.java:123)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于android.app.ActivityThread.main(ActivityThread.java:4627)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于java.lang.reflect.Method.invokenactive(本机方法)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于java.lang.reflect.Method.invoke(Method.java:521)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-01 17:31:15.722:ERROR/SQLiteOpenHelper(4160):位于dalvik.system.NativeStart.main(本机方法)
06-01 17:31:15.738:信息/数据库(4160):sqlite返回:错误代码=14,消息=无法打开源代码行25467处的文件
06-01 17:31:15.738:错误/数据库(4160):sqlite3_open_v2(“/data/data/com.servision.svclient/databases/servision.db”,&handle,1,NULL)失败
从不调用onCreate of DBCreator。 我正在从Eclipse以调试模式在Galaxy S上运行程序。 我是否缺少一些权限?
可能是上下文不好或其他原因?

关于sqlite返回的
error code=14,msg=cannot open file at source line 25467
sqlite3\u open\u v2(“/data/data/com.servision.svclient/databases/servision.db”,&handle,1,NULL)失败
错误,这似乎是由于没有数据库而Android试图创建数据库时造成的

有些人在这里读书


此链接中已出现错误

使用以下api

openOrCreateDatabase(“sample.db”,MODE_PRIVATE,null)

试试这个链接


检查清单中的权限 加


我的应用程序出现问题,在重新安装应用程序后,用旧数据库更改数据库文件会导致该错误。 我猜问题在于旧DB文件的所有者与重新安装的应用程序的文件所有者不同。因此,解决方案是授予新文件完全权限(777),以便其他用户也可以读取该文件


最后,请确保您没有文件权限问题。

我也遇到过类似的问题,下面是我解决问题的方法:

总而言之:

adb shell
cd /data/data/YOUR.APP.PACKAGE/databases/
ls
这将为您提供应用程序使用的数据库列表

cp your_database.db your_database.db_backup
rm your_database.db

当我得到这个错误时,我将这个代码用于我的构造函数

public DBClass(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    context.openOrCreateDatabase(DATABASE_NAME, context.MODE_PRIVATE, null);
}

能否尝试从数据库名称中删除*.db文件扩展名(将数据库名称从servision.db更改为servision)?嗨,Daniel,谢谢您的快速回答。不,更改名称没有任何帮助。您是否手动检查了读/写数据库的权限?我遇到了一些奇怪的市场崩溃,报告说在一些罕见的情况下,我的应用程序无法打开数据库进行写作。我保护我
public DBClass(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    context.openOrCreateDatabase(DATABASE_NAME, context.MODE_PRIVATE, null);
}