Suji
Suji

Reputation: 1

insert query in sqlite in android

db2.execSQL("Create Table StudentElective1(studentid text UNIQUE,
    elective1 text ,elective2 text ,year text)");
if ((db2.insert("StudentElective1", null, values))!=-1) {
    Toast.makeText(eigth.this, "Elective Successfully Selected", 2000).show();
} else {
    Toast.makeText(eigth.this,
         "Insert Error,Elective already selected", 2000).show();
}

how to specify the where condition in the insert query??

Upvotes: 0

Views: 1618

Answers (3)

CoderDecoder
CoderDecoder

Reputation: 445

All the keys are previously predefined based on the column names in the table.. Here is the insert query..id is passed value on what basis the result will be shown..

 String areaTyp = "SELECT " + AREA_TYPE + "  FROM "
            + AREA_TYPE_TABLE + " where `" + TYPE + "`="
            + id;

Upvotes: 1

Boris Strandjev
Boris Strandjev

Reputation: 46963

What do you mean where condition. This might make sense for updates, but inserts require all the values specified. If you mean that you want to select some of the values to insert from selects, the only way to construct that complex a query in Android is by first making the selects and then using their results.

EDIT Now it is obvious you need update. Here is an example of how you do update in Android:

int yourId = 42; //randomly chosen. you must provide this
String tableName = "StudentElective1";
String whereClause = "_id=?";
String [] whereArgs = { String.valueOf(your_id) };
db2.insert(tableName, values, whereClause, whereArgs);

Here I do a where on the _id column, but you can do it on whichever you want. Note that I do not specify the parameter in the whereClause straight away, instead I use the fourth parameter of the update method: it adds the parameters on the places of the question marks in the where clause and does some escaping. This is the correct way to do it.

Upvotes: 0

Android Killer
Android Killer

Reputation: 18509

you cant Insert where clause in insert use update instead.

Upvotes: 0

Related Questions