从sqlite数据库获取内容并使用登录

时间:2016-05-26 05:57:02

标签: java android sqlite

您好我正在构建一个应用程序,您必须先登录或注册然后才能继续 所以我创建一个临时表来存储登录用户或当前用户的ID问题是当我尝试获取它以在其他地方使用它我无法找到它或我无法获取内容。

这是登录活动

public class loginActivity extends AppCompatActivity {
BaseDeDonee bdd;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final EditText lemail =(EditText) findViewById(R.id.lemail);
    final EditText lpassword =(EditText) findViewById(R.id.lpassword);
    final Button sbotton=(Button) findViewById(R.id.sbutton);
    final Button lbotton=(Button) findViewById(R.id.lbutton);
    bdd = new BaseDeDonee(this);

    lbotton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (lemail.getText().toString()==null || lpassword.getText() .toString()==null){
                lemail.setError( "email is required!" );

                lemail.setHint("please enter email");
                lpassword.setError("passwaor required");
            }else {
                boolean ress =bdd.finde(lemail.getText().toString(),lpassword.getText().toString());
                if (ress==true){
                    int th=bdd.findID(lemail.getText().toString());//here is where my problem is 
                    if (th==-1){
                        Toast.makeText(loginActivity.this, "ID was not found to preform ur thing ", Toast.LENGTH_LONG).show();
                    }else{
                        boolean o =  bdd.inseloginid(th);
                        if (o==true){
                            Intent intent = new Intent(loginActivity.this,MainActivity.class);
                            startActivity(intent);
                        }else Toast.makeText(loginActivity.this, "login info not inserted", Toast.LENGTH_LONG).show();

                    }
                    }

                else  Toast.makeText(loginActivity.this, "wrong dont exist", Toast.LENGTH_LONG).show();
            }




        }
    });





}

}

这是数据库类

public class BaseDeDonee extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Platforme.db";

public static final String TABLE_PERSONNE = "UsersTable";
public static final String COL_1 = "ID";
public static final String COL_2 = "nom";
public static final String COL_3 = "prenom";
public static final String COL_4 = "numero_tel";
public static final String COL_5 = "profile";
public static final String COL_s = "password";
public static final String COL_k = "etat";

public static final String TABLE_IDENTIFICATION = "IdentificationTable";

public static final String COL_24 = "profile";
public static final String COL_25 = "password";

public static final String TABLE_Temp = "tempTable";
public static final String COL_29 = "ID";

public BaseDeDonee(Context context ) {
    super(context, DATABASE_NAME, null, 1);
}

public boolean insertl(String profile ,String password ){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues ContentVlues = new ContentValues();
    ContentVlues.put(COL_24,profile);
    ContentVlues.put(COL_25,password);

    long  result =db.insert(TABLE_IDENTIFICATION,null,ContentVlues);
    if (result==-1)
        return false;
    else return true ;

}
   public boolean inseloginid(int  id  ){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+TABLE_Temp,null);
    ContentValues ContentVlues = new ContentValues();
    ContentVlues.put(COL_29,id);
    if(res.getCount()>0){ // I changed this condition from  <= to >
        db.delete(TABLE_Temp,null,null); // added this line
        long  result =db.insert(TABLE_Temp,null,ContentVlues);
        if (result==-1)
            return false;
        else return true ;
    }else return true;
}

public Boolean finde(String lemail, String lpass) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.query(TABLE_IDENTIFICATION, null, "profile=? AND password=?",new String[]{lemail,lpass},null,null,null);
    if(res.getCount()<=0){
        res.close();

        return false;
    }else {
        res.close();
        return true;
    }

}
public int findID (String lemail){
    SQLiteDatabase db = this.getWritableDatabase();
    int k ;
    Cursor res = db.query(TABLE_PERSONNE, null, "profile=?", new String[]{lemail}, null, null, null);
    if (res.getCount()>0){
        res.moveToFirst();
        k=res.getInt(res.getColumnIndexOrThrow("ID"));
    }else k =-1;
    return  k;
}
public int tempID (){
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor res = db.rawQuery("select * from " + TABLE_Temp, null);
    if (res.getCount()<=0){
        return -1;
    }else {
        res.moveToFirst();

         return res.getInt(res.getColumnIndex(" ID "));
    }

}

}

the stack trace

1 个答案:

答案 0 :(得分:0)

  

编辑:1

在看到您的堆栈跟踪后,我建议您检查 Google API使用情况,因为您的堆栈跟踪说明使用Google API进行身份验证失败。

试试这个:

public int findID (String lemail){
    SQLiteDatabase db = this.getReadableDatabase();
    int k ;

    // Try changing here TABLE_IDENTIFICATION or TABLE_Temp..!!

    Cursor res = db.rawQuery("select * from " + TABLE_PERSONNE + " WHERE profile='"+lemail+"'", null);
    if (res.getCount()>0){
        res.moveToFirst();
        k=res.getInt(res.getColumnIndexOrThrow("ID"));
    }else k =-1;
    return  k;
}