Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package test.data.com.sqlite;
  2.  
  3. import android.content.Intent;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. Button Insert, Exit, Display;
  15. EditText e1,e2;
  16. String nam, coll;
  17. SQLiteDatabase db;
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23.  
  24. Insert = (Button) findViewById(R.id.ins);
  25. Exit= (Button) findViewById(R.id.ext);
  26. Display= (Button) findViewById(R.id.dis);
  27.  
  28. e1 = (EditText) findViewById(R.id.e1);
  29. e2 = (EditText) findViewById(R.id.e2);
  30.  
  31. db = openOrCreateDatabase("Mydb",MODE_PRIVATE,null);
  32. db.execSQL("CREATE TABLE IF NOT EXISTS student(name VARCHAR,college VARCHAR);");
  33.  
  34. Insert.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. nam = e1.getText().toString();
  38. coll = e2.getText().toString();
  39. db.execSQL("INSERT INTO student VALUES('"+nam+"','"+coll+"');");
  40. Toast.makeText(getApplicationContext(),"Row Inserted",Toast.LENGTH_SHORT).show();
  41.  
  42. }
  43. });
  44.  
  45. Exit.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. System.exit(0);
  49. }
  50. });
  51.  
  52. Display.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. Intent intent = new Intent(MainActivity.this, Display.class);
  56. startActivity(intent);
  57. finish();
  58. }
  59. });
  60.  
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement