简体   繁体   English

如何在SQLite Android中更新表

[英]How to update table in sqlite android

In my app i want to update my database table based on two column.Means update salary where firstname="ekant" and last name="kancha" .So can any body plz tell me what will be the query i have to write. 在我的应用程序中,我想基于两列更新我的数据库表。方法更新了薪水,其中firstname="ekant"last name="kancha" 。所以任何机构请告诉我我要写的查询是什么。

public int updateStatus(int salary,String fname,String lName)
{
    ContentValues cv=new ContentValues();

    String where = fname+ "=" + "ekanta";
    cv.put("salary",salary);
    return sdb.update(DATABASE_TABLENAME, cv, where, null);

}

this code works only when i want to update based on first name..But i want to update based on firstname and lastname. 此代码仅在我想基于名字更新时有效。.但是我想基于名字和姓氏更新。

plz help me.thanx 请帮助我

Use placeholders . 使用占位符 This makes it easier to read the SQL query and protects against SQL Injection (accidental or otherwise). 这样可以更轻松地读取 SQL查询,并防止SQL注入 (意外或其他)。

public int updateSalary (int salary, String fname, String lName)
{
    ContentValues cv = new ContentValues();
    cv.put("salary", salary);

                 /* use COLUMN NAMES here */                     
    String where = "firstname = ? and lastname = ?";
                 /* bind VALUES here */
    String[] whereArgs = new { fname, lname };
    return sdb.update(DATABASE_TABLENAME, cv, where, whereArgs);
}

If you have constants (eg private final static COLUMN_FNAME = "firstname" ) for the COLUMN NAMES, then you can build where using these constants. 如果您有用于COLUMN NAMES的常量(例如, private final static COLUMN_FNAME = "firstname" ),则可以使用这些常量where构建。

However, do not put VALUES in the where string. 但是, 请勿将VALUES放在where字符串中。 Instead, use ? 而是使用? and supply any VALUES via the whereArgs array as per the above example. 并按照上述示例通过whereArgs数组提供任何值。


Also, it is possible for people (even within the same organization) to share the same first name and last name. 同样,人们(甚至在同一组织内)也可以共享相同的名字和姓氏。 Basing the database queries/updates around such a pairing will break in such cases so it may be prudent to work on designing the API to work with a better record identifier. 在这种情况下,基于数据库查询/更新的配对将中断 ,因此,谨慎设计API以与更好的记录标识符一起使用可能是明智的。

用这个...

 String where = fname+ "=" + "ekanta" + " and " + lname + "=" + "your lastname";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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