Note that there are some explanatory texts on larger screens.

plurals
  1. POSQLite Android - how to select or check for a row
    primarykey
    data
    text
    <p>So im new to working with SQlite and android, I have started out by creating a DataBase Manager class as seen below:</p> <pre><code>import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DataBaseManager extends SQLiteOpenHelper{ static final String dbName ="LCInstore"; static final String allIcons = "Icons"; static final String homeIcons = "HomeScreenIcons"; static final String colIconID = "IconID"; static final String colID = "ID"; static final String colImage = "IconImage"; static final String colLabel = "IconLabel"; static final String colName = "IconName"; public DataBaseManager(Context context) { super(context, dbName, null, 0); // starting value is zero must change on upgrades // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub // Create Icon Table if does not exist db.execSQL("CREATE TABLE IF NOT CREATED"+ allIcons +"" + "("+colIconID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ colName + " TEXT," + colImage + " TEXT," + colLabel + "TEXT)"); // Create HomeScreen Icons Table if does not exist db.execSQL("CREATE TABLE IF NOT EXIST" + homeIcons+"" + "("+colID+" INTEGER NOT NULL, FOREIGN KEY ("+colID+") REFERENCES" + ""+allIcons+" ("+colIconID+"));"); InsertIcons(db); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS "+allIcons); } private void InsertIcons(SQLiteDatabase db) { ContentValues cv=new ContentValues(); cv.put(colIconID, 1); cv.put(colName, "Icon1"); cv.put(colImage, "icon_one"); cv.put(colLabel, "ONE"); db.insert(allIcons, colIconID, cv); cv.put(colIconID, 2); cv.put(colName, "Icon2"); cv.put(colImage, "icon_two"); cv.put(colLabel, "TWO"); db.insert(allIcons, colIconID, cv); cv.put(colIconID, 3); cv.put(colName, "Icon3"); cv.put(colImage, "icon_three"); cv.put(colLabel, "THREE"); db.insert(allIcons, colIconID, cv); db.close(); } public void AddHomeScreenIcon(int id){ SQLiteDatabase db=this.getWritableDatabase(); //HELP // Need to check if id already exists in colID of homeIcons TABLE // and if it doesn't do this Cursor c = db.rawQuery("select * from "+ homeIcons +" where colID = " +id, null); int numFound = c.getCount(); if(numFound&lt;1){ ContentValues cv=new ContentValues(); cv.put(colID, id); db.insert(homeIcons, colID, cv); } // else do nothing } public void DeleteHomeScreenIcon (int id){ SQLiteDatabase db=this.getWritableDatabase(); db.delete(homeIcons, "id = " +id, null); // HELP //Need to find row of homeIcons table that matches id and delete it db.close(); } } </code></pre> <p>you'll notice in my last two methods i have put //HELP those comments are what i need help with.. </p> <p>ANONTHER QUESTION: if later i update my app, i assume it will not get rid of the DB or make any changes to it.. but how does the onUpgrade get called.. and how do i control that in upgrades?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload