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

  • SEARCH
  • Home
  • 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 8108921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:15:35+00:00 2026-06-06T01:15:35+00:00

Possible Duplicate: Fetch data in database table insert it if not exist else return

  • 0

Possible Duplicate:
Fetch data in database table insert it if not exist else return the row id

I try to fetch if data exist in database table, if yes I should get the row id else I insert it in the table this my code

public int finddate(String date){
    int id=0;
    AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(this);
    SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj.getWritableDatabase();
    Cursor cursor = sqliteDatabase.rawQuery("SELECT _id FROM " + 
                            AndroidOpenDbHelper.TABLE_DATE + " where " +
                            AndroidOpenDbHelper.COLUMN_NAME_DATE + " = " + date,
                            null);
    startManagingCursor(cursor);
    if (cursor != null) { 
        if( cursor.moveToFirst() ) {
            id = cursor.getInt(cursor.getColumnIndex("_id"));
        }
    } else { 
        id=0;
    }
    sqliteDatabase.close(); 
    return id;          
}

when i try with an existing item I get the result of this method =0 and the item is inserted in the table,
how can I do this?

  • 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-06T01:15:37+00:00Added an answer on June 6, 2026 at 1:15 am

    The date is not being correctly escaped. The safest and cleanest way to resolve it is to pass the date value as a parameter, rather than embedding it in the query.

    You can also tidy up the cursor management quite a bit. You don’t need to check for a null cursor, as an exception will be thrown if the query fails. Also, you don’t need to set id = 0 as you’ve already done that at the start. This is much simpler:

    Cursor cursor = sqliteDatabase.rawQuery("SELECT _id FROM " + 
                            AndroidOpenDbHelper.TABLE_DATE + " where " +
                            AndroidOpenDbHelper.COLUMN_NAME_DATE + " = ?",
                            new String[] { date } );
    
    if ( cursor.moveToFirst() ) {
        id = cursor.getInt(cursor.getColumnIndex("_id"));
    }
    
    cursor.close();
    sqliteDatabase.close(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: how to delete item from listView? I fetch data from Database in
Possible Duplicate: insert contacts into database but does not want to duplicate already existing
Possible Duplicate: SQL - find records from one table which don't exist in another
Possible Duplicate: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result I'm getting
Possible Duplicate: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result I am
Possible Duplicate: Difference of create Index by using include column or not using Edit:
Possible Duplicate: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result what is
Possible Duplicate: jquery/ajax load new content when available i have a table named news
Possible Duplicate: Sending and Parsing JSON in Android I am fetching JSON Data from
Possible Duplicate: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result okay so

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.