我当前正在制作一个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
答案 0 :(得分:0)
您用于创建和删除表的SQL代码看起来有效。 logcat-log中有一行:“无此列”,这意味着您的“ getAllConfirmed”已执行,但您的迁移无法正常工作,因此请留意以下内容:
通常,如果要添加列,最好更改表并添加列。但是我想您只是应用程序的开始,并且您想让事情变得更简单,因此只要您尚未发布应用程序,您的方法就有效。
如果您刚刚开始,我只想让您知道,Android引入了一种新的数据库创建方法,该方法更常见,更简单:
https://developer.android.com/training/data-storage/room
这不能解决您的问题,但是如果您能够升级,则可以简化项目的数据库创建
答案 1 :(得分:0)
只需添加,请始终添加一个null检查器,以防止应用程序崩溃。
简单:
if (null) {
return "";
}
would go a long way