2013-04-09 35 views
0

這是我的函數來比較它.......請找出錯誤,如果any.i都還沒有能夠如何從CursorWindow讀取行/列? - Android電子

public void onClick(View v) { 
// TODO Auto-generated method stub 

String unname=username1.getText().toString(); 
String storedPassword1=db.getdata(unname); 
db.open(); 
try 
{ 
if(password.equals(storedPassword1)) 
{ 
Intent it=new Intent("com.Butterfly.bmw.ADMINLIST"); 
startActivity(it); 
} 
else 
{ 
Toast.makeText(Database.this, "User Name or Password does not match",  Toast.LENGTH_LONG).show(); 
System.out.println("errorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"); 
} 
db.close(); 
} 
catch (Exception e) 
{ 
Toast.makeText(Database.this, "does not enter in try block", Toast.LENGTH_LONG).show(); 
} } 
}); 

記錄,這是我的DB類請從這裏找到錯誤。我沒有在這個

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; 
import android.util.Log; 
public class EditDatabase 
{ 
public static final String KEY_USERNAME="username"; 
public static final String KEY_ROLE="role"; 
public static final String KEY_PASSWORD="password"; 
private static final String TAG="DbaAdapter"; 
private static final String DATABASE_NAME="mydatabase"; 
private static final String DATABASE_TABLE="login"; 
private static final int DATABASE_VERSION=1; 

private static final String DATABASE_CREATE="create table "+DATABASE_TABLE+"("+KEY_USERNAME+" text primary key ,"+KEY_PASSWORD+" text not null,"+KEY_ROLE+" text not null)"; 
    private final Context context; 
    private static DatabaseHelper DBhelper; 
    private static SQLiteDatabase db; 

    public EditDatabase(Context ctx){ 
this.context=ctx; 
DBhelper=new DatabaseHelper(context); 
    } 
    private static class DatabaseHelper extends SQLiteOpenHelper{ 
    DatabaseHelper(Context context){ 
    super(context,DATABASE_NAME,null,DATABASE_VERSION); 
    } 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
// TODO Auto-generated method stub 
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
    + newVersion + ", which will destroy all old data"); 
    db.execSQL("DROP TABLE IF EXISTS contacts"); 
    onCreate(db); 
} 
@Override 
public void onCreate(SQLiteDatabase db) { 
// TODO Auto-generated method stub 
db.execSQL(DATABASE_CREATE); 
} 
} 
public EditDatabase open() throws SQLException{ 
    // TODO Auto-generated method stub 
    db=DBhelper.getWritableDatabase(); 
     return this; 
} 

public long insertDetails(String uname, String pass, String string) { 
    // TODO Auto-generated method stub 

     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_USERNAME, uname); 
     initialValues.put(KEY_PASSWORD, pass); 
     initialValues.put(KEY_ROLE, "admin"); 
     return db.insert(DATABASE_TABLE, null, initialValues); 
} 
public void close() { 
    // TODO Auto-generated method stub 
    DBhelper.close(); 
} 

public String getdata(String unname) { 
    // TODO Auto-generated method stub 

    Cursor cursor = db.rawQuery("SELECT * FROM login WHERE KEY_USERNAME=?", new String [] { unname.toString() }); 
    if(cursor.getCount()<1) // UserName Not Exist 
    { 
     cursor.close(); 
     return "NOT EXIST"; 
    } 
    cursor.moveToFirst(); 
    String password= cursor.getString(cursor.getColumnIndex("KEY_PASSWORD")); 
    cursor.close(); 

    return password; 

} 
public long insertDetailsuser(String uname, String pass, String string) { 
    // TODO Auto-generated method stub 
    ContentValues initialValuess = new ContentValues(); 
     initialValuess.put(KEY_USERNAME, uname); 
     initialValuess.put(KEY_PASSWORD, pass); 
     initialValuess.put(KEY_ROLE, "user"); 
     return db.insert(DATABASE_TABLE, null, initialValuess); 
} 

public Cursor getAllContacts() { 
    // TODO Auto-generated method stub 

    return db.query(DATABASE_TABLE, new String[] {KEY_USERNAME, KEY_PASSWORD, 
       KEY_ROLE}, null, null, null, null, null);} 
public boolean deleteDetails(String uname) 
{ 
// TODO Auto-generated method stub 
//Cursor cursor=db.rawQuery("select password from login where " + KEY_USERNAME + "='" + unname + "'", null); 
return db.delete(DATABASE_TABLE,KEY_USERNAME + "='" + uname + "'", null)>0; 
} 
} 

回答

0

我認爲它可以幫助您登錄..嘗試讓使用此代碼數據..其一個代碼片段,您可以管理自己..

YourDatabaseFile.Java

public String get(String unname) 
{ 
    return db.rawQuery("SELECT * FROM login WHERE KEY_USERNAME=?", new String [] { unname.toString() }); 
} 

YourCodeFile.Java

public void onClick(View v) 
{ 
    String storedPassword; 
    String unname=username1.getText().toString(); 
    db.open(); 

    Cursor cur = db.get(unname); 
    cur.moveToFirst();  
    if(cur.getCount() < 1) 
    { 
     storedPassword = "NOT EXIST"; 
    } 
    else 
    { 
     storedPassword = cur.getString(cur.getColumnIndex("KEY_PASSWORD")); 
    } 

    cur.close(); 

    try 
    { 
     if(password.equals(storedPassword)) 
     { 
      Intent it=new Intent("com.Butterfly.bmw.ADMINLIST"); 
      startActivity(it); 
     } 
     else 
     { 
      Toast.makeText(Database.this, "User Name or Password does not match", Toast.LENGTH_LONG).show(); 
      System.out.println("errorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"); 
     } 
     db.close(); 
    } 
    catch (Exception e) 
    { 
     Toast.makeText(Database.this, "does not enter in try block", Toast.LENGTH_LONG).show(); 
    } 
} 
+0

這不是workingggggggg – nitesh 2013-04-09 11:54:05

+0

你有任何錯誤?你試過通過調試代碼? – Ajay 2013-04-09 14:01:30

+0

這顯示回報語句中的錯誤 – nitesh 2013-04-10 04:01:03

相關問題