db = SQLiteDatabase.openDatabase("myDataBase.db", null, Context.MODE_PRIVATE);
但它生成了
09-18 00:00:08.238 26784-26784/com.dhilip.college E/SQLiteLog: (14) cannot open file at line 31364 of [2ef4f3a5b1]
09-18 00:00:08.238 26784-26784/com.dhilip.college E/SQLiteLog: (14) os_unix.c:31364: (2) open(//myDataBase.db) -
09-18 00:00:08.251 26784-26784/com.dhilip.college E/SQLiteDatabase: Failed to open database 'myDataBase.db'.
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:212)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:196)
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:836)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:821)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:714)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:689)
at com.dhilip.college.ebooks_adapter.MyArrayAdaptor.getView(MyArrayAdaptor.java:69)
at android.widget.AbsListView.obtainView(AbsListView.java:2474)
at android.widget.ListView.makeAndAddView(ListView.java:1920)
at android.widget.ListView.fillDown(ListView.java:717)
at android.widget.ListView.fillFromTop(ListView.java:778)
at android.widget.ListView.layoutChildren(ListView.java:1701)
at android.widget.AbsListView.onLayout(AbsListView.java:2235)
at android.view.View.layout(View.java:16969)
at android.view.ViewGroup.layout(ViewGroup.java:5583)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1367)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:849)
at android.view.View.layout(View.java:16969)
at android.view.ViewGroup.layout(ViewGroup.java:5583)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:16969)
at android.view.ViewGroup.layout(ViewGroup.java:5583)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:16969)
at android.view.ViewGroup.layout(ViewGroup.java:5583)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:16969)
at android.view.ViewGroup.layout(ViewGroup.java:5583)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1753)
at android.view.View.layout(View.java:16969)
at android.view.ViewGroup.layout(ViewGroup.java:5583)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2728)
at android.view.View.layout(View.java:16969)
at android.view.ViewGroup.layout(ViewGroup.java:5583)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2552)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2255)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1321)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6708)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
at android.view.Choreographer.doCallbacks(Choreographer.java:696)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
at android.os.Handler.handleCallback(Handler.java:822)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.mai
09-18 00:00:08.252 26784-26784/com.dhilip.college D/AndroidRuntime: Shutting down VM
答案 0 :(得分:1)
好的,你需要这个设计一个DBHelper类和你想要使用DB的功能的任何其他Activity(Class),你可以调用DB那就是说你最好处理所有数据库函数DBHelper类你知道CRUD函数
首先在DBHelper中定义使用数据库`静态SQLiteDatabase db的变量; 现在假设我们想要将一些数据添加到MainActivity中的数据库中的表中,我们使用此代码定义“helper”和“db”
private DBHelper helper;
private SQLiteDatabase db;
helper = new DBHelper(this);
public void doAdd() {// Adds Master Password to TABLE_PW
db = helper.getWritableDatabase();
String password = etPW.getText().toString().trim();
ContentValues cv = new ContentValues();
cv.put( Col_MPW,password);
db.insert( TABLE_PW,null,cv);
etPW.setText("");
etCPW.setText("");
onLoad();
db.close();
}
现在再从DBHelper类导入一个项目,如果您要引用并使用MainActivity中DBHelper类中定义的项目
import static <package.name.goes.here>.DBHelper.TABLE_PW;
答案 1 :(得分:0)
使用
db = context.openOrCreateDatabase("myDataBase.db",MODE_PRIVATE,null);
而不是
db = SQLiteDatabase.openDatabase("myDataBase.db", null, Context.MODE_PRIVATE);
它对我有用;)
答案 2 :(得分:0)
getApplicationContext().openOrCreateDatabase("myDB", Context.MODE_PRIVATE, null);
这对我有用。就我而言,我要用database
打开WorkManager
。