openOrCreateDatabase(String,int,null)方法获得异常"无法打开数据库"

时间:2016-01-03 05:36:03

标签: android database logcat

我正在尝试创建数据库实例。这就是为什么我有以下ocde:

public class ContactFragment extends SherlockFragment {

    private static final String THIS_FILE = "ContactFragment";
    ImageButton new_contact ;
    ListView listView;
    ArrayAdapter arrayAdapter;
    SQLiteDatabase database;
    List<CONTACT_ITEM> contactBody;
    int MODE_PRIVATE=1;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        database = openOrCreateDatabase("ContactDatabase",null);


    //    database.execSQL("CREATE TABLE if not exists constants (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, number text , extension text);");

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.contactdialer, container, false);

        contactBody = getReadFromDatabase();
        new_contact = (ImageButton) rootView.findViewById(R.id.imageButton);
        new_contact.setOnClickListener(this);

        listView = (ListView) rootView.findViewById(R.id.list_view);
        arrayAdapter=new ContactAdapter(getActivity(),R.layout.row_item_contact,contactBody);
        listView.setAdapter(arrayAdapter);

        return rootView;
    }

    List<CONTACT_ITEM> getReadFromDatabase()
    {
            List<CONTACT_ITEM> contact = new ArrayList<CONTACT_ITEM>();
            return contact;
    }

}

但是我在以下方面遇到了例外:

 database = openOrCreateDatabase("ContactDatabase",null);

logcat给出以下异常:

android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
            at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
            at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
            at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:829)
            at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:814)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:709)
            at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:724)
            at com.csipsimple.ui.contacts.ContactFragment.onCreate(ContactFragment.java:38)
            at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
            at android.view.View.measure(View.java:16848)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
            at android.view.View.measure(View.java:16848)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
            at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
            at android.view.View.measure(View.java:16848)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2368)
            at android.view.View.measure(View.java:16848)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2246)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1312)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1509)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
            at android.view.Choreographer.doCallbacks(Choreographer.java:591)
            at android.view.Choreographer.doFrame(Choreographer.java:560)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
            at android.os.Handler.handleCallback(Handler.java:808)
            at android.os.Handler.dispatchMessage(Handler.java:103)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5330)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
            at dalvik.system.NativeStart.main(Native Method)

为什么我收到此错误?我怎么解决这个问题?任何建议都有很大帮助。

1 个答案:

答案 0 :(得分:0)

public abstract SQLiteDatabase openOrCreateDatabase(String name,int mode,SQLiteDatabase.CursorFactory factory)

打开与此Context的应用程序包关联的新私有SQLiteDatabase。如果数据库文件不存在,请创建它。

参数 name数据库的名称(在应用程序包中唯一)。 模式操作模式。使用0或MODE_PRIVATE作为默认操作,使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE来控制权限。默认情况下,使用MODE_ENABLE_WRITE_AHEAD_LOGGING启用预写日志记录。 factory一个可选的工厂类,在调用查询时调用它来实例化游标。 返回

抛出 SQLiteException如果无法打开数据库文件。

openOrCreateDatabase(String,int,SQLiteDatabase.CursorFactory) 使用

    database = openOrCreateDatabase("ContactDatabase",0);
    database = openOrCreateDatabase("ContactDatabase",MODE_PRIVATE);