简体   繁体   中英

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??

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;

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.

您不能在插入中的插入where子句中使用update。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM