0

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

    }

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

it is cursor.getString() .it should return a String from database..and i think getcountt() is for Returning the numbers of rows in the cursor.I tried rawQuery cursor=db.rawQuery("SELECT ID FROM colors WHERE English = name" , null); but nothing is printed again.name is not recognized as a variable i guess
Which columns does your database table have? The SQL statement you posted should give you all ID values from colors database where die Column English equals the column name. if you want all rows where column ExampleCol equals the name variable try this: db.rawQuery("SELECT * FROM colors WHERE ExampleCol = ?", {name});
i got the answer when i put it as , cursor=db.rawQuery("SELECT ID FROM colors WHERE English='"+name1+"'", null); and name has to be defined as Editable.Thank you for your support
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.