vendredi 27 mars 2015

Insert values from one table to another using execSQL (Android)

I am currently trying to fill several smaller tables using a much larger table. The user fills out their profile on a separate fragment, then when he hits create, values from a large database of food, will be pulled and placed into separate day meals.


The database is read in through assets using sqliteAssetHelper in a separate activity. I have tried these two methods from what i have researched but they don't change the tables.



//db.execSQL("INSERT INTO fstMeal_1 (fName, Calories, Protein, Carbs) VALUES (?,?, ?, ?)", new Object[]{"Pork", 1, 1, 1});

//String ROW1 = "INSERT INTO " + "fstMeal_1" + " ("
// + "fName" + ", " + "Calories" + ", "
// + "Protein" + ", " + "Carbs" + ") Values ('Pork', '1', '1', '0' )";
//db.execSQL(ROW1);


I was currently testing the code to just see if i could insert into a table before trying to use other table values, but with no results. The new values should show up in a fragment that has the meal plan on it. I had these attempts place in the database class like this. I am not sure where they are suppose to go.



public Cursor getMeal_1_day1() {

SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

String [] sqlProject = {"0 _id", "fName", "Calories", "Protein", "Carbs"};
String sqlTables = "fstMeal_1";

qb.setTables(sqlTables);
//db.execSQL("INSERT INTO fstMeal_1 (fName, Calories, Protein, Carbs) VALUES (?,?, ?, ?)", new Object[]{"Pork", 1, 1, 1});

//String ROW1 = "INSERT INTO " + "fstMeal_1" + " ("
// + "fName" + ", " + "Calories" + ", "
// + "Protein" + ", " + "Carbs" + ") Values ('Pork', '1', '1', '0' )";
//db.execSQL(ROW1);

Cursor c = qb.query(db, sqlProject, null, null,
null, null, null);

c.moveToFirst();
return c;


I saw some suggestions to use insert, but I don't understand how it works. Am i missing some code or should this info be placed in the main activity?


Aucun commentaire:

Enregistrer un commentaire