本文介绍了SQLite的Andr​​oid无法打开数据库文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我想这对我的手机和它的作品,谁能告诉我,为什么它不能在模拟器工作?

我想在Android上打开一个数据库,但它抛出无法打开数据库文件异常。在调试,似乎错误发生就行了 MDB = mDbHelper.getWritableDatabase();

我的code是如下:

 包com.track.map;

进口android.content.Context;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;


    公共类DBAdapter {

        受保护的静态最后弦乐DATABASE_NAME =数据;
        受保护的静态最终诠释DATABASE_VERSION = 1;

        //数据库表
        //跟踪表
        受保护的静态最后弦乐TRACK_TABLE =轨道;
        受保护的静态最后弦乐TRACK_ID =_id;
        受保护的静态最后弦乐TRACK_DATE_TIME_START =date_time_start;
        受保护的静态最后弦乐TRACK_DATE_TIME_END =date_time_end;

        //表中创建
        受保护的静态最后弦乐TRACK_TABLE_CREATE =
                                创建表+ TRACK_TABLE +(
                                    + TRACK_ID +integet promary键不为空,
                                    + TRACK_DATE_TIME_START +文字不为空,
                                    + TRACK_DATE_TIME_END +文字NOT NULL);;

        保护DatabaseHelper mDbHelper;
        保护SQLiteDatabase MDB;

        受保护的最终上下文mContext;

        公共DBAdapter(上下文的背景下){
            mContext =背景;
        }

        公共DBAdapter的open()抛出android.database.SQLException {
            mDbHelper =新DatabaseHelper(mContext);
            MDB = mDbHelper.getWritableDatabase();
            回到这一点;
        }

        公共无效的close(){
            mDb.close();
        }

        受保护的静态类DatabaseHelper扩展SQLiteOpenHelper {

            公共DatabaseHelper(上下文的背景下){
                超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
            }

            @覆盖
            公共无效的onCreate(SQLiteDatabase DB){
                db.execSQL(TRACK_TABLE_CREATE);
            }

            @覆盖
            公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
                的onCreate(DB); //现在只是重新创建数据库
            }

        }

    }
 

 包com.track.map;

进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;

公共类MainActivity延伸活动{

    私人DBAdapter分贝;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        DB =新DBAdapter(本);
        db.open();
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }

}
 

有谁知道问题是什么?

编辑:

在logcat的是:

  11月三号至24号:43:42.022:I /数据库(758):源码返回:错误code = 14,味精=无法打开文件的源代码行25467
11月3日至24号:43:42.022:E /数据库(758):sqlite3_open_v2(/数据/数据​​/ com.track.map /数据库/数据,和放大器;手柄,6,NULL)失败
11月3日至24号:43:42.032:D / AndroidRuntime(758):关闭虚拟机
11月3日至24号:43:42.032:W / dalvikvm(758):主题ID = 1:螺纹退出与未捕获的异常(组= 0x4001d800)
11月3日至24号:43:42.062:E / AndroidRuntime(758):致命异常:主要
11月3日至24号:43:42.062:E / AndroidRuntime(758):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.track.map / com.track.map.MainActivity}:android.database.sqlite.SQLiteException :无法打开数据库文件
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.os.Handler.dispatchMessage(Handler.java:99)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.os.Looper.loop(Looper.java:123)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.app.ActivityThread.main(ActivityThread.java:4627)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在java.lang.reflect.Method.invokeNative(本机方法)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在java.lang.reflect.Method.invoke(Method.java:521)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在dalvik.system.NativeStart.main(本机方法)
11月3日至24号:43:42.062:E / AndroidRuntime(758):android.database.sqlite.SQLiteException:无法打开数据库文件所致
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.database.sqlite.SQLiteDatabase.dbopen(本机方法)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.database.sqlite.SQLiteDatabase< INIT>(SQLiteDatabase.java:1812)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在com.track.map.DBAdapter.open(DBAdapter.java:37)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在com.track.map.MainActivity.onCreate(MainActivity.java:17)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11月3日至24号:43:42.062:E / AndroidRuntime(758):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11月3日至24号:43:42.062:E / AndroidRuntime(758):11 ...更多
 

解决方案

这可能有点晚,但希望这有助于为谁得到这个问题(因为我无法找到一个明确的解决办法各地)。

