0

In My database I want to retrieve a row from my database . For this I write a query :

Cursor mCursor = db.query("Author_table", column_name, "Author_name=?" , params, null , null , null) ;

By this I want to do the following query : "Select AID from Author_table where Author_name = Sultan" . But I have got anexception in SQLiteDatabase in android . Please suggest me what to do ??

8
  • 1
    mention the exception that you got Commented Oct 10, 2012 at 13:33
  • Better yet, look at the exception and try to understand it and fix it. (And post a question here when all else fails) Commented Oct 10, 2012 at 13:34
  • The exception shows that There is no table named "Author_table" .But I am sure that the table exists . From this I guess that there is some error in query syntax . Commented Oct 10, 2012 at 13:35
  • can u show the code for creating table? are you using open helper or plain sql commands? Commented Oct 10, 2012 at 13:48
  • I have used SQLiteDatabase Browser to create that database . Commented Oct 10, 2012 at 13:51

3 Answers 3

1

Something like this should work:

Cursor c = db.query("Author_table", new String[] {"AID"}, "Author_name=?", new String[] {"Sultan"}, null, null, null);

EDIT

SQLOpenHelper

import java.io.File;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

public class OpenHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "test.db";
    private static final int DATABASE_VERSION = 8;

    private static final String CREATE_TABLE_USER = "create table USER( "
            + "username varchar(256) not null, " + "sex varchar(1) not null, "
            + "password varchar(64) not null, " + "active int not null"
            + ");";

    public OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_USER);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        if (oldVersion < 8) {
            // db update goes here and in onCreate() (for newly installed apps)
        }
    }

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

Comments

0

Wrap string values in single quotes. Your query should look like

Select AID from Author_table where Author_name = 'Sultan';

instead of

Select AID from Author_table where Author_name = Sultan;

Comments

0

Try this query

Cursor cursor = db.query("Author_table", new String[] { "AID" },
                    "Author_name LIKE ?", new String[] { "%Sultan%" }, null, null,null);

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.