【发布时间】: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 并给你这个错误信息。