我想我知道了这个原因(至少对我而言)的原因。展望在 DDMS - > 文件管理器,你会认识到,数据库文件夹( /数据/数据​​//数据库/ )不存在,这是为什么应用程序不能创建在于不存在的文件夹中的数据库文件。如果你能以某种方式创建一个数据库文件夹,就可以避免这个问题。

由于我很懒,我只使用了 /数据/数据​​//文件/ 文件夹,当我在模拟器模式。您可以使用此获得的文件目录:

  context.getFilesDir()。getPath()
 

这在模拟器工作美丽的我。

希望这可以帮助别人。

如果你想看到一些code:

 字符串dbFilename =example.db;
尝试
{
    文件databaseFile = getDatabasePath(dbFilename);
    SQLiteDatabase _db = SQLiteDatabase.openOrCreateDatabase(databaseFile);
}赶上(例外五)
{
    串databasePath = getFilesDir()getPath()+/+ dbFilename。
    文件databaseFile =新的文件(databasePath);
    _db = SQLiteDatabase.openOrCreateDatabase(databaseFile);
}
 

编辑:

我尝试登录到Facebook的(我的应用程序有FB集成)的仿真器和/数据库文件出现后(并坚持)。不知道发生了什么,但它可能以某种方式创建该文件夹。我下一次的专家在这里阐明光。

Edit:I tried this on my phone and it works, can anyone tell me why it does not work on an emulator?

I am trying to open a database on android, but it is throwing an "Database file could not be opened" Exception. In the debugger, it seems that the error is occurring on the line mDb = mDbHelper.getWritableDatabase();

My code is as follows:

package com.track.map;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


    public class DBAdapter {

        protected static final String DATABASE_NAME = "data";
        protected static final int DATABASE_VERSION = 1;

        //database tables
        //track table
        protected static final String TRACK_TABLE = "tracks";
        protected static final String TRACK_ID = "_id";
        protected static final String TRACK_DATE_TIME_START = "date_time_start";
        protected static final String TRACK_DATE_TIME_END = "date_time_end";

        //tables create
        protected static final String TRACK_TABLE_CREATE =
                                "create table " + TRACK_TABLE + " ("
                                    + TRACK_ID + " integet promary key not null, "
                                    + TRACK_DATE_TIME_START + " text not null, "
                                    + TRACK_DATE_TIME_END + " text not null);";

        protected DatabaseHelper mDbHelper;
        protected SQLiteDatabase mDb;

        protected final Context mContext;

        public DBAdapter(Context context) {
            mContext = context;
        }

        public DBAdapter open() throws android.database.SQLException {
            mDbHelper = new DatabaseHelper(mContext);
            mDb = mDbHelper.getWritableDatabase();
            return this;
        }

        public void close() {
            mDb.close();
        }

        protected static class DatabaseHelper extends SQLiteOpenHelper {

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

            @Override
            public void onCreate(SQLiteDatabase db) {
                db.execSQL(TRACK_TABLE_CREATE);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                onCreate(db); //for now just recreate the database
            }

        }

    }

and

package com.track.map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    private DBAdapter db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = new DBAdapter(this);
        db.open();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Does anyone know what the problem is?

EDIT:

The logcat is:

03-24 11:43:42.022: I/Database(758): sqlite returned: error code = 14, msg = cannot open file at source line 25467
03-24 11:43:42.022: E/Database(758): sqlite3_open_v2("/data/data/com.track.map/databases/data", &handle, 6, NULL) failed
03-24 11:43:42.032: D/AndroidRuntime(758): Shutting down VM
03-24 11:43:42.032: W/dalvikvm(758): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-24 11:43:42.062: E/AndroidRuntime(758): FATAL EXCEPTION: main
03-24 11:43:42.062: E/AndroidRuntime(758): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.track.map/com.track.map.MainActivity}: android.database.sqlite.SQLiteException: unable to open database file
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.os.Looper.loop(Looper.java:123)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-24 11:43:42.062: E/AndroidRuntime(758):  at java.lang.reflect.Method.invokeNative(Native Method)
03-24 11:43:42.062: E/AndroidRuntime(758):  at java.lang.reflect.Method.invoke(Method.java:521)
03-24 11:43:42.062: E/AndroidRuntime(758):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-24 11:43:42.062: E/AndroidRuntime(758):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-24 11:43:42.062: E/AndroidRuntime(758):  at dalvik.system.NativeStart.main(Native Method)
03-24 11:43:42.062: E/AndroidRuntime(758): Caused by: android.database.sqlite.SQLiteException: unable to open database file
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1812)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
03-24 11:43:42.062: E/AndroidRuntime(758):  at com.track.map.DBAdapter.open(DBAdapter.java:37)
03-24 11:43:42.062: E/AndroidRuntime(758):  at com.track.map.MainActivity.onCreate(MainActivity.java:17)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-24 11:43:42.062: E/AndroidRuntime(758):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-24 11:43:42.062: E/AndroidRuntime(758):  ... 11 more
解决方案

This may be a little late, but hope this helps for whoever gets this problem (since I can't find a definitive solution around).

I think I know the reason for this cause (at least for my case). Looking in the DDMS --> File Explorer, you'd realize that the Database Folder (/data/data//databases/) does not exist, which is why the application cannot create the database file in that non-existent folder. If you can create a databases folder in some manner, you can avoid this problem.

Because I'm lazy, I just used the /data/data//files/ folder when I'm in Emulator mode. You can get the files dir using this:

context.getFilesDir().getPath()

This worked beautifully for me in the Emulator.

Hope this helps someone.

In case you want to see some code:

String dbFilename = "example.db";
try
{
    File databaseFile = getDatabasePath(dbFilename);
    SQLiteDatabase _db = SQLiteDatabase.openOrCreateDatabase(databaseFile);
} catch (Exception e)
{
    String databasePath =  getFilesDir().getPath() +  "/" + dbFilename;
    File databaseFile = new File(databasePath);
    _db = SQLiteDatabase.openOrCreateDatabase(databaseFile);
}

Edit:

I tried logging into Facebook (my app has FB integration) on the Emulator and /databases folder appeared after that (and persisted). Not sure what happened, but it's possible to create that folder somehow. Something for another expert around here to shed light on.

这篇关于SQLite的Andr​​oid无法打开数据库文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:12