gpt4 book ai didi

安卓 SQLite 错误 : Inserting data

转载 作者:行者123 更新时间:2023-11-30 02:21:52 26 4
gpt4 key购买 nike

每当我尝试访问数据库中的数据时,我的应用程序就会崩溃。我怀疑我没有将数据正确插入数据库,下面是我使用的类和方法。

public class MySQLiteHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "mydb.db";
private static final int DATABASE_VERSION = 1;

public static final String TABLE_QUICK_STOP = "quick_stop";

public static String[] QUICK_STOP_FIELDS = {
"_id", // autoincrement
"quick_stop_stop_id", // stop id
"quick_stop_name",
"quick_stop_types", // ex. 1111 or 0101. Underground Train Bus Ferry, used to filter some specific stations
"quick_stop_position"
//TODO should there be a "stop_name" here?
};

private static final String DATABASE_CREATE = "create table "
+ TABLE_QUICK_STOP + "("
+ QUICK_STOP_FIELDS[0] + " long primary key, " // id (same stop can be added multiple times with different filters)
+ QUICK_STOP_FIELDS[1] + " integer not null, " // stop id
+ QUICK_STOP_FIELDS[2] + " varchar not null, " // stop name
+ QUICK_STOP_FIELDS[3] + " integer not null, " // stop types
+ QUICK_STOP_FIELDS[4] + " integer not null);"; // stop position array[4] = cursor.getInt(4); // stop position

public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUICK_STOP);
onCreate(db);
}
}

我用它来插入:

public Boolean insertStop(int stopId, String name, int types, int position) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.QUICK_STOP_FIELDS[1], stopId);
values.put(MySQLiteHelper.QUICK_STOP_FIELDS[2], name);
values.put(MySQLiteHelper.QUICK_STOP_FIELDS[3], types);
values.put(MySQLiteHelper.QUICK_STOP_FIELDS[4], position);
long insertId = database.insert(MySQLiteHelper.TABLE_QUICK_STOP, null, values);
if(insertId == -1)
return false;
return true;
}

如前所述,Android Studio 在提供调试信息方面非常糟糕。

最佳答案

您是否在第一次运行后更改表结构?

如果是这样,更改DATABASE_VERSION = 1;到 DATABASE_VERSION = 2;或更高的数字,以使 onUpgrade() 触发。

或者,作为替代方案,尝试从 /data/data/... 路径中删除(或重命名)数据库,以便在下次运行时重新创建它。

关于安卓 SQLite 错误 : Inserting data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386527/

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