如何根据用户名获取电子邮件地址

时间:2017-05-30 06:43:27

标签: android sqlite

我想在选择用户名时从Sqlite Data获取usermail我试过这段代码获取错误

      android.database.sqlite.SQLiteException: no such column: royal (code 1): , while compiling: SELECT userName, compMail FROM Records WHERE userName= royal

DBController.java

public String returnickname(long l) {
        // TODO Auto-generated method stub
        String[] coloumns = new String[] { COLUMN_NAME, COLUMN_TYPE,COLUMN_COMP_MAIL };//calling elements in an array
        Cursor c = db.query(DATABASE_NAME, coloumns,COLUMN_NAME + "=" + l, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
            String nickname = c.getString(2); // since name is  in position 2 ie 3rd coloumn
            return nickname;
        }
        return null;
    }

VehicleReceipt.java

 if (spinner.getId()== R.id.insu_comp_spinner) {

            String label = parent.getItemAtPosition(position).toString();


            long l=Long.parseLong(label);
            String returnedname=controller.returnickname(l);
            editICM.setText(returnedname);

我想通过微调选择器获取数据

例如:选择contact_no,其中name ='ram';

当我选择ram set rams contact_no来编辑文本时。

1 个答案:

答案 0 :(得分:0)

正如您发布的例外情况所说:

  

android.database.sqlite.SQLiteException:没有这样的列:royal(code 1)

如果在查询中放入String值而没有引号,则数据库会将其视为列名。

您的编译查询是:

SELECT userName, compMail FROM Records WHERE userName=royal

应该是:

SELECT userName, compMail FROM Records WHERE userName='royal'

将值换成引号'。尝试将您的查询更改为:

Cursor c = db.query(DATABASE_NAME, coloumns,COLUMN_NAME + "='" + l + "'", null, null, null, null);

这将使您的方法看起来像:

public String returnickname(long l) {
    // TODO Auto-generated method stub
    String[] coloumns = new String[] { COLUMN_NAME, COLUMN_TYPE,COLUMN_COMP_MAIL };//calling elements in an array
    Cursor c = db.query(DATABASE_NAME, coloumns,COLUMN_NAME + "='" + l + "'", null, null, null, null);
    if (c != null) {
        c.moveToFirst();
        String nickname = c.getString(2); // since name is  in position 2 ie 3rd coloumn
        return nickname;
    }
    return null;
}

您可以在此处阅读有关SQLite查询的基础知识:SQLite SELECT Query