I created code to create a SQLite database based on the article here.
The most pertinent code is:
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Platypus.db";
private static final String TABLE_VENDORS = "vendors";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_VENDORID = "vendorId";
public static final String COLUMN_COMPANYNAME = "companyName";
public SQLiteHandlerVendors(Context context, String vendor,
SQLiteDatabase.CursorFactory factory, int company) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_VENDORS_TABLE = "CREATE TABLE " +
TABLE_VENDORS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_VENDORID
+ " TEXT," + COLUMN_COMPANYNAME + " TEXT" + ")";
db.execSQL(CREATE_VENDORS_TABLE);
}
public void addVendor(Vendor vendor) {
ContentValues values = new ContentValues();
values.put(COLUMN_VENDORID, vendor.getVendorId());
values.put(COLUMN_COMPANYNAME, vendor.getCompanyName());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_VENDORS, null, values);
db.close();
}
Yet, although the insertions of data seem to be working -- the calls to addVendor()
run without any exception being thrown with the following code:
protected void onPostExecute(String result) {
try {
JSONArray jsonArr = new JSONArray(result);
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
String vendorId = jsonObj.getString("vendorId");
String companyName = jsonObj.getString("companyName");
Log.i("vendorId", vendorId);
Log.i("companyName", companyName);
// Prepare for writing to db
Vendor vend = new Vendor();
vend.setVendorId(vendorId);
vend.setCompanyName(companyName);
SQLiteHandlerVendors sqliteHandler = new SQLiteHandlerVendors(MainActivity.this, null, null, 1);
sqliteHandler.addVendor(vend);
}
} catch (JSONException j) {
System.out.println(j.getMessage());
Log.i("jsonEx", j.getMessage());
}
Log.i("GetVendorsTask.FromOnPostExecute", result);
}
I cannot locate the Platypus.db file that should have been created.
This article says, "If your application creates a database, this database is by default saved in the directory DATA/data/APP_NAME/databases/FILENAME."
A global search for "Platypus.DB" on my hard drive turns up nothing. Is the database created not being persisted, or is there some other reason why I'm not finding it?
I tried to find it by adding this code:
Log.i("Data Dir", Environment.getDataDirectory().toString());
...and when LogCat gives me is:
04-07 12:36:47.133 1089-1089/platypus.app I/Data Dir﹕ /data
There is no "data" folder in C:\Users\clay\AndroidStudioProjects\Platypus, nor is there one in the root of my hard drive (*C:*).
Can the data dir/file only be viewed in some virtual space, in a file system for the emulator or so? If so, how can I actually access the emulator's file system to view this data?
UPDATE
Okay, I opened up this view, and navigated to data/data/hhs (the app's name is really "hhs" not "platypus"):
...but still do not see HHS.DB (I see no "databases" folder below all that)...
UPDATE 2
If I select the "<=" button ("Pull a file from the device") with the file with the size of 16384 highlighted, it does open the "Get Device File" dialog with a filename of "HHS.DB" (just what I was hoping to find).
But, why is its path "data\data\hhs\unnamed folder\unnamed file" instead of "data\data\hhs\databases\HHS.DB"?
The other file (size 8720) is HHS.db-journal
UPDATE 3
After saving those files to my hard drive and downloading SQLite Data Browser, I'm able to verify that the records actually were written to the table:
SQLite Data Browser is a great tool for just such a need!
UPDATE 4
I don't know what changed/how it changed, but it's now working as it should: