database data retrieval problem on android
<p>hi all
in my app i count no of records in a table and if no records in a table i will insert one record. there is no problem in getting the count and inserting data. <strong>problem is retrieve the data from table.</strong>. if i retrieve no values is shown in Logcat. if i run app again the count shows is counter is 1.</p>

<p>My code:</p>

<pre><code>public class First extends Activity
  {
EditText Name;
ImageButton Start;
SQLiteDatabase db;
String nameValue,tmp;
int counter=0;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.regpage);

    db = openOrCreateDatabase("Reg.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
    db.setVersion(1);
    db.setLocale(Locale.getDefault());
    db.setLockingEnabled(true);
    final String CREATE_TABLE_ONLINEDATA ="CREATE TABLE IF NOT EXISTS OnlineData(name TEXT);";
    db.execSQL(CREATE_TABLE_ONLINEDATA);

  //        get the count for No of records in the table.
    Cursor c = db.rawQuery("select * from OnlineData"  , null);
    if (c != null ) {
    if  (c.moveToFirst()) {
      do {
        counter=counter+1;
        tmp=c.getString(0);
        Log.e("first page",tmp);
      }while (c.moveToNext());
    }
    }
    Log.e("sql","counter" +counter);

    Name = (EditText)findViewById(R.id.regEdit);
    nameValue = Name.getText().toString();

    Start = (ImageButton)findViewById(R.id.gobtn);
    Start.setOnClickListener(new View.OnClickListener() 
    {   
        @Override
        public void onClick(View v) 
        {
     //             inserting name into the data base.
            if(counter==0)
            {
              ContentValues values = new ContentValues();
              values.put("name",nameValue);
              db.insert("OnlineData", null, values);
              Log.e("sql-insert", "ok");
            }       

            Cursor cx = db.rawQuery("select * from OnlineData"  , null);
            if  (cx.moveToFirst()) {
              do {
                Log.e("First page-insert",""+cx.getString(0));
              }while (cx.moveToNext());
            }


            db.close();
     //          ----------------------------------------------------           
            Intent myIntent = new Intent(v.getContext(), Tabbar.class);
            startActivityForResult(myIntent, 0);
            First.this.finish();
        }
    });
}
</code></pre>

<p>}</p>
