我有2个java类,DatabaseAdapter.java和DBHelper.java。为用户登录编写的代码不正确,我不知道错误在哪里以及如何编写代码。有人可以请帮助。 “新DBHelper(this)”在该计划中有下划线。
public class DatabaseAdapter {
// Table attributes
public static final String TABLE_USER = "user_table";
//User Table
public static final String USER_ID = "_id";
public static final String UNAME = "name";
public static final String USURNAME = "surname";
public static final String USERNAME = "username";
public static final String UPASSWORD = "password";
public static final String UEMAILADDRESS = "emailadd";
public boolean Login(String username, String password) throws SQLException
{
DBHelper dbhelper = new DBHelper(this);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_USER + " WHERE username=? AND password=?", new String[]{username,password});
if (mCursor != null) {
if(mCursor.getCount() > 0)
{
return true;
}
}
mCursor.close();
db.close();
return false;
}
这里是创建表的DBHelper。
public class DBHelper extends SQLiteOpenHelper {
public static final int DB_VERSION = 1;
public static final String DB_NAME = "moneym.db";
public DBHelper(Context ctx) {
super(ctx, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
createTables(db);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
public void createTables(SQLiteDatabase database) {
String UserTable = "create table if not exists " + DatabaseAdapter.TABLE_USER + " ( " +
DatabaseAdapter.USER_ID + " integer primary key autoincrement, "
+ DatabaseAdapter.UNAME + " text, "
+ DatabaseAdapter.USURNAME + " text, "
+ DatabaseAdapter.USERNAME + " text, "
+ DatabaseAdapter.UPASSWORD + " text, "
+ DatabaseAdapter.UEMAILADDRESS + " text);";
try {
database.execSQL(UserTable);
Log.d("Users","Tables created!");
}
catch(Exception ex) {
Log.d("Users", "Error in DBHelper.onCreate() : " + ex.getMessage());
}
}
答案 0 :(得分:4)
您的DbHelper构造函数需要Context作为参数,但您要为其提供DatabaseAdapter的实例。您应该阅读一些基本的Java和Android教程和文档。