Reputation: 33
I have an xml file as shown in the picture. I want two things, first, I want to insert pre-defined values in my sqlite database on onCreate method using an arraylist (or any method). Then secondly, I want to use those values from the arraylist, now in my database, to populate the two spinners(Producer and Product).
I have a database file that handles the db connections and methods and this is what I have done so far. This code is inside my onCreate method
db.execSQL("CREATE TABLE " + TABLE_PRODUCT + "("
+ PRODUCT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + PRODUCT_NAME + " TEXT,"
+ PRODUCER_FK + " INTEGER" + " FOREIGN KEY(PRODUCER_FK) REFERENCES TABLE_PRODUCER(PRODUCER_ID))");
ContentValues contentv=new ContentValues();
contentv.put("name", "Soya Seed");
getWritableDatabase().insert("TABLE_PRODUCT", null, contentv);
db.execSQL("CREATE TABLE " + TABLE_PRODUCER + "("
+ PRODUCER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + PRODUCER_NAME + " TEXT,"
+ PRODUCER_ADDRESS + " TEXT,"
+ PRODUCER_PHONE + " TEXT UNIQUE," + PRODUCER_EMAIL + " TEXT,"
+ CONTACT_PERSON + " TEXT" + ")");
ContentValues cv=new ContentValues();
cv.put("name", "ZamSEED");
cv.put("address", "Luanda");
cv.put("phone", "+244977654321");
cv.put("email", "[email protected]");
cv.put("contact_name", "Mr. Tembo");
getWritableDatabase().insert("TABLE_PRODUCER", null, cv);
Then I have an inventory file that reads from the database to get values from the product and producers tables to populate the spinners.
My challenge is how can I tell if my db.execSQL is actually successful? And how would I use an arraylist to insert values in the database?
NOTE: the app runs fine when I ran it but I am not sure if the tables are created and values inserted. Am a newbie to Android programming, thanks.
Upvotes: 0
Views: 1611
Reputation: 33
After some time of research into this, I found this article here that led me to see the actual tables of my db.execSQL command and the data contained. In Ubuntu
:
cd /path/to/my/sdk/platform-tools
./adb shell
run-as <app package name>
cd /data/data/<app package name>/databases
ls
chmod 666 <database file name>
sqlite3 <database file name>
> (semi-colon terminated commands can be run here to query the data)
> .exit
(copy full database path)
exit
Then use sqlite3 commands to work on database > (semi-colon terminated commands can be run here to query the data)
Upvotes: 1
Reputation: 152827
My challenge is how can I tell if my db.execSQL is actually successful?
It was successful if it did not throw an exception.
However calling getWritableDatabase()
in onCreate()
is an error since that results in recursion: getWritableDatabase()
triggers a call to onCreate()
in the first place. You should use the SQLiteDatabase
passed to you as a param instead.
If you did not see an exception about the recursive call then you already had a database file with the correct version. You can uninstall your app to clean up its data and then install and run it again to make onCreate()
run again.
And how would I use an arraylist to insert values in the database?
Use a loop to iterate the list and insert values one by one.
Upvotes: 0
Reputation: 1
DatabaseHelper.java
public ArrayList<String>getProductList(){
ArrayList<String>products = new ArrayList<String>();
SQLiteDatabase qDB = getReadableDatabase();
String columns[]=new String[]{PRODUCT_NAME};
Cursor cursor = qDB.query(TABLE_PRODUCT,columns,null,null,null,null,null);
while (cursor.moveToNext()){
String produectName = cursor.getString(1);
products.add(productName);
Log.e("e",products);
}
return products;
}
SomeActivity.java
spinnerProducts = (Spinner) findViewById(R.id.spinnerProducts);
ArrayList<String>productsList = new DatabaseHelper(context).getProductsList();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,productsList);
spinnerProducts.setAdapter(arrayAdapter);
...
//same logic for Producers list
Upvotes: 0