在Android中执行SQLite时没有这样的表异常

时间:2015-06-07 05:53:01

标签: android sqlite

当我运行我的应用程序并按下按钮时,它会因此例外而崩溃:

  

android.database.sqlite.SQLiteException:没有这样的表:LOGIN_TABLE_NAME(代码1):,同时编译:SELECT * FROM LOGIN_TABLE_NAME WHERE A_NAME =?       在com.example.resturantms.Login $ 2.onClick(Login.java:69)

这是我的代码:

### database class ###
public class DBhandle {
//table name
private static final String LOGIN_TABLE_NAME = "Login";

//login table column name
public static final String KEY_ROWID= "ID";
public static final String A_NAME = "admin_name";
public static final String A_PHONE = "admin_phone";

db.execSQL("CREATE TABLE " + LOGIN_TABLE_NAME + " (" + KEY_ROWID
                        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + A_NAME
                        + " TEXT NOT NULL, " + A_PHONE + " TEXT NOT NULL);"
                        );

public String getSingleEntry(String userName)
{
    Cursor cursor=ourDatabase.query("LOGIN_TABLE_NAME", null, " A_NAME=?", new String[]{userName}, null, null, null);
    if(cursor.getCount()<1) // UserName Not Exist
    {
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String password= cursor.getString(cursor.getColumnIndex("A_PHONE"));
    cursor.close();
    return password;                
}


### login class ###
@Override
public void onClick(View arg0) {
    // get user name password
    String userName=editTextUserNameToLogin.getText().toString();
    String password=editTextPasswordToLogin.getText().toString();

    // fetch the Password form database for respective user name
    String storedPass=dbhandle.getSingleEntry(userName);

// check if the Stored password matches with  Password entered by user
    if(password.equals(storedPass))
     {
     Toast.makeText(Login.this, "login successfull", Toast.LENGTH_LONG).show();}
            else
            {
                Toast.makeText(Login.this, "wrong username or password", Toast.LENGTH_LONG).show();
            }

1 个答案:

答案 0 :(得分:0)

就像其他人提到的那样,这就是你的错误所在:

Cursor cursor=ourDatabase.query("LOGIN_TABLE_NAME", null, " A_NAME=?", new String[]{userName}, null, null, null);

第一个参数应该是您的表名(Login),存储在名为LOGIN_TABLE_NAME的变量中,而不是字符串"Login_TABLE_NAME"。长话短说,删除引号。