lundi 25 avril 2016

Issues in Using SQLite Database

Here My Issue is database is created but table was not created and values are not inserted into database any one can solve this problem?

Main ACTIVITY:

    EditText editText,editText1,;
    Database mydb;
    SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mydb = new Database(this);
      db = mydb.getWritableDatabase();
    }
    public void loginPage(View v) {
        editText = (EditText) findViewById(R.id.username);
        editText1 = (EditText) findViewById(R.id.pasword);
        String username = editText.getText().toString();
        String password = editText1.getText().toString();

        if (username.equals("")) {
            editText.setError("Pls Enter UserName");
        } else if (password.equals(""))
        {
            editText1.setError("pls enter password");
        }
    try
    {
        StringBuffer sb = new StringBuffer();
        String query = "select * from Users";
        Cursor c = db.rawQuery(query,null);
        boolean res = c.moveToFirst();
        if (res)
        {
            do {
                username = c.getString(0);
                sb.append(username);
            }
            while (c.moveToNext());
        }
        else
        {
            Toast.makeText(MainActivity.this, "Wrong credentials", Toast.LENGTH_SHORT).show();
        }
        String data = sb.toString();
        Intent  i  = new Intent(this,WelcomeActivity.class);
        i.putExtra("k1",data);
        startActivity(i);
    }catch (Exception e)
    {
        Log.e("ReadDataException",""+e);
    }

    }
    public void signupPage(View v)
    {
        Intent i = new Intent(this,SignupActivity.class);
        startActivity(i);
    }
}

Signup Activity

EditText  editText2,editText3,editText4,editText5,editText6;

    Database mydb;
    SQLiteDatabase db;
    String Gender = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signuppage);

        mydb = new Database(this);
        db = mydb.getWritableDatabase();
    }
    public void createAccount(View v)
    {
        editText2 = (EditText)findViewById(R.id.username);
        editText3 = (EditText)findViewById(R.id.passwordd);
        editText4 = (EditText)findViewById(R.id.cnfrmpassword);
        editText5 = (EditText)findViewById(R.id.emailaddress);
        editText6  = (EditText)findViewById(R.id.phone);
        RadioGroup rg = (RadioGroup)findViewById(R.id.radio);
        final int id = rg.getCheckedRadioButtonId();
        final RadioButton rb1 = (RadioButton)findViewById(id);
     final    RadioButton rb2 = (RadioButton)findViewById(id);
        String username = editText2.getText().toString();
        String password = editText3.getText().toString();
        String cnfmpasswrd = editText4.getText().toString();
        String email = editText5.getText().toString();
        String phone = editText6.getText().toString();

      rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(RadioGroup group, int checkedId) {
              switch (id)
              {
                  case R.id.male:
                  {
                      if (rb1.isChecked())
                      {
                          Gender ="Male";
                      }
                  }
                  break;
                  case R.id.female:
                  {
                      if (rb2.isChecked())
                      {
                          Gender = "Female";
                      }
                  }
              }
          }
      });
        if (username.equals(""))
        {
            editText2.setError("pls enter name");
        }
        else if (password.equals(""))
        {
            editText3.setError("pls fill fileds");
        }
      else  if (!password.equals(cnfmpasswrd))
        {
            editText4.setError("Does not match passwords");
        }
        else if (email.equals(""))
        {
            editText5.setError("pls enter email address");
        }
        else if (phone.equals(""))
        {
            editText6.setText("pls enter valid number");
        }
        String query = "insert into Users values('"+username+"',"+password+","+email+","+phone+","+Gender+")";
        try
        {
           db.execSQL(query);
            AlertDialog.Builder ab = new AlertDialog.Builder(this);
            ab.setMessage("Account Created Succesfully");
            ab.show();
            setContentView(R.layout.activity_main);
        }
        catch(Exception e)
        {
            Log.e("InsertionException",""+e);
        }
        editText2.setText("");
        editText3.setText("");
        editText4.setText("");
        editText5.setText("");
        editText6.setText("");
    }
}

Database Activity

 public static final String DBNAME = "anil";
    public static final int VERSION = 1;
    public Database(Context c)
    {
        super(c,DBNAME,null,VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        try
        {
        String query = "create table Users(Username TEXT,Password TEXT,Email TEXT,PhoneNumber INTEGER,Gender TEXT)";
            db.execSQL(query);
            }catch (Exception e)
        {
            Log.e("TableCreationException",""+e);
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

Aucun commentaire:

Enregistrer un commentaire