gpt4 book ai didi

java - Android 上的 SQLite 错误

转载 作者:行者123 更新时间:2023-12-02 03:48:07 24 4
gpt4 key购买 nike

我正在尝试将 SQLite 添加到我的项目中,但当我将数据发送到数据库时出现此错误

> 03-21 18:59:20.742 27050-27050/org.lili14.taps E/SQLiteLog: (1) table
> Images_table has no column named TAG0 03-21 18:59:20.782
> 27050-27050/org.lili14.taps E/SQLiteDatabase: Error inserting ...
> android.database.sqlite.SQLiteException: table Images_table has no
> column named TAG0 (code 1): , while compiling: INSERT INTO
> Images_table(TAG0,URL) VALUES (?,?)
> at
> android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
> Method)
> at
> android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:917)
> at
> android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:528)
> at
> android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
> at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
> at
> android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
> at
> android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1547)
> at
> android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1404)
> at
> org.lili14.taps.database.DatabaseHelper.insertData(DatabaseHelper.java:48)
> at org.lili14.taps.TwoFragment$1.onClick(TwoFragment.java:75)
> at android.view.View.performClick(View.java:4785)
> at android.view.View$PerformClick.run(View.java:19869)
> at android.os.Handler.handleCallback(Handler.java:739)
> at android.os.Handler.dispatchMessage(Handler.java:95)
> at android.os.Looper.loop(Looper.java:155)
> at android.app.ActivityThread.main(ActivityThread.java:5696)
> at java.lang.reflect.Method.invoke(Native Method)
> at java.lang.reflect.Method.invoke(Method.java:372)
> at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

这是我的 DatabaseHelper 类

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Images.db";
public static final String TABLE_NAME = "Images_table";


public static final String col01 = "URL";
public static final String col02 = "TAG0";



public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("create table " + TABLE_NAME + " ( id INTEGER PRIMARY KEY AUTOINCREMENT, TAG0 TEXT, URL TEXT )");


}

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

db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
onCreate(db);
}

public boolean insertData(String url, String tag){

SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(col01, url);
contentValues.put(col02, tag);
long result = db.insert(TABLE_NAME, null, contentValues);

if(result == -1){
return false;

}
else{
return true;
}
}



public Cursor getAllDate(){

SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("Select * from "+TABLE_NAME, null);
return res;
}

}

如果我删除 ContentValue 工作中的第二个值但不保存所有数据我只想保存 2 个值并自动生成 ID

最佳答案

尝试在构造函数中更改数据库版本:

super(context, DATABASE_NAME, null, 2);

关于java - Android 上的 SQLite 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36144004/

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