gpt4 book ai didi

Android Studio 说数据库为空,但实际上不是

转载 作者:行者123 更新时间:2023-11-29 18:14:18 24 4
gpt4 key购买 nike

这是将项目插入到名为 Train_List\

的数据库中的代码
 SQLiteDatabase db = openOrCreateDatabase( "Train_list.db", SQLiteDatabase.CREATE_IF_NECESSARY , null);
try {
final String CREATE_TABLE_TRAIN_LIST = "CREATE TABLE IF NOT EXISTS Train_list ("
+ "Train_name VARCHAR,"
+ "Train_no VARCHAR,"
+ "Train_start VARCHAR,"
+ "Train_end VARCHAR,"
+ "Seats_Available VARCHAR);";
db.execSQL(CREATE_TABLE_TRAIN_LIST);
Toast.makeText(admin_manipulation.this, "Table created", Toast.LENGTH_LONG).show();
String sql = "INSERT or replace INTO Train_list (Train_name, Train_no, Train_start, Train_end, Seats_Available) VALUES('"+str_Train_name + "',' " +str_Train_no + "', '" +str_Train_start+"','" +str_Train_end+"',' " +str_Train_seats +"');";
try {
db.execSQL(sql);
Toast.makeText(admin_manipulation.this, "train no:"+str_Train_no, Toast.LENGTH_SHORT).show();
}catch(Exception e)
{
Toast.makeText(admin_manipulation.this, "Sorry Not Inserted Sucessfully", Toast.LENGTH_SHORT).show();
Log.d("Error experienced",e.toString());
}

而且我观察到没有抛出任何异常并且logcat中没有创建任何条目。从而让我相信数据已成功已插入。

这是检索插入的数据的代码:

setContentView(R.layout.activity_display_data);
Bundle bundle = getIntent().getExtras();
no_name = (ListView) findViewById(R.id.Disp_list);
String message = bundle.getString("package");
String parameter;
SQLiteDatabase db = openOrCreateDatabase(message, SQLiteDatabase.CREATE_IF_NECESSARY, null);
SQLiteDatabase db1 = openOrCreateDatabase("Train_list.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
Cursor header;
namer=(TextView)findViewById(R.id.T1);
try {
header = db1.rawQuery("Select * From Train_list Where Train_no='"+message.substring(0,(message.length()-3))+ "'", null);
String temp = header.getString((header.getColumnIndex("Train_name")));
Toast.makeText(Display_data.this, "blahhh:"+temp, Toast.LENGTH_LONG).show();
Log.d("Sucess!","temp:"+temp);
Toast.makeText(Display_data.this, "Gotcha:"+temp , Toast.LENGTH_LONG).show();
namer.setText("Train Name: " + temp);
}catch(Exception e)
{
Log.d("Final Error",e.toString());
Toast.makeText(Display_data.this, "Error", Toast.LENGTH_LONG).show();
}

这会引发异常

D/Final Error: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

通过一点谷歌搜索,我知道当 null 返回到游标时会引发此错误,我不明白为什么会发生这种情况,任何人都会感激不已!

最佳答案

好的,我找到了这个,你需要添加

?

在你的 rawQuery 语句中

header = db1.rawQuery("Select * From Train_list Where Train_no= ?'"+message.substring(0,(message.length()-3))+ "'", null)

link

关于Android Studio 说数据库为空,但实际上不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159778/

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