是什么导致以下Java代码中的错误

时间:2019-04-15 21:07:22

标签: java android sqlite

我已经实现了一种针对SQLite DB验证用户的方法,但是单击登录按钮时,程序始终引发错误,错误代码如下

  

2019-04-15 21:38:27.455 4601-4601 / com.example.mymedicare   E / AndroidRuntime:致命异常:main       流程:com.example.mymedicare,PID:4601       android.database.sqlite.SQLiteException:无此类列:Brad(代码1):,而在编译时:SELECT * FROM users WHERE Brad =?和   Brad1994 =?           在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native   方法)           在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)           在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)           在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)           在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)           在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37)           在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)           在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)           在android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)           在com.example.mymedicare.MyDBHelper.checkLogin(MyDBHelper.java:96)

我已经尝试读取错误日志,但是找不到任何严重错误,我的方法将显示在下面

  // This is my check login method in mydbhelper   

   public boolean checkLogin(String username, String password) {
    SQLiteDatabase db = this.getWritableDatabase();

    String s;
    Cursor c = db.rawQuery("SELECT * FROM users WHERE " + username + " =? AND " + password + " =?", null);

    if (c.getCount() <= 0) {
        c.close();
        db.close();
        return false;
    } else {
        c.close();
        db.close();
        return true;
    }
}


   //This is LoggingIn activity where I have tried to implement the above 
   method 

   public class LoggingIn extends AppCompatActivity {

//defines page objects
private Button SignIn;
private EditText Usernames;
private EditText Passwords;
private MyDBHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_logging_in);

    //Finds objects by ID
     //edit text to string
    Usernames = (EditText) findViewById(R.id.Usernames);

    Passwords = (EditText) findViewById(R.id.Passwords);


    Button SignIn = (Button) findViewById(R.id.SignIn);
    db = new MyDBHelper(this);

    SignIn.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            Boolean validUser = 
     db.checkLogin(Usernames.getText().toString(), 
     Passwords.getText().toString());

            if (validUser == true) {
                Intent i = new Intent(getApplicationContext(), 
                HomePage.class);
                startActivity(i);
            } else {
                Toast.makeText(getApplicationContext(), "Invalid login", 
                Toast.LENGTH_SHORT).show();
            }

        }
    });
}
}

2 个答案:

答案 0 :(得分:2)

您正在传递参数usernamepassword作为列名。
正确的方法是:

Cursor c = db.rawQuery(
    "SELECT * FROM users WHERE usenamecolumn = ? AND passwordcolumn = ?", 
     new String[] {username, password});

usenamecolumnpasswordcolumn更改为表的列名,以获取用户名和密码。

答案 1 :(得分:0)

您可能想要更接近此的东西

const arr = [
   {  
     "name":"BMW",
     "price":"55 000",
     "country":"Germany",
     "certificate":"yes"
  },
  {  
    "name":"Mitsubishi",
    "price":"93 000", 
    "constructor":"Bar John",
    "door":"3",
    "country":"Japan",
  },
  {  
    "name":"TOYOTA", 
    "price":"48 000", 
    "max_people":"7",
    "country":"Japan",
    "certificate":"yes"
  },
  {  
    "name":"Volkswagen",
    "price":"36 000", 
    "constructor":"Pier Sun",
    "country":"Germany",
    "certificate":"no"
  },
 ];
function pushArr (arr, item){
    arr.push(item); 
}

let Cars = pushArr(arr,{  
  "name":"Audi",
  "price":"89 000", 
  "constructor":"Hubert Trumpius",
  "country":"Germany",
  "certificate":"yes"
});

console.log(arr);

按照上面的编写方式,您尝试使用搜索条件(值)作为列名(键)来执行查询。

例如用简单的英语来说,原始语句的第一部分显示“在Brad列中找到内容”,这就是为什么您的异常会弹出“ SQLiteException:无此类列:Brad”的原因

您实际上想说的是“在列用户名中找到Brad”,这就是我在上面添加的修订后的语句应该为您所做的(未经测试,但在正确的方法上可以让您前进)。

我发现了另外一个StackOverflow帖子,它也可能有助于创建准备好的语句-> How do I use prepared statements in SQlite in Android?

希望这会有所帮助。