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 8925765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:47:12+00:00 2026-06-15T07:47:12+00:00

I have large number of strings, approximately 15,000 that I stored in a SQLite

  • 0

I have large number of strings, approximately 15,000 that I stored in a SQLite database using the following code:

 void addKey(String key, String value, String table) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_KEY, key); // Contact Name
        values.put(KEY_VALUE, value); // Contact Phone

        // Inserting Row
        db.insert(table, null, values);
        db.close(); // Closing database connection

    }

And then i search through that database using the following method in order to pick out any strings that match the key im looking for:

public String searchKeyString(String key, String table){
        String rtn = "";
        Log.d("searchKeyString",table);

            // Select All Query
            String selectQuery = "SELECT  * FROM " + table;

            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    Log.d("searchKeyString","searching");

                    if(cursor.getString(1).equals(key)) 
                        rtn = rtn + "," + cursor.getString(2);
                } while (cursor.moveToNext());
            }
            cursor.close();
            db.close();
            Log.d("searchKeyString","finish search");

        return rtn;
    }

The goal is to do this in real time as the user is typing on the keep board so response time is key and the way it stands now it takes over a second to run through the search.

I considered reading all of the items into an array list initially and sorting through that which might be faster, but i thought an array list of that size might cause memory issues. What is the best way to search through these entries in my database?

  • 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-15T07:47:13+00:00Added an answer on June 15, 2026 at 7:47 am

    A couple of things you can do…

    • Change the return to a StringBuilder until the end.
    • Only use a readable version of the database (that’s probably not making much difference though)
    • Do not get a new instance of the database every time, keep it opened until you don’t need it anymore
    • Query for only what you need with the “WHERE” argument in the SQL query.

    See the code below with some changes:

    // move this somewhere else in your Activity or such
    SQLiteDatabase db = this.getReadableDatabase();
    
    public String searchKeyString(String key, String table){
        StringBuilder rtn = new StringBuilder();
        Log.d("searchKeyString",table);
    
            // Select All Query
            String selectQuery = "SELECT  * FROM " + table + " WHERE KEY_KEY=?";
    
            Cursor cursor = db.rawQuery(selectQuery,  new String[] {key});
            // you can change it to
            // db.rawQuery("SELECT * FROM "+table+" WHERE KEY_KEY LIKE ?", new String[] {key+"%"});
            // if you want to get everything starting with that key value
    
            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    Log.d("searchKeyString","searching");
    
                    rtn.append(",").append(cursor.getString(2));
                } while (cursor.moveToNext());
            }
            cursor.close();
            Log.d("searchKeyString","finish search");
    
        return rtn.toString();
    }
    

    Note even if you want this to happen in “real-time” for the user, you will still need to move this to a separate Thread or ASyncTask or you are going to run into problems….

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

Sidebar

Related Questions

I have a large number of records (10,000, increasing every day) that essentially is
I have a large number of strings that I need to store in a
I have an application that manages a large number of strings. Strings are in
I have a large number of objects which are identified by names (strings). So,
I have a large number of documents (mainly PDFs) that I want to index
I have a large number of csv files that look like this below: xxxxxxxx
We have a large number of programmers on different platforms all using CVS. We
I have a large number of C# WCF services that are being called by
We have a large number of Python bytearray strings of variable length which need
Suppose I have a large number of strings formatted something like: <tag>blah blahXXXXXblah blah</tag>

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.