gpt4 book ai didi

java - 如何在android studio中的java类中的包文件中打开sqlite db

转载 作者:行者123 更新时间:2023-12-02 12:14:49 26 4
gpt4 key购买 nike

我里面有一个包 ebooks_adaptor 和 MyArrayAdaptor java 类,我想在其中调用 sqlite db 以将数据导入其中,我发现在包内我们应该使用静态方法,所以我使用了

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

最佳答案

好的,您需要设计一个 DBHelper 类,并且在任何其他 Activity (类)中您想要使用数据库的功能,您可以调用数据库这就是说,最好是处理DBHelper类你知道的CRUD函数

因此,首先在 DBHelper 中定义使用数据库的变量`static 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();
}

现在,如果您要按照 MainActivity 中的 DBHelper 类中的定义引用和使用该项目,那么现在还需要从 DBHelper 类导入一项

import static <package.name.goes.here>.DBHelper.TABLE_PW;

关于java - 如何在android studio中的java类中的包文件中打开sqlite db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46267187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com