gpt4 book ai didi

java - 当我尝试加载数据库时应用程序不断崩溃

转载 作者:行者123 更新时间:2023-12-01 18:32:20 26 4
gpt4 key购买 nike

我目前正在制作一个 Android 应用程序,现在我正在尝试将这个新的“listingStatus”列添加到我的数据库中,我将列名称添加到我的 onCreate() 函数中,但我仍然不断收到这是我的 logCat。有什么想法如何解决这个问题吗?我尝试删除数据库并重新创建它,但我不断收到相同的错误。

package com.example.shareandcare;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class listingHelper extends SQLiteOpenHelper {
public listingHelper(Context context) {
super(context, "listing.db", null, 2);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table listing(_id integer primary key autoincrement, listingTitle text, listingTag text, listingDescription text, listingDimensionX float, listingDimensionY float, listingDimensionZ float, listingImage text, listingStatus text)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("drop table if exists listing");
onCreate(db);
}

public Cursor getAll() {
return (getReadableDatabase().rawQuery("select _id, listingTitle, listingTag, listingDescription, listingDimensionX, listingDimensionY, listingDimensionZ, listingImage from listing order by listingTag", null));
}

public Cursor getAllConfirmed() {
String arg = "1";
String[] args = {arg};
return (getReadableDatabase().rawQuery("select _id, listingTitle, listingTag, listingDescription, listingDimensionX, listingDimensionY, listingDimensionZ, listingImage from listing where listingStatus=?", args));
}

public Cursor getById(String id) {
String[] args = {id};

return (getReadableDatabase().rawQuery("select _id, listingTitle, listingTag, listingDescription, lisitngDimensionX, listingDimensionY, listingDimensionZ, listingImage from listing where _id=?", args));
}

public Cursor getByTitle(String title) {
String[] args = {title};

return (getReadableDatabase().rawQuery("select _id, listingTitle, listingTag, listingDescription, listingDimensionX, listingDimensionY, listingDimensionZ, listingImage from listing where listingTitle =?", args));
}

public Cursor getByTag(String tag) {
tag = tag.toLowerCase();
String[] args = {tag};

return (getReadableDatabase().rawQuery("select _id, listingTitle, listingTag, listingDescription, listingDimensionX, listingDimensionY, lisitngDimensionZ, listingImage from listing where listingTag =?", args));
}

public void updateListingStatus(String title) {
ContentValues cv = new ContentValues();
String[] args ={title};

cv.put("listingStatus", "1");
getWritableDatabase().update("listing", cv, "title=?", args);
}

public void addListing(String listingTitle, String listingTag, String listingDescription, String listingDimensionX, String listingDimensionY, String listingDimensionZ, String listingImage) {
ContentValues cv = new ContentValues();

cv.put("listingTitle", listingTitle);
cv.put("listingTag", listingTag.toLowerCase());
cv.put("listingDescription", listingDescription);
cv.put("listingDimensionX", listingDimensionX);
cv.put("listingDimensionY", listingDimensionY);
cv.put("listingDimensionZ", listingDimensionZ);
cv.put("listingImage", listingImage);

getWritableDatabase().insert("listing", "listingTitle", cv);
}

public void delete(String id) {
String[] args = {id};
getWritableDatabase().delete("listing","_id=?", args);
}

public void resetDatabase() {
getWritableDatabase().execSQL("drop table if exists listing");
}
}

这是我的 logCat。

    --------- beginning of crash
2020-02-10 00:24:06.786 12602-12602/com.example.shareandcare E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shareandcare, PID: 12602
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shareandcare/com.example.shareandcare.admin}: android.database.sqlite.SQLiteException: no such column: listingStatus (code 1 SQLITE_ERROR): , while compiling: select _id, listingTitle, listingTag, listingDescription, listingDimensionX, listingDimensionY, listingDimensionZ, listingImage from listing where listingStatus=1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.database.sqlite.SQLiteException: no such column: listingStatus (code 1 SQLITE_ERROR): , while compiling: select _id, listingTitle, listingTag, listingDescription, listingDimensionX, listingDimensionY, listingDimensionZ, listingImage from listing where listingStatus=1
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:593)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1443)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1382)
at com.example.shareandcare.listingHelper.getAllConfirmed(listingHelper.java:31)
at com.example.shareandcare.admin.onCreate(admin.java:33)
at android.app.Activity.performCreate(Activity.java:7825)
at android.app.Activity.performCreate(Activity.java:7814)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
2020-02-10 00:24:06.814 12602-12602/com.example.shareandcare I/Process: Sending signal. PID: 12602 SIG: 9

最佳答案

您用于创建和删除表的 SQL 代码看起来有效。 logcat-log 中有一行:“no such column”,这意味着您的“getAllConfirmed”已执行,但您的迁移不起作用,因此请注意这一点:

  1. 增加数据库的版本号,也许您的升级脚本未执行(因此只需在 super 构造函数调用中将“2”增加到“3”即可)
  2. 如果这不起作用,您可以从设备/模拟器中卸载该应用,就像评论中提到的 forpas 一样。
  3. 设置断点,查看建表/升级命令是否执行

一般来说,如果您想添加列,最好只更改表并添加列。但我想您刚刚开始开发您的应用程序,您希望让事情变得更简单,因此只要您尚未发布您的应用程序,您的方法就有效。

如果您刚刚开始,我只是想让您知道,Android 引入了一种新的数据库创建方式,这种方式更加常见和简单:

https://developer.android.com/training/data-storage/room

这并不能解决您的问题,但如果您能够升级,这将简化项目的数据库创建

关于java - 当我尝试加载数据库时应用程序不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60138802/

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