Last active
June 1, 2020 16:55
-
-
Save rjlutz/96c7255d90cc5f8094e90b19e43a39c3 to your computer and use it in GitHub Desktop.
WordList DB Helper Code Fragments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// remember to adjust the package hierarchy so that it matches you project | |
import static edu.ggc.lutz.sqlprototype.WordListContract.WordListEntry.COLUMN_NAME_CONTENT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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