Sqllite_database
//display ni xml file blank rakhvi
//MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText name, id, address;
Button insert, delete, display, update;
String Mname[]={"Asus","Nokia","Samsung","Oppo","Redmi","Vivo","Other"};
RadioButton rb;
RadioGroup rg;
Spinner sp;
SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = openOrCreateDatabase("register", Context.MODE_PRIVATE, null);
String tab = "create table if not exists newTable(name text,id text,address text)";
database.execSQL(tab);
name = findViewById(R.id.editText1);
id = findViewById(R.id.editText2);
address = findViewById(R.id.editText3);
insert = findViewById(R.id.button1);
delete = findViewById(R.id.button2);
update = findViewById(R.id.button3);
display = findViewById(R.id.button4);
rg=findViewById(R.id.rg);
sp=findViewById(R.id.sp);
ArrayAdapter adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,Mname);
sp.setAdapter(adapter);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (name.getText().toString().equals("")) {
name.setError("Enter your name");
name.requestFocus();
return;
}
if (id.getText().toString().equals("")) {
id.setError("Enter your id");
id.requestFocus();
return;
}
if (address.getText().toString().equals("")) {
address.setError("Enter address");
address.requestFocus();
return;
} else {
String insert = "insert into newTable values('" + name.getText().toString()+"','"+id.getText().toString() + "','" + address.getText().toString() + "')";
database.execSQL(insert);
Toast.makeText(MainActivity.this, "Insert success", Toast.LENGTH_SHORT).show();
}
}
});
display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (name.getText().toString().equals("")) {
name.setError("Enter name");
name.requestFocus();
return;
}
String d = "delete from newTable where name='" + name.getText().toString() + "'";
database.execSQL(d);
Toast.makeText(MainActivity.this, "Record delete", Toast.LENGTH_LONG).show();
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int select=rg.getCheckedRadioButtonId();
rb=findViewById(select);
String update="update newTable set id='"+id.getText().toString()+"',address='"+address.getText().toString()+"'where name='"+name.getText().toString()+"'";
database.execSQL(update);
Toast.makeText(MainActivity.this, "upadate Record", Toast.LENGTH_SHORT).show();
}
});
}
}
//display.java
//display.java
public class Main2Activity extends AppCompatActivity {
SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
database = openOrCreateDatabase("register", Context.MODE_PRIVATE, null);
String tab = "create table if not exists newTable(name text,id text,address text)";
database.execSQL(tab);
String s = "select * from newTable";
Cursor c = database.rawQuery(s, null);
StringBuilder builder = new StringBuilder();
if (c.moveToFirst()){
do {
builder.append("\t" + c.getString(0));
builder.append("\t" + c.getString(1));
builder.append("\t" + c.getString(2) + "\n");
} while (c.moveToNext());
Toast.makeText(Main2Activity.this, builder.toString(), Toast.LENGTH_SHORT).show();
}
}
}
//MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText name, id, address;
Button insert, delete, display, update;
String Mname[]={"Asus","Nokia","Samsung","Oppo","Redmi","Vivo","Other"};
RadioButton rb;
RadioGroup rg;
Spinner sp;
SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = openOrCreateDatabase("register", Context.MODE_PRIVATE, null);
String tab = "create table if not exists newTable(name text,id text,address text)";
database.execSQL(tab);
name = findViewById(R.id.editText1);
id = findViewById(R.id.editText2);
address = findViewById(R.id.editText3);
insert = findViewById(R.id.button1);
delete = findViewById(R.id.button2);
update = findViewById(R.id.button3);
display = findViewById(R.id.button4);
rg=findViewById(R.id.rg);
sp=findViewById(R.id.sp);
ArrayAdapter adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,Mname);
sp.setAdapter(adapter);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (name.getText().toString().equals("")) {
name.setError("Enter your name");
name.requestFocus();
return;
}
if (id.getText().toString().equals("")) {
id.setError("Enter your id");
id.requestFocus();
return;
}
if (address.getText().toString().equals("")) {
address.setError("Enter address");
address.requestFocus();
return;
} else {
String insert = "insert into newTable values('" + name.getText().toString()+"','"+id.getText().toString() + "','" + address.getText().toString() + "')";
database.execSQL(insert);
Toast.makeText(MainActivity.this, "Insert success", Toast.LENGTH_SHORT).show();
}
}
});
display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (name.getText().toString().equals("")) {
name.setError("Enter name");
name.requestFocus();
return;
}
String d = "delete from newTable where name='" + name.getText().toString() + "'";
database.execSQL(d);
Toast.makeText(MainActivity.this, "Record delete", Toast.LENGTH_LONG).show();
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int select=rg.getCheckedRadioButtonId();
rb=findViewById(select);
String update="update newTable set id='"+id.getText().toString()+"',address='"+address.getText().toString()+"'where name='"+name.getText().toString()+"'";
database.execSQL(update);
Toast.makeText(MainActivity.this, "upadate Record", Toast.LENGTH_SHORT).show();
}
});
}
}
//display.java
//display.java
public class Main2Activity extends AppCompatActivity {
SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
database = openOrCreateDatabase("register", Context.MODE_PRIVATE, null);
String tab = "create table if not exists newTable(name text,id text,address text)";
database.execSQL(tab);
String s = "select * from newTable";
Cursor c = database.rawQuery(s, null);
StringBuilder builder = new StringBuilder();
if (c.moveToFirst()){
do {
builder.append("\t" + c.getString(0));
builder.append("\t" + c.getString(1));
builder.append("\t" + c.getString(2) + "\n");
} while (c.moveToNext());
Toast.makeText(Main2Activity.this, builder.toString(), Toast.LENGTH_SHORT).show();
}
}
}
Comments
Post a Comment