Values not inserting in database in android
<p>Below given is my code.  </p>

<p>I have 2 classes: <code>Dbhelper.java</code> and <code>RssActivity.java</code></p>

<p>Basically I have made a <code>students</code> table and my code is creating the table, but values are not being inserted into it.</p>

<p>There are 2 textboxes: one is for entering name and other for entering <code>roll no</code>.</p>

<p>I  want that the values should be inserted when the <code>roll no</code> lies in the range 00113202709 to 12013203709</p>

<p>I want that the values should be taken from the 2 textboxes <code>editText1</code> and <code>editText2</code> and be inserted in my database table.</p>

<p>Thanks for any help.</p>

<p>Dbhelper.java:</p>

<pre><code>public class Dbhelper extends SQLiteOpenHelper {

//private static final String TAG = Dbhelper.class.getSimpleName();

public static final String db_name = "member.db";

public static final int db_version = 1;

public final String TABLE = "students";

public static final String C_ID = BaseColumns._ID;

public static final String C_Name = "SName";

public static final String C_ENo = "Enroll No";

private Context context;


public Dbhelper(Context context) {

    super(context, db_name, null, db_version);
    this.context=context;

}

@Override
public void onCreate(SQLiteDatabase db) {

    String sql = String.format(
            "create table %s (%s TEXT primary key, %s TEXT, %s TEXT )",
            TABLE, C_ID, C_Name, C_ENo);

db.execSQL(sql);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("drop table if exists " + TABLE);
    this.onCreate(db);

}
}
</code></pre>

<p>RssActivity.java:</p>

<pre><code>public class RssActivity extends Activity implements OnClickListener {


TextView Title;
EditText Name, ENo;
Button but;
public String n1 = "001";
public String n2 = "120";
public String s1, s2, s3;
Dbhelper obj;
SQLiteDatabase db;
ContentValues val;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Title = (TextView) findViewById(R.id.textView1);
    Name = (EditText) findViewById(R.id.editText2);
    ENo = (EditText) findViewById(R.id.editText1);
    but = (Button) findViewById(R.id.button1);
    but.setOnClickListener(this);
    obj=new Dbhelper(RssActivity.this);
    db=obj.getWritableDatabase();
}

public void onClick(View v) {
    String name = Name.getText().toString();
    String enr = ENo.getText().toString();

    try {

        if (name.equals("")) {
            Name.setError(getText(R.string.Empty));
            Toast.makeText(this, R.string.Empty, Toast.LENGTH_LONG).show();

        } else if ((ENo.getText().toString().length() == 0)) {
            ENo.setError(getText(R.string.Empty));
            Toast.makeText(this, R.string.Empty, Toast.LENGTH_LONG).show();

        } else if ((ENo.getText().toString().length() &lt; 11))

        {
            ENo.setError(getText(R.string.Invalid));
            Toast.makeText(this, R.string.Invalid, Toast.LENGTH_LONG)
                    .show();

        }

        else

        {
            s1 = "000";
            s2 = "00000000";
            throw new Exception();
        }

    }

    catch (Exception e) {
        e.printStackTrace();

        s1 = enr.substring(0, 3);
        s2 = enr.substring(3, 11);
        s3 = "13202709";
        int r1 = s1.compareTo(n1);
        int r2 = s1.compareTo(n2);

        if (s2.equals(s3)) {
            if (r1 == 0 || r2 == 0) {

                Toast.makeText(this, R.string.Success, Toast.LENGTH_LONG)
                        .show();
                val=new ContentValues();
                val.clear();
                val.put(obj.C_ID, s1);
                val.put(obj.C_Name, name);
                val.put(obj.C_ENo,enr);
                db.insert(obj.TABLE, null, val);
                db.close();




            } else if (r1 &lt; 0 || r2 &gt; 0) {
                Toast.makeText(this, R.string.Fail, Toast.LENGTH_LONG)
                        .show();
            } else if (r1 &gt; 0 || r2 &lt; 0) {
                Toast.makeText(this, R.string.Success, Toast.LENGTH_LONG)
                        .show();
            }
        }

        else

            Toast.makeText(this, R.string.Fail, Toast.LENGTH_LONG).show();

    }
    db.close();
    obj.close();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.item1:
        startActivity(new Intent(this, Welcomescreen.class));
        this.finish();
        break;

    case R.id.item2:
        this.finish();
        break;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, Welcomescreen.class);
    // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    this.finish();
}

}
</code></pre>
