gpt4 book ai didi

android - SD卡中的SQLite数据库打不开

转载 作者:行者123 更新时间:2023-11-29 18:03:27 24 4
gpt4 key购买 nike

为什么我的数据库出错。请帮助我:- 我正在 sdcard 中创建我的数据库。

我哪里做错了。

public class SDcardDatabase extends SQLiteOpenHelper
{

public static final String DATABASE_FILE_PATH = "/sdcard";
public static final String DATABASE_NAME = "testDatabase";
public final static String NAME ="name";
public final static String ADDRESS ="address";
public final static String CITY ="city";

public SDcardDatabase(final Context context) {
super(context,Environment.getExternalStorageDirectory()+File.separator+DATABASE_FILE_PATH + File.separator + DATABASE_NAME, null, 1);
}


@Override
public void onCreate(SQLiteDatabase db) {
try
{
db.execSQL( "CREATE TABLE mylistdata(_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, address TEXT,city TEXT);");

}

catch (SQLiteException ex)
{
Log.e("Hello", "error -- " + ex.getMessage(), ex);
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP Table mylistdata");
onCreate(db);
}
}

并在 Activity 中调用它。

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mydb = new SDcardDatabase(this);
sqldb = mydb.getWritableDatabase();
sqldb.close();
}

错误日志:-

01-29 10:03:46.534: W/dalvikvm(8185): threadid=1: thread exiting with uncaught exception (group=0x40014760)
01-29 10:03:46.548: E/AndroidRuntime(8185): FATAL EXCEPTION: main
01-29 10:03:46.548: E/AndroidRuntime(8185): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sdcard/com.example.sdcard.SDcardDBActivity}: android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.os.Looper.loop(Looper.java:126)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.app.ActivityThread.main(ActivityThread.java:3997)
01-29 10:03:46.548: E/AndroidRuntime(8185): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 10:03:46.548: E/AndroidRuntime(8185): at java.lang.reflect.Method.invoke(Method.java:491)
01-29 10:03:46.548: E/AndroidRuntime(8185): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-29 10:03:46.548: E/AndroidRuntime(8185): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-29 10:03:46.548: E/AndroidRuntime(8185): at dalvik.system.NativeStart.main(Native Method)
01-29 10:03:46.548: E/AndroidRuntime(8185): Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:978)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:951)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1016)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:726)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
01-29 10:03:46.548: E/AndroidRuntime(8185): at com.example.sdcard.SDcardDBActivity.onCreate(SDcardDBActivity.java:20)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
01-29 10:03:46.548: E/AndroidRuntime(8185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)

最佳答案

尝试添加:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于android - SD卡中的SQLite数据库打不开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14575729/

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