如何从CursorWindow读取行/ col? - android

时间:2013-04-09 06:14:02

标签: android

这是我比较它的功能.......如果any.i没有登录,请找错误

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

1 个答案:

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