【问题标题】:String Where condition in SQLiteSQLite 中的字符串 Where 条件
【发布时间】:2014-02-09 14:46:41
【问题描述】:

我正在开发一个 Android 应用程序,我希望在我的 SQL 查询中的 WHERE 条件中有两个单独的字符串。

这是我的代码:

       String query=" SELECT * FROM "+USER_TABLE+" where Username=? COLLATE NOCASE and Password=?";
    Cursor logincursor=   database.rawQuery(query, new String[]{username,password})
if (logincursor.moveToFirst()) {

            user.UserName = logincursor.getString(logincursor
                    .getColumnIndex("Username"));
            user.Password =new EncryptDecrypt().decrypt(logincursor.getString(logincursor
                    .getColumnIndex("Password")));
            user.FullName = logincursor.getString(logincursor
                    .getColumnIndex("Fullname"));
            user.UserID = logincursor.getInt(logincursor
                    .getColumnIndex("UserID"));
            user.EmployeeID = logincursor.getInt(logincursor
                    .getColumnIndex("EmployeeID"));

        }

请提出一个在我的 Android 应用中完成此任务的好方法。

【问题讨论】:

  • 当我使用字符串作为 where 条件时,我没有从我的应用程序中获取任何值
  • 考虑在问题中添加相关代码。您发布的 SQL 没有任何问题。
  • @laalto 我已根据您的评论更改了我的问题...
  • 仍然没有什么可以证明这个问题。至少你没有使用来自rawQuery()Cursor 返回值。
  • 如果你没有得到任何数据,where 条件不匹配任何行...还有一些实际信息可以重现问题(例如,示例数据和参数) .但是,密码看起来很可疑,因为它似乎在数据库中被加密,并且在传递给查询时可能没有加密。

标签: android sql sqlite android-sqlite


【解决方案1】:

使用下面的查询。

db.rawQuery("select * from login_table where username =" + "\""+ username.trim() + "\""+" and password="+ "\""+ password.trim() + "\""+, null);

db.query(USER_TABLE,  new String[] {"*"}," Username = ? and Password =?", new String[]{username,password}, null, null, null);

【讨论】:

  • 如果用户名是" OR 1=1; :)
猜你喜欢
  • 2021-07-07
  • 2021-12-09
  • 2016-06-09
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 2015-06-12
  • 2021-12-11
  • 1970-01-01
相关资源
最近更新 更多