Skip to content

Instantly share code, notes, and snippets.

@rjlutz
Last active June 1, 2020 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjlutz/96c7255d90cc5f8094e90b19e43a39c3 to your computer and use it in GitHub Desktop.
Save rjlutz/96c7255d90cc5f8094e90b19e43a39c3 to your computer and use it in GitHub Desktop.
WordList DB Helper Code Fragments
// remember to adjust the package hierarchy so that it matches you project
import static edu.ggc.lutz.sqlprototype.WordListContract.WordListEntry.COLUMN_NAME_CONTENT;
// TODO write out to SQL
WordListDbHelper dbHelper = new WordListDbHelper(getApplicationContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();// Gets the data repo in write mode
db.delete("wordlist", null, null); // Wipe DB
ContentValues values = new ContentValues(); // Create a new map of values, where column names are the keys
for (DummyContent.DummyItem item : DummyContent.ITEMS) {
values.put(COLUMN_NAME_CONTENT, item.content);
db.insert("wordlist", null, values); // Insert the new row, which can return the primary key of new row
}
Cursor mCount = db.rawQuery("select count(*) from wordlist", null);
mCount.moveToFirst();
Log.v("ListPersistSQLPrototype", "written = " + mCount.getInt(0));
mCount.close();
db.close();
// TODO
WordListDbHelper dbHelper = new WordListDbHelper(ItemListActivity.this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c = db.rawQuery("select * from wordlist",null);
if (c.getCount() > 0)
DummyContent.ITEMS.clear(); // scorch the earth!
while (c.moveToNext()) {
String content = c.getString(c.getColumnIndex(COLUMN_NAME_CONTENT));
DummyContent.addItem(new DummyContent.DummyItem("0", content, ""));
}
Log.i("ListPersistSQLPrototype", "read in and added: " + c.getCount());
c.close();
db.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment