简体   繁体   中英

Android : SQLite search

I am developing a simple app that inserts in database and searches in the inserted data

i created the database and i could see all the rows inserted...i want to search for the rows using the Name column

i am having an error and i don't know where is the error line

below is my code to create the database.

sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

sampleDB.execSQL("CREATE TABLE IF NOT EXISTS "+SAMPLE_TABLE_NAME +
                    " (Id INT(50), Name VARCHAR," +
                    " Price INT(50), Qty INT(50), Discount INT(50), Category INT(10));");

and here's the code to search in the database

Button saveButton = (Button) findViewById(R.id.search_button);

        saveButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                String searchStr = search.getText().toString();

                Cursor c = sampleDB.rawQuery("SELECT Name, Price FROM "+SAMPLE_TABLE_NAME+" WHERE Name like '"+searchStr+"'", null);

                if (c != null ) { 
                    if  (c.moveToFirst()) {
                        do {
                            String name = c.getString(c.getColumnIndex("Name"));
                            int cat = c.getInt(c.getColumnIndex("Category"));
                            results.add("" + name + ",Age: " + cat);
                            Log.v("Fname", name);
                            Log.v("Cat", cat+"");
                        }while (c.moveToNext());
                    }
                }

            }
        });

knowing that searchStr has the name i want to search for in the database

here's the error i am having

Bad request for field slot 0,-1. numRows = 18, numColumns = 2
FATAL EXCEPTION: main
java.lang.IllegalStateException: get field slot from row 0 col -1 failed

any help please ?

You are selecting only name and price in the query:

Cursor c = sampleDB.rawQuery("SELECT Name, Price FROM "+SAMPLE_TABLE_NAME+" WHERE Name like '"+searchStr+"'", null);

and then you are trying to access category ..

int cat = c.getInt(c.getColumnIndex("Category"));

So you should add category to your select statement:

Cursor c = sampleDB.rawQuery("SELECT Name, Price, Category FROM "+SAMPLE_TABLE_NAME+" WHERE Name like '"+searchStr+"'", null);

Use FTS table. FTS tables are designed so that any SELECT statement will work correctly. You can even search for specific text values or patterns directly with the == or LIKE operators. These will work, although they'll be somewhat slow, since standard operators will require a full table scan.

The real power of the FTS system comes from a custom MATCH operator. This operator is able to take advantage of the indexes built around the individual text values, allowing very fast searches over large bodies of text. Generally, searches are done using a query similar to:

Issue is here: Your SQL is:

SELECT Name, Price FROM 

But you are getting the value of Category

int cat = c.getInt(c.getColumnIndex("Category")); 

You are trying to access Category field, but in your select query, there's no Category. You have to add it in your select query so you can use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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