2012-11-13 124 views
0

我有2個java類,DatabaseAdapter.java和DBHelper.java。爲用戶登錄編寫的代碼不正確,我不知道錯誤在哪裏以及如何編寫代碼。有人可以請幫助。 '新DBHelper(this)'在程序中被強調。無法登錄到Android應用程序

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()); 
    } 
} 

回答

4

您的DbHelper構造函數需要一個Context作爲參數,但是您要給它一個DatabaseAdapter的實例。您應該閱讀一些基本的Java和Android教程和文檔。

相關問題