2014-11-04 209 views
-1

嗨,大家好,我在Java和SQLite編程方面有點新,我從SQLite數據庫中提取和檢索特定數據時遇到問題,我想創建一個登錄程序,用戶將輸入用戶名和密碼(LoginScreen.java),它將檢查數據庫(database.java)中的現有數據,如果存在,它將返回true至LoginScreen.java。無法從SQLite數據庫檢索特定數據(Android)

之後,LoginScreen.java將檢查,如果爲true,它將運行SQLViews Intent,如果爲false,它將運行Registration Intent。

而問題是,即使數據庫中存在數據,我的代碼也總是返回false。 你能幫我檢查一下我的代碼嗎?

謝謝

這裏是代碼: Database.java

package com.thesis.teamizer; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class Database { 

public static final String DATABASE_NAME = "TeamizerDB.db"; 
public static final String TABLE_MEMBER = "Member"; 
public static final int DATABASE_VERSION = 1; 
public static final String MEMBER_USERNAME = "Username"; 
public static final String MEMBER_PASSWORD = "Password"; 
public static final String MEMBER_EMAIL = "Email"; 
public DbHelper ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDatabase; 

public static class DbHelper extends SQLiteOpenHelper { 

    public DbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL("CREATE TABLE " + TABLE_MEMBER + " (" + MEMBER_USERNAME 
       + " TEXT PRIMARY KEY NOT NULL, " + MEMBER_PASSWORD 
       + " TEXT NOT NULL, " + MEMBER_EMAIL + " TEXT NOT NULL);"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER); 
     onCreate(db); 
    } 

} 

public Database(Context c) { 
    ourContext = c; 
} 

public Database open() throws SQLException { 
    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 

    ourHelper.close(); 
} 

public boolean createEntry(String username, String password, String email) { 
    // TODO Auto-generated method stub 
    ContentValues cv = new ContentValues(); 
    cv.put(MEMBER_USERNAME, username); 
    cv.put(MEMBER_PASSWORD, password); 
    cv.put(MEMBER_EMAIL, email); 
    ourDatabase.insert(TABLE_MEMBER, null, cv); 
    return true; 
} 

public String getData() { 
    // TODO Auto-generated method stub 
    String[] columns = new String[] { MEMBER_USERNAME, MEMBER_PASSWORD, 
      MEMBER_EMAIL }; 
    Cursor c = ourDatabase.query(TABLE_MEMBER, columns, null, null, null, 
      null, null); 

    String result = ""; 

    int iUsername = c.getColumnIndex(MEMBER_USERNAME); 
    int iPassword = c.getColumnIndex(MEMBER_PASSWORD); 
    int iEmail = c.getColumnIndex(MEMBER_EMAIL); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 

     result = result + c.getString(iUsername) + " " 
       + c.getString(iPassword) + " " + c.getString(iEmail) + "\n"; 

    } 

    return result; 
} 

public Boolean getName(String l) throws SQLException { 
    // TODO Auto-generated method stub 
    String Query = "SELECT USERNAME FROM " + TABLE_MEMBER + " WHERE " 
      + MEMBER_USERNAME + " =?"; 
    Cursor mCursor = ourDatabase.rawQuery(Query, new String[] { l }); 

    boolean hasObject = false; 
    if (mCursor.moveToFirst()) { 
     hasObject = false; 
     mCursor.close(); 
    } 

    else 
     hasObject = true; 

    return hasObject; 

} 

public Boolean getMatched(String username, String password) { 
    // TODO Auto-generated method stub 


    String Query = "SELECT USERNAME FROM " + TABLE_MEMBER + " WHERE " 
      + MEMBER_USERNAME + " =? AND " + MEMBER_PASSWORD + " =?"; 

    Cursor c = ourDatabase.rawQuery(Query, new String[] { username, 
      password }); 
    if (c.moveToFirst()) { 
     return true; 
    } else { 
     return false; 
    } 
} 

} 

LoginScreen.java

package com.thesis.teamizer; 

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
public class LoginScreen extends Activity { 

private EditText etUsername; 
private EditText etPassword; 
private Button bLogin; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.login_screen); 

    validationUsernameAndPassword(); 
    ifRegisterButtonClicked(); 

} 

private void validationUsernameAndPassword() { 
    // TODO Auto-generated method stub 
    etUsername = (EditText) findViewById(R.id.etLoginUsername); 
    etPassword = (EditText) findViewById(R.id.etLoginPassword); 
    bLogin = (Button) findViewById(R.id.bLogin); 

    final String username = etUsername.getText().toString(); 
    final String password = etPassword.getText().toString(); 

    bLogin.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 

      int flag = 0; 

      /* 
      * if (!isValid(username)) { 
      * etUsername.setError("Username must be filled"); flag++; } 
      * 
      * if (!isValid(password)) { 
      * etPassword.setError("Password must be filled"); flag++; } 
      */ 
      if (flag == 0) { 

       Database checkLogin = new Database(LoginScreen.this); 
       checkLogin.open(); 
       Boolean temp = checkLogin.getMatched(username, password); 

       // Kalo username n passwordnya benar2 ada di database 

       if (temp == true) { 
        Intent intent = new Intent(
          "com.thesis.teamizer.SQLVIEWS"); 
        startActivity(intent); 

       } // Kalo username n passwordnya salah atau gak ada di 
        // database 
       if (temp == false) { 
        Intent i = new Intent("com.thesis.teamizer.REGISSCREEN"); 
        startActivity(i); 
       } 

       checkLogin.close(); 
      } 

     } 

     private boolean isValid(String temp) { 

      if (temp != null && temp.length() > 6 && !temp.contains(" ")) { 
       return true; 
      } 

      return false; 
     } 

    }); 
} 

private void ifRegisterButtonClicked() { 
    // TODO Auto-generated method stub 
    Button bRegis = (Button) findViewById(R.id.bRegister); 
    bRegis.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent intent = new Intent("com.thesis.teamizer.REGISSCREEN"); 
      startActivity(intent); 
     } 
    }); 
} 

} 
+0

雖然你正在重複你的問題,我會在這裏回答。我想你以前正在存儲一組正確值以便比較,如果是這種情況,請檢查數據庫中是否有信息(插入是正確的)並使用日誌來檢查數據是否與你的行匹配。 – zozelfelfo 2014-11-04 10:10:10

+0

是的,我已經檢查數據是否存在或沒有手動與選擇*,但它stil不工作 – 2014-11-04 10:19:38

回答

0
private void validationUsernameAndPassword() { 
    ... 
    final String username = etUsername.getText().toString(); 
    final String password = etPassword.getText().toString(); 

    bLogin.setOnClickListener(new OnClickListener() { 
     ... 

創建活動時,您正在閱讀的價值。 您應該在單擊按鈕後執行此操作。

+0

謝謝,它真的幫助。從來沒有想過,結構和位置是非常重要的.. – 2014-11-04 15:54:11