![](/img/trans.png)
[英]Android studio SQLite database data retrieval for specific entires
[英]Data Retrieval issue from sqlite database in Android
我是 Android 开发的新手。 我面临的问题是我想从停止名称中获取 stop_id。我有一个数据库,它包含所有必需的数据。
当我直接在数据库中运行查询时,它给了我正确的答案,但它在 android 中不起作用。
如果我使用getString(0)
函数返回 null,如果我使用getInt(0)
函数返回 0.. 请帮助我..
这是我的代码:
public class Bus_Schedule_tab3 extends Fragment {
DataBase_Class db;
Button btn_go;
EditText source_txt;
EditText destination_txt;
int source_id ;
int destination_id ;
String Source_Destination_query;
String sourceTxt , destinationTxt;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bus_schedule_tab3, container, false);
btn_go = (Button) rootView.findViewById(R.id.btn_go);
db = new DataBase_Class(getActivity());
source_txt = (EditText) rootView.findViewById(R.id.edtxt_source);
destination_txt = (EditText) rootView.findViewById(R.id.edtxt_destination);
getOnlySource();
return rootView;
}
public void getOnlySource() {
btn_go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer buffer = new StringBuffer();
Cursor c_sourse_id = db.GetOnlySource("select stop_id from stops_table where stop_name = '"+source_txt.getText().toString()+"'");
Cursor c_destination_id = db.GetOnlySource("select stop_id from stops_table where stop_name = '"+destination_txt.getText().toString()+"'");
while (c_sourse_id.moveToNext()) {
source_id = c_sourse_id.getInt(0);}
while (c_destination_id.moveToNext()) {
destination_id = c_destination_id.getInt(0);}
buffer.append(source_id +" "+ destination_id);
showMessage("Data",buffer.toString());
}
});
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
使用这个:
cursor.getInt(cursor.getColumnIndex(USER_ID))
其中USER_ID
是列名
检查您的 db 对象是否为空,因为您没有在 getOnlySource() 方法中将非空对象作为参数传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.