【问题标题】:No such table exception when executing SQLite in Android在Android中执行SQLite时没有这样的表异常
【发布时间】:2015-06-07 05:53:01
【问题描述】:

当我运行我的应用并按下一个按钮时,它会崩溃并出现以下异常:

android.database.sqlite.SQLiteException: no such table: LOGIN_TABLE_NAME (code 1): , while compile: 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();
            }

【问题讨论】:

  • 你错误地写了"LOGIN_TABLE_NAME"而不是LOGIN_TABLE_NAME,其他类似的变量也是如此。
  • 您的表名是 LOGIN_TABLE_NAME 但您使用了存储在变量LOGIN_TABLE_NAME 中的数据,根据您的程序是"Login"。所以数据库找不到表 Login 并给你这个错误信息。

标签: android sqlite


【解决方案1】:

就像其他人提到的那样,这就是您的错误的来源:

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

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

【讨论】:

    猜你喜欢
    • 2012-04-12
    • 2015-04-21
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多