Android Studio说数据库是空的,而实际上并非如此

时间:2017-11-07 13:57:51

标签: android mysql sqlite android-studio indexoutofboundsexception

这是将项目插入我的数据库 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()); } \

的代码
exceptions

我观察到没有抛出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(); } 并且没有在logcat中创建任何条目。因此,让我相信数据是成功的 插入

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

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

这引发了异常

null

从一点点谷歌搜索我知道当 createButton() { '<div style="width: 200px; height: 100px;overflow: hidden">' + '<strong>' + line.title + '</strong>' + ' <ul style=" margin: 8px 0 0;">' + ' <li><span style="background: url(../../../../../assets/app-icons/arrow-popup.png) no-repeat; width: 100%;height: 100%;background-size: 32%;padding: 0px 11px 13px 30px;">' + line.milage + '</span></li>' + ' <li><span style="background: url(../../../../../assets/app-icons/view-popup.png) no-repeat;width: 100%;height: 100%;background-size: 18%;padding: 0px 11px 13px 30px;">KMZ (web)</span></li>' + '</ul>' + '<button role="button" (click)="viewCircuit()" style=" color: #18377C;font-size: 14px;display: block;text-align: right;">View</button>' + '</div>' } viewCircuit() { console.log('data') } 返回光标时会抛出此错误我不明白为什么会发生这种情况会感激不尽!

1 个答案:

答案 0 :(得分:0)

好的,我发现这个,你需要添加

?
你在rawQuery语句中的

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

link