My code for an attendance database
public class Main2Activity extends AppCompatActivity {
SQLiteDatabase db;
TextView tv;
EditText e1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
MainActivity obj=new MainActivity();
tv=(TextView)findViewById(R.id.textView1);
e1=(EditText)findViewById(R.id.edText1);
db= openOrCreateDatabase("Mydb", MODE_PRIVATE, null);
db.execSQL("create table if not exists mytable1(id varchar,val varchar )");
db.execSQL("create table if not exists mytable2(id varchar,val varchar)");
}
public void insert(View v)
{
String name=e1.getText().toString();
String option="present";
// String sur_name=et1.getText().toString();
e1.setText("");
//et1.setText("");
//insert data into able
db.execSQL("insert into mytable1 values('"+name+"','"+option+"')");
//display Toast
Toast.makeText(this, "value inserted successfully.", Toast.LENGTH_LONG).show();
}
public void insert2(View v)
{
String name=e1.getText().toString();
String option2="absent";
//String sur_name=et1.getText().toString();
e1.setText("");
//et2.setText("");
//insert data into able
db.execSQL("insert into mytable2 values('"+name+"','"+option2+"')");
//display Toast
Toast.makeText(this, "values inserted successfully.", Toast.LENGTH_LONG).show();
}
public void display(View v)
{
//use cursor to keep all data
//cursor can keep data of any data type
Cursor c1=db.rawQuery("select * from mytable1", null);
tv.setText("");
//move cursor to first position
c1.moveToFirst();
//fetch all data one by one
do
{
//we can use c.getString(0) here
//or we can get data using column index
String name=c1.getString(c1.getColumnIndex("id"));
String surname=c1.getString(1);
//display on text view
tv.append("Name:"+name+" and SurName:"+surname+"\n");
//move next position until end of the data
}while(c1.moveToNext());
}
} Not able to find out errors in this code! even tried changing datatype of val to integer autoincrement. doesnt work
Aucun commentaire:
Enregistrer un commentaire