简体   繁体   English

Android中sqlite数据库的数据检索问题

[英]Data Retrieval issue from sqlite database in Android

I am new to Android development.我是 Android 开发的新手。 The problem that I am facing is that I want to get stop_id from stop name.I have a database and it contains all required data.我面临的问题是我想从停止名称中获取 stop_id。我有一个数据库,它包含所有必需的数据。

When I run query directly in database so it's giving me right answer but it's not working in android.当我直接在数据库中运行查询时,它给了我正确的答案,但它在 android 中不起作用。

It's returning null if I use getString(0) function and returning 0 if I use getInt(0) function.. please help me..如果我使用getString(0)函数返回 null,如果我使用getInt(0)函数返回 0.. 请帮助我..

Here's my code:这是我的代码:

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();
   }
}

use this :使用这个:

cursor.getInt(cursor.getColumnIndex(USER_ID))

where USER_ID is name of column其中USER_ID是列名

检查您的 db 对象是否为空,因为您没有在 getOnlySource() 方法中将非空对象作为参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM