簡體   English   中英

在Android的SQLite中插入查詢

[英]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();
}

如何在插入查詢中指定where條件?

所有鍵都是基於表中的列名預先定義的。這是插入查詢。.id根據顯示結果的基礎傳遞值。

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

你是什​​么意思條件。 這對於更新可能有意義,但是插入需要指定的所有值。 如果您想從選擇中選擇要插入的某些值,那么在Android中構造該復雜查詢的唯一方法是首先進行選擇,然后使用其結果。

編輯現在很明顯,您需要更新。 這是一個如何在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);

在這里,我在_id列上執行了一個操作,但是您可以在任意位置執行操作。 請注意,我沒有立即在whereClause指定參數,而是使用了update方法的第四個參數:它將參數添加到where子句中問號的位置,並進行一些轉義。 這是正確的方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM