Android SQLite _ Connection

时间:2017-07-03 03:16:21

标签: android sqlite

我想登录Control.It不会出错,但是当我按下时 R.id.girisBtn,它没有进入新的活动。我继续使用Debug, 但我看不出错误,我也不明白错误的起源

DatabaseHelper.java

public class DatabaseHelper  extends SQLiteOpenHelper{
   private  static  final int db_version=2;
   private  static  final String db_Name="SosyalMedyaDB.db";
   private  static  final String db_table="Kisi";

   private  static  final int Index_UserName=0;
   private  static  final int Index_UserPassword=1;

//Db Create 

private static final String DATABASE_CREATE = "CREATE TABLE \"Kisi\" (
`isim` TEXT NOT NULL, `parola` TEXT, `email` TEXT, `cinsiyet` TEXT, `tarih` 
TEXT, `resim` INTEGER, " +
        "`url` TEXT )";
SQLiteDatabase db;
public  DatabaseHelper(Context context)
{
    super(context,db_Name,null,db_version);
 }
 @Override
 public void onCreate(SQLiteDatabase db) {
    db.execSQL(DATABASE_CREATE);
    this.db=db;
  }
public  String searchPass(String usName)
 {
   db=this.getReadableDatabase();
   String query="select * from "+db_table;
   Cursor cursor=db.rawQuery(query,null);
   String a,b;
   b="not found";
   if(cursor.moveToFirst())
   {
       do {
           a = cursor.getString(Index_UserName);
           b=cursor.getString(Index_UserPassword);
           if (a.equals(usName))
           {
               b = cursor.getString(Index_UserPassword);
               db.close();
       break;
       }}
           while (cursor.moveToNext());
       }
       return  b;
   }
 @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
 {
    db.execSQL("DROP TABLE IF EXISTS "+db_table);
    this.onCreate(db);
  }
 }

Login.java

 public class Login extends AppCompatActivity {
   EditText name,pass;
   ArrayList<Kisi> uyeler;
   String loginIsim, loginPass;
   Intent intent;
   DatabaseHelper dbHelper=new DatabaseHelper(this);
   Button btnLogin;
   String userName, userPassword;
    @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_login);

     name = (EditText) findViewById(R.id.loginName);
     pass = (EditText) findViewById(R.id.password);
     loginIsim = name.getText().toString();
     loginPass = pass.getText().toString();
      }
    public void onClick(View v) {
    switch (v.getId()) {
        case R.id.girisBtn:
            Log.w(".............. ","");
           String password = dbHelper.searchPass(loginIsim);
            Log.w("string password  ","................");
          //Password Contol
          if (loginPass.equals(password)) {
              Log.w("İf",".............................................");
                intent = new Intent(this, HomePage.class);
                startActivity(intent);
            } else {
                Log.w("come", "else");
            }
            break;
        case R.id.uyeBtn:
            intent = new Intent(this, Register.class);
            startActivity(intent);
            break;
      }}}

2 个答案:

答案 0 :(得分:0)

而不是这样你可以尝试这种方法

String[] columns ={"field_1", "field_2"}
Cursor pCursor = db.query(db_table, columns, "username_field=?", new String[] { USERNAME }, null, null, null);

答案 1 :(得分:0)

试试这帮助你

 /////DatabaseHelper.java 


    public class DatabaseHelper  extends SQLiteOpenHelper{
       private  static  final int db_version=2;
       private  static  final String db_Name="SosyalMedyaDB.db";
       private  static  final String db_table="Kisi";

       private  static  final int Index_UserName=0;
       private  static  final int Index_UserPassword=1;

    //Db Create 

    private static final String DATABASE_CREATE = "CREATE TABLE \"Kisi\" (
    `isim` TEXT NOT NULL, `parola` TEXT, `email` TEXT, `cinsiyet` TEXT, `tarih` 
    TEXT, `resim` INTEGER, " +
            "`url` TEXT )";
    SQLiteDatabase db;
    public  DatabaseHelper(Context context)
    {
        super(context,db_Name,null,db_version);
     }
     @Override
     public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
        this.db=db;
      }
    public  String searchPass(String usName)
     {
       db=this.getReadableDatabase();
       String query="select * from "+db_table;
       Cursor cursor=db.rawQuery(query,null);
       String a,b;
       b="not found";
       if(cursor.moveToFirst())
       {
           do {
               a = cursor.getString(Index_UserName);
               b=cursor.getString(Index_UserPassword);
               if (a.equals(usName))
               {
                   b = cursor.getString(Index_UserPassword);
                   db.close();
           break;
           }}
               while (cursor.moveToNext());
           }
           return  b;
       }
     @Override
       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
     {
        db.execSQL("DROP TABLE IF EXISTS "+db_table);
        this.onCreate(db);
      }
     }


     //////Login.java

     public class Login extends AppCompatActivity implements View.OnClickListener{
       EditText name,pass;
       ArrayList<Kisi> uyeler;
       String loginIsim, loginPass;
       Intent intent;
       DatabaseHelper dbHelper=new DatabaseHelper(this);
       Button btnLogin;
       String userName, userPassword;
        @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_login);

         name = (EditText) findViewById(R.id.loginName);
         pass = (EditText) findViewById(R.id.password);
    btnLogin=(Button)findviewbyid(R.id.girisBtn);
    btnLogin.setOnClickListener(this);
         loginIsim = name.getText().toString();
         loginPass = pass.getText().toString();
          }
        public void onClick(View v) {
        switch (v.getId()) {
            case R.id.girisBtn:
                Log.w(".............. ","");
               String password = dbHelper.searchPass(loginIsim);
                Log.w("string password  ","................");
       //Password Contol
              if (loginPass.equals(password)) {
                  Log.w("İf",".............................................");
                    intent = new Intent(this, HomePage.class);
                    startActivity(intent);
                } else {
                    Log.w("come", "else");
                }
                break;
            case R.id.uyeBtn:
                intent = new Intent(this, Register.class);
                startActivity(intent);
                break;
          }}}