Android saving data into datatable but when i click for show it gives me error...check plz
package Database.Use;
import Database.Use.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class use_database extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText firstname;
final Context context = this;
firstname = (EditText)findViewById(R.id.firstname);
Button save =(Button)findViewById(R.id.Go);
Button show = (Button)findViewById(R.id.show);
save.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0)
{
try{
SQLiteDatabase myDB;
String TableName = "tb";
String ShowData;
myDB= openOrCreateDatabase("db",MODE_PRIVATE, null);
String query1,query2;
query1="create table if not exists "+TableName+
""+"(first VARCHAR );";
myDB.execSQL(query1);
query2=("INSERT INTO "+ TableName+ " ( first)
VALUES ('"+firstname.getText().toString()+"');");
myDB.execSQL(query2);
}
catch(Exception e) {
Log.e("Error", "Error", e);
}
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Salvar");
alertDialogBuilder
.setMessage("Todus Informaroes Incerido!")
.setCancelable(false)
.setNegativeButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
firstname.setText("");
}});
//........................... button for show data
show.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0)
{
SQLiteDatabase myDB = null;
String TableName = "tb";
Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);
int frist = c.getColumnIndex("first");
try{
if(c!=null)
{
String str = c.getString(frist);
firstname.setText(str);
}
}catch(Exception e) {
Log.e("Error", "Error", e);}
// Check result.
}});}}