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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:51:16+00:00 2026-05-29T20:51:16+00:00

Currently I’m using ContentProvider in my application. Because of layers and no actual need

  • 0

Currently I’m using ContentProvider in my application. Because of “layers” and no actual need for provider – I’m working on optimizing data access as much as possible. Here is my attempt to do this:

public static String getPreferenceString(Context context, String key)
    {
        DatabaseHelper helper = new DatabaseHelper(context);
        SQLiteDatabase database = helper.getReadableDatabase();
        SQLiteStatement statement = database.compileStatement("SELECT Value FROM Preferences WHERE Key='" + key + "' LIMIT 1");

        try
        {
            return statement.simpleQueryForString();

        }
        catch (Exception ex)
        {
            return "";
        }
        finally
        {
            statement.close();
            database.close();
            helper.close();
        }
    }

    public static void setPreferenceString(Context context, String key, String value)
    {
        DatabaseHelper helper = new DatabaseHelper(context);
        SQLiteDatabase database = helper.getReadableDatabase();
        SQLiteStatement statement = database.compileStatement("INSERT OR REPLACE INTO Preferences (Key, UpdatedOn, Value) VALUES ('" +
                key + "', '" +
                Utility.getDateConvertedToUTCDBString(new Date()) + "', '" +
                value + "'); ");
        try
        {
            statement.execute();
        }
        finally
        {
            statement.close();
            database.close();
            helper.close();
        }
    }
  1. Is that about as close as I can get to direct calls to SQLite?
  2. Should I have all this .close() statements in my code?
  3. In setPreferenceString I did copy/paste and called getReadableDatabase even though I write data and it works. Why?
  • 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-05-29T20:51:18+00:00Added an answer on May 29, 2026 at 8:51 pm

    Is that about as close as I can get to direct calls to SQLite?

    AFAIK SQL queries are closest you can go against RDBs

    Should I have all this .close() statements in my code?

    Personally, I would not create a DatabaseHelper, an SQLiteDatabase, and an SQLiteStatement each time I call that method. I would create all this just before you need them, and close them when no needed anymore. Also centralizing this is a good idea IMHO (using a singleton, for example).

    Also your SQL statement could be written like

    SELECT Value FROM Preferences WHERE Key= ? LIMIT 1
    

    This way you only have to prepare it once and bind parameters as you need the statement. Same goes for any SQL query.

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

Sidebar

Related Questions

Currently I'm using the Jackson JSON Processor to write preference data and whatnot to
Currently writing a C# application it should do backups using GIT in the background.
Currently I am working in message compose screen in Android, Using Intent to show
currently i am working with Awsomnium 1.7 in the C# environment. I'm just using
Currently, I am writing a MiddleWare application that synchronizes information between and accounting application
Currently I am using HTML files for parts of my user interface. I display
currently, I`m implementing a map App with Mono4Droid and there I`m using a WebView
Currently, I'm working on a project where I have a server - client relationship
Currently working with converting SQLException error messages into messages that are more useful for
Currently i have a node.js and socket.io application in development on my local machine

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.