Soumya
Soumya

Reputation: 383

How to Query user input data in SQLIte?

I am trying to write a query in SQLite.The application is like, when user gives the ID in text box corresponding color(which is taken from database) has to be printed as output.But i am facing a problem here.The variable name is defined as String in the code, but the query fromat is query(String, String[], String, String[], String, String, String).

Given below is my code.Nothing is printed in the out put.Please help me

package cis493.sqldatabases;



import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.*;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SQLDemo1Activity extends Activity {
    private SQLiteDatabase db;
    CharSequence colour = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       EditText edit1 = (EditText) findViewById(R.id.e1);
        final TextView met = (TextView) findViewById(R.id.t1);
    Button but1 = (Button) findViewById(R.id.b1);
      final String name=edit1.getText().toString();

    but1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


        try{
            db= SQLiteDatabase.openDatabase(
            "/data/data/cis493.sqldatabases/databases/multilinguialdatabase.sqlite",
            null,
            SQLiteDatabase.CREATE_IF_NECESSARY);
             db.beginTransaction();
            Cursor cursor =  db.query(
                        "colors" /* table */,
                        new String[] { "English" } /* columns */,
                        "ID = ?" /* where or selection */,
                        name /* selectionArgs i.e. value to replace ? */,
                        null /* groupBy */,
                        null /* having */,
                        null /* orderBy */
                    );
            if (cursor.moveToFirst()) {
                do {
                     colour= cursor.getString(0);
                // use value
                } while (cursor.moveToNext());
            }
                            met.setText(colour);
                         db.endTransaction();
            db.close();
            }
            catch(SQLiteException e) {
            //Toast.makeText(this, e.getMessage(), 1).show();
            }
        }
    });
           }

    }

Upvotes: 0

Views: 3057

Answers (2)

Soumya
Soumya

Reputation: 383

Got the solution when I put the query like

cursor=db.rawQuery("SELECT ID FROM colors WHERE English='"+name1+"'", null);

name has to be declared as Editable to use getText();.

Upvotes: 0

wessnerj
wessnerj

Reputation: 152

Try to use rawQuery instead of query:

rawQuery (String sql, String[] selectionArgs)

with rawQuery you can send your own sql-query to sqliteDatabase.

Futhermore opening a sqliteDatabase with full path looks evil to me, maybe you can use something like SQLiteOpenHelper.getWritableDatabase().

Edit: Does cursor.getCount() return a positive integer?

Upvotes: 1

Related Questions