本文介绍了如何在Android中使用光标(SQLite的查询)通过DB搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个机器人初学者。我试图设计这个简单的应用程序这需要从使用EDITTEXT查看用户一个城市的名字,比较了在数据库中,并返回该城的ZIP code。
现在,我在使用游标的实现问题。请我help.How可以查询数据库获取相应的code。

 的EditText城市;
    按钮添加,秀王;
    RadioGroup中选择;
    字符串K表;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        尝试
        {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        //最后ContentValues​​值=新ContentValues​​();
        城市=(EditText上)findViewById(R.id.editText1);
        添加=(按钮)findViewById(R.id.add);
        选择=(RadioGroup中)findViewById(R.id.radio01);
        秀王=(按钮)findViewById(R.id.button1);
        K = city.getText()的toString()。
        的CreateDatabase();
    }
        赶上(例外五)
        {        }
show1.setOnClickListener(新View.OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根
        DB = openOrCreateDatabase(DATABASE_NAME,Context.MODE_PRIVATE,NULL);
        尝试{        如果(K ==CITY)
        {
            的String [] = result_columns新的String [] {_ ID,城市,code};
            光标光标= db.query(TABLE_NAME,result_columns,
                    CITY+=?,新的String [] {K},NULL,NULL,NULL);    cursor.moveToFirst();        字符串xnew code = cursor.getString(0);
        Toast.makeText(activity1.this,xnew code,Toast.LENGTH_LONG).show();
        cursor.moveToNext();            //db.close();
        }}
        赶上(例外五)
        {
            Toast.makeText(activity1.this,故障在显示+ E,Toast.LENGTH_LONG).show();        }    }});


解决方案

 光标光标= db.rawQuery(选择_id,市,code
      从城市里CITY_NAME像'+ S +%'ORDER BY城,NULL);

s是你输入搜索框中的字符字符串

使用下面的链接,它在Android应用程序的开发非常有益的。

I am an android beginner. I am trying to design this simple application which takes the name of a city from the user using an editText View , compares that in the database , and returns the ZIP code of that city.Now ,I'm having a problem with the implementation of cursors. Please help.How can i query the database to fetch the corresponding code.

        EditText city;
    Button add,show1;
    RadioGroup choose;
    String k;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        try
        {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //final ContentValues values = new ContentValues();
        city=(EditText)findViewById(R.id.editText1);
        add=(Button)findViewById(R.id.add);
        choose=(RadioGroup)findViewById(R.id.radio01);
        show1=(Button)findViewById(R.id.button1);
        k=city.getText().toString();
        createDatabase();
    }
        catch(Exception e)
        {

        }


show1.setOnClickListener(new View.OnClickListener() {


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        db=openOrCreateDatabase(DATABASE_NAME,Context.MODE_PRIVATE,null);
        try{

        if(k == "CITY")
        {
            String[] result_columns=new String[]{"_id","CITY","CODE"};
            Cursor cursor = db.query(TABLE_NAME, result_columns,
                    "CITY" +"=?", new String[]{"k"}, null, null, null);

    cursor.moveToFirst();

        String xnewcode=cursor.getString(0);
        Toast.makeText(activity1.this, xnewcode, Toast.LENGTH_LONG).show();
        cursor.moveToNext();

            //db.close();
        }}
        catch(Exception e)
        {
            Toast.makeText(activity1.this,"Fault in showing " + e,Toast.LENGTH_LONG).show();

        }

    }

});
解决方案
Cursor cursor = db.rawQuery("select _id,city,code
      FROM city where city_name like '"+s+"%' order by city", null);

s is the your string of enter characters in search box

use the below links it's very useful in android app development.

http://samir-mangroliya.blogspot.in/2012/05/android-sectioned-listview-with-search_6865.html

这篇关于如何在Android中使用光标(SQLite的查询)通过DB搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:53