Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:23:29+00:00 2026-06-15T23:23:29+00:00

My main activity passes a string to another activity (let’s just call it sub

  • 0

My main activity passes a string to another activity (let’s just call it sub activity). In this sub activity, I need to use that string in a cursor raw query to select data from the database that matches the string.

public class RestaurantsInfo extends ListActivity {

static final String restaurantListTable = "RestaurantList";
static final String colRestaurantID = "RestaurantID";
static final String colRestaurantName = "RestaurantName";
static final String colRestaurantStore = "StoreNo";
static final String colRestaurantAddress = "StoreAddress";
public String strRestaurantName;
RestaurantDB db = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listview);
    db=new RestaurantDB(RestaurantsInfo.this);

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();

    String strRestaurantName = extras.getString("RestaurantName");
    setTitle(strRestaurantName);
    getData();
  }

private Cursor doQuery(String strRestaurantName) {
    return(db.getReadableDatabase().rawQuery("SELECT "+colRestaurantID+" AS _id, "+colRestaurantName+", "+colRestaurantStore+", "+colRestaurantAddress+
            " FROM "+restaurantListTable+" WHERE "+colRestaurantName+" = strRestaurantName ORDER BY "+colRestaurantStore+"", null));
    }


public void getData() {
    SimpleCursorAdapter adapter;

    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
        adapter=new SimpleCursorAdapter(this, R.layout.activity_address,
                doQuery(strRestaurantName), new String[] {
            RestaurantDB.colRestaurantName,
            RestaurantDB.colRestaurantStore,
            RestaurantDB.colRestaurantAddress },
            new int[] { R.id.textView1, R.id.textView2, R.id.textView3 },
            0);
        }
    else {
        adapter=new SimpleCursorAdapter(this, R.layout.activity_address,
                doQuery(strRestaurantName), new String[] {
            RestaurantDB.colRestaurantName,
            RestaurantDB.colRestaurantStore,
            RestaurantDB.colRestaurantAddress },
            new int[] { R.id.textView1, R.id.textView2, R.id.textView3 });
        }

    setListAdapter(adapter);

    }
}

As you can see from the codes, I try to pass the string strRestaurantName and use it in WHERE in the query, but it just doesn’t work! The log file says “no such column as strRestaurantName” but I don’t mean it as a column. I tried doQuery() instead of doQuery(strRestaurantName), but it gave the same error too. What should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:23:30+00:00Added an answer on June 15, 2026 at 11:23 pm

    You must wrap SQL Strings in quotes:

    "WHERE "+colRestaurantName+" = '" + strRestaurantName + "'"
    

    (I had this backwards at one point, sorry).


    Though really you should use parametrization to prevent SQL injection attacks. As in:

    return(db.getReadableDatabase().rawQuery(
        "SELECT "+colRestaurantID+" AS _id, "+colRestaurantName+", "+colRestaurantStore+", "+colRestaurantAddress+
        " FROM "+restaurantListTable+
        " WHERE "+colRestaurantStore+" = ?" + 
        " ORDER BY "+colRestaurantStore, 
        new String[] {strRestaurantName}));
    

    It seems that my private doQuery() can’t retrieve the value of strRestaurantName when the intent and bundles are placed inside onCreate().

    You have accidentally created two variables named strRestaurantName… You have the field variable and a local variable in onCreate(). Change this line:

    String strRestaurantName = extras.getString("RestaurantName");
    

    To this:

    strRestaurantName = extras.getString("RestaurantName");
    

    Now you will only have one strRestaurantName and it will have the appropriate value in doQuery().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Application that contains main Activity, Service and several Threads run on this
The main activity includes some variables with set values. I created a sub-activity with
Splash I have main activity UI startup operations that take between 5-10 seconds (that
I'm passing an object from activity to another. Then from that activity to another
I have a small application that launches a login activity as a sub activity
I need to have a class with one activity that is performed once per
This has been killing me for two days now. I have a main Activity
This has been killing me for two days now. I have a main Activity
I have an activity and an extended View class,that both use the same xml
Main Activity: private Button btnSubmit; private DataSource mDataSource; private Context mContext; @Override public void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.