gpt4 book ai didi

java - 编译时无法启动 Activity ComponentInfo : android. database.sqlite.SQLiteException : no such table ,

转载 作者:行者123 更新时间:2023-12-01 14:05:28 25 4
gpt4 key购买 nike

请帮帮我..在第一个应用程序版本中,我在 DBHelper 中写了错误的 DB_PATH,安装 apk 后出现找不到数据库的异常。然后(下一个应用程序版本)我写了正确的 DB_PATH 但安装后出现异常

    FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo: android.database.sqlite.SQLiteException: no such table: category: , while compiling: SELECT * FROM category
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: category: , while compiling: SELECT * FROM category
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1497)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1376)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1330)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1450)
at package.name.MainPage.update(MainPage.java:125)
at package.name.MainPage.onCreate(MainPage.java:88)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more

如果我重新安装 apk 并清除缓存,那么 apk 安装成功!如何在不清除缓存并重新安装的情况下成功安装?

主类

     ...
public class MainPage extends MainHelper implements OnClickListener{
...
public void onCreate(Bundle savedInstanceState) {
...
DBHelper myDbHelper = new DBHelper(this);

String oldpath = "/data/data/package.name1/databases/";
String newpath = "/data/data/package.name/databases/";
File dbFile = new File(newpath+"db.sqlite");

if (!dbFile.exists()) {
try {
myDbHelper.copyDataBase(oldpath);
myDbHelper.myContext.deleteDatabase(oldpath+bd_name);
} catch (IOException e) {
e.printStackTrace();
}
}

try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
....
}
}

数据库助手

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

public class DBHelper extends SQLiteOpenHelper{

private static String DB_PATH = "/data/data/package.name/databases/";
private static String DB_NAME = "db.sqlite";
private SQLiteDatabase myDataBase;
public final Context myContext;

public DBHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}

public void createDataBase() throws IOException{

boolean dbExist = checkDataBase();
if(dbExist){
}else{
this.getReadableDatabase();

try {
copyDataBase(DB_PATH);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}

private boolean checkDataBase(){
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}

public void copyDataBase(String bdpath) throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = bdpath + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();
}

public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {

if(myDataBase != null)
myDataBase.close();

super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

最佳答案

我想你知道解决办法

<小时/>

在执行数据库操作之前,请执行以下操作

您应该检查新数据库路径中是否存在数据库

如果不存在

然后从您的旧数据库路径{预期的位置}复制它

如果它不在其中

然后创建您的数据库

关于java - 编译时无法启动 Activity ComponentInfo : android. database.sqlite.SQLiteException : no such table ,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18942055/

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