Note that there are some explanatory texts on larger screens.

plurals
  1. POMost efficient way to access/write data to SQLite on Android
    text
    copied!<p>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:</p> <pre><code>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(); } } </code></pre> <ol> <li>Is that about as close as I can get to direct calls to SQLite? </li> <li>Should I have all this <code>.close()</code> statements in my code? </li> <li>In <code>setPreferenceString</code> I did copy/paste and called <code>getReadableDatabase</code> even though I write data and it works. Why?</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload