查询表有问题:没有这样的列

时间:2015-12-14 06:38:54

标签: java android sqlite

这是日志猫:

  

引起:android.database.sqlite.SQLiteException:没有这样的列:uname(代码1):,同时编译:select uname,pass   fromaccounts

这是我在SqliteDatabase Helper中的代码:

public String searchPass(String uname)
{
    db = this.getReadableDatabase();
    String query = "select uname , pass from"+TABLE_NAME;
    Cursor cursor = db.rawQuery(query , null);
    String a, b;
    b = "not found";
    if (cursor.moveToFirst())
    {
        do {
            a = cursor.getString(0);
            b = cursor.getString(1);

            if (a.equals(uname))
            {
                b = cursor.getString(1);
                break;
            }
        }
        while (cursor.moveToNext());
    }
    return b;
}

这些是我的专栏:

     private static final int DATABASE_VERSION = 1;
     private static final String DATABASE_NAME = "echo_all.db";
     private static final String TABLE_NAME = "accounts";
     private static final String COLUMN_ID = "id";
     private static final String COLUMN_FIRST_NAME = "fname";
     private static final String COLUMN_LAST_NAME = "lname";
     private static final String COLUMN_USERNAME = "uname";
     private static final String COLUMN_EMAIL = "email";
     private static final String COLUMN_PASS = "pass";
     SQLiteDatabase db;
     private static final String TABLE_CREATE = "create table accounts (id integer primary key not null ," +
        "fname text not null , lname text not null , uname text not null , email text not null , pass text not null );";

我在编码解决日志cat错误时需要帮助“select uname,pass from”+ TABLE_NAME;它在log cat中标记为错误。

3 个答案:

答案 0 :(得分:4)

您在SQL中遇到问题。 来自 TABLE_NAME

之间应该有一个空格

而不是

String query = "select uname , pass from" + TABLE_NAME;

使用

String query = "select uname , pass from " + TABLE_NAME;

答案 1 :(得分:0)

您错过了from和TABLE_NAME之间的空白区域。使用此代码使其有效:

String query = "select uname, pass from " + TABLE_NAME;

答案 2 :(得分:0)

请不要使用原始查询,这些是错误和漏洞的来源。请使用为您提供的Android API。

Cursor c = db.query(TABLE_NAME, new String[]{uname , pass}, ...);