大家好我试图检索我在之前的活动中插入数据库的一些数据。这是我检索数据的代码
public class Display_data extends AppCompatActivity {
ArrayList<String> Displist = new ArrayList<String>();
ListView no_name;
String db_Name;
TextView namer;
TextView Starter;
TextView Ender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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 Train_name From Train_list Where Train_no="+message.substring(0,(message.length()-3)), null);
header.moveToFirst();
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();
}
db_Name=message.substring(0,(message.length()-3));
try{
Cursor data_fetch = db.rawQuery("Select Stops From "+db_Name, null);
while (data_fetch.moveToNext()) {
String name = data_fetch.getString(data_fetch.getColumnIndex("Stops"));
if(Displist.contains(name)) {
}
else {
Displist.add(name);
//Toast.makeText(Display_data.this, "Added :"+name, Toast.LENGTH_SHORT).show();
}
}
data_fetch.close();
}catch (Exception e)
{
Toast.makeText(Display_data.this, "Yeah"+e.toString(), Toast.LENGTH_LONG).show();
Log.d("Damn Why",e.toString());
}
try {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1,Displist);
no_name.setAdapter(arrayAdapter);
}catch (Exception e){
Log.d("Display data error ",e.toString());
Toast.makeText(Display_data.this, "Listview Error" , Toast.LENGTH_LONG).show();
}
}
}
我注意到该部分
try {
header = db1.rawQuery("Select Train_name From Train_list Where Train_no="+message.substring(0,(message.length()-3)), null);
header.moveToFirst();
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.sqlite.SQLiteException: no such column: aa1000 (code 1): , while compiling: Select Train_name From Train_list Where Train_no=aa1000
我真的不明白这个错误,因为&#39; aa1000&#39;被插入数据库。
这是数据库&train; list&#39;
的定义 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());
}
我检查了Android监视器页面,我没有发现错误,建议&#34; aa1000&#34;尚未插入数据库。
请注意,仅插入Train_name
和Train_no
时,其余内容均为空。
任何帮助将不胜感激!
答案 0 :(得分:1)
将aa1000
放入单引号('aa1000'):
您的SQL
查询应如下所示:
Select Train_name From Train_list Where Train_no='aa1000'
所以修改db.rawQuery
中的选择字符串:
"Select Train_name From Train_list Where Train_no='"+message.substring(0,(message.length()-3))+"'";
但是,建议不要将rawQuery
用于选择查询并以这种方式传递参数,更好地学习使用query
方法
答案 1 :(得分:0)
首先尝试理解rawQuery方法声明,然后执行如下:
<input type="radio" value="{{commencementDate.value}}" id="bankCommencementDateSelect" formControlName="bankCommencementDate">
<input #commencementDate id="bankCommencementDateInput" formControlName="bankCommencementDateInput" type="date" format="DD-MM-YYYY" min="{{ today | date: 'y-MM-dd' }}"/>
注意:'? '在查询运行到数据库时替换selectionArgs实际值