android.database.sqlite.SQLiteException:无此类列:user_id(代码1 SQLITE_ERROR): [英] android.database.sqlite.SQLiteException: no such column: user_id (code 1 SQLITE_ERROR):
问题描述
作为一名Android Studio初学者,我会遇到很多我无法发现的小错误,请让我知道我在做错什么,因为我以某种方式尝试了所有其他答案,但并没有得到更好的解决: )
being an Android Studio beginner i tend to get lots and lots of tiny errors i cannot find out, please let me know what I am doing wrong because I've tried all the other answers somehow and it doesn't get better :)
public class DatabaseHelper extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "UserManager.db";
private static final String TABLE_USER = "user";
private static final String COLUMN_USER_ID= "user_id";
private static final String COLUMN_USER_NAME= "user_name";
private static final String COLUMN_USER_EMAIL= "user_email";
private static final String COLUMN_USER_PASSWORD= "user_password";
private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "(" +
COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_USER_NAME + " TEXT," + COLUMN_USER_EMAIL + " TEXT," +
COLUMN_USER_PASSWORD + " TEXT" + ")";
private String DROP_USER_TABLE = " DROP TABLE IF EXISTS " + TABLE_USER;
public DatabaseHelper(Context context){
super(context, DATABASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_USER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL(DROP_USER_TABLE);
onCreate(db);
}
public void addUser(User user){
SQLiteDatabase db= this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_USER_NAME, user.getName());
values.put(COLUMN_USER_EMAIL, user.getEmail());
values.put(COLUMN_USER_PASSWORD, user.getPassword());
db.insert(TABLE_USER,null, values);
db.close();
}
public boolean checkUser(String password, String email){
String[] columns = {
COLUMN_USER_ID
};
SQLiteDatabase db= this.getWritableDatabase();
String selection = COLUMN_USER_EMAIL + " = ? " + "AND "+ COLUMN_USER_PASSWORD+" =? ";
String[] selectionArgs = { email,password };
Cursor cursor = db.query(TABLE_USER,
columns,
selection,
selectionArgs,
null,
null,
null);
int cursorCount = cursor.getCount();
cursor.close();
db.close();
if(cursorCount > 0){
return true;
}
return false;
}
}
我知道logcat让我知道这一点:
I know the logcat let me know this:
07-11 22:08:29.072 9071-9071/com.example.lavinia.sqllogin E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.lavinia.sqllogin, PID: 9071
android.database.sqlite.SQLiteException: no such column: user_id (code 1
SQLITE_ERROR): , while compiling: SELECT user_id FROM user WHERE user_email =
?AND user_password =?
而且我找不到 user_id 的错误用法,我认为我使用得很好,并且我登录自己后该应用程序将停止运行. (该应用程序具有登录和注册活动-并且注册可以正常进行,但是在Login.activity中按登录"按钮会使应用程序崩溃)
And I can't find the wrong usage of user_id, I think I used it fine and the app will stop working after I Login myself. (the app has login and registration activities- and the registration works ok, but pressing the Login button in the Login.activity will make the app crash)
非常感谢您的支持:)如果需要,我将提供进一步的代码.
Thank you very much for you support :) If needed, I will provide with further code.
推荐答案
此问题可能是由于 我倾向于遇到很多细小的错误 关于onCreate
方法的常见误解.
This issue is probably due to i tend to get lots and lots of tiny errors in conjunction with a common misconception regarding the onCreate
method.
onCreate
方法仅在数据库已创建且已实际创建时自动自动运行一次.它不会在每次运行该应用程序时运行.
The onCreate
method automatically runs only once when the database is and has actually been created. It does not run every time the App is run.
因此,如果在onCreate
方法中对结构(表和列)进行了编码/操作,则对结构(表和列)所做的任何更改(包括更正)都将不适用.
As such any changes (corrections included) to the structure (tables and columns) will not be applied if they are coded/actioned within the onCreate
method.
开发最简单的解决方法是执行以下3项之一:-
When developing the easiest fix is to do one of the following 3:-
- 删除应用程序的数据(删除数据库,以便调用
onCreate
). - 卸载应用程序(删除数据库,因此将调用
onCreate
). - 如果
onUpgrade
方法将删除表,然后调用onCreate
,以增加作为第4个参数传递给SQLIteOpenHelper子类(又称为DatabaseHelper类)的数据库版本号.
- delete the App's data (deletes the database so
onCreate
will be called). - uninstall the App (deletes the database so
onCreate
will be called). - if the
onUpgrade
method will drop the table(s) and then callonCreate
, to increase the database version number as passed as the 4th parameter to the SQLIteOpenHelper sub-class (aka the DatabaseHelper class).
完成上述操作之一后,重新运行该应用程序.
After doing one of the above, rerun the App.
- 如果必须保留数据,则修复会更加复杂,但将基于使用ALTER TABLE语句.
这篇关于android.database.sqlite.SQLiteException:无此类列:user_id(代码1 SQLITE_ERROR):的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!