Guest User

Untitled

a guest
Aug 3rd, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. Android SQLite - small I/O difficulty
  2. public class DBExampleActivity extends Activity implements OnClickListener{
  3.  
  4.  
  5. /** Called when the activity is first created. */
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9.  
  10. // Declare the database object and open.
  11. MyDBManager db = new MyDBManager(this);
  12. db.open();
  13.  
  14. // Take names from EditTexts and store in string.
  15. first_name = fname.getText().toString();
  16. last_name = lname.getText().toString();
  17. }
  18.  
  19. @Override
  20. public void onClick(View view) {
  21. // In the onclick method, add values to database, call next activity
  22. db.insertData(first_name, last_name);
  23. Intent intent = new Intent(this, Display.class);
  24. startActivity(intent);
  25. }}
  26.  
  27. public class Display extends Activity {
  28.  
  29. /** Called when the activity is first created. */
  30. @Override
  31. public void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.main);
  34.  
  35. Cursor myCursor = db.getDetails();
  36.  
  37. // Get relevant data from columns in cursor.
  38. username = c.getString(c.getColumnIndex("first_name"));
  39. password = c.getString(c.getColumnIndex("last_name"));
  40.  
  41. // set textview values.
  42. fname.setText(first_name);
  43. lname.setText(last_name);
  44. }}
  45.  
  46. public class MyDBManager {
  47. public static final String KEY_ROWID = "id";
  48. public static final String KEY_USERNAME = "firstname";
  49. public static final String KEY_PASSWORD = "lastname";
  50.  
  51. private static final String DATABASE_NAME = "User_Information";
  52. private static final String DATABASE_TABLE = "User_name";
  53. private static final int DATABASE_VERSION = 1;
  54.  
  55. private static final String DATABASE_CREATE =
  56. "create table " + DATABASE_TABLE +
  57. " (_id integer primary key autoincrement, " +
  58. "username text not null, " +
  59. "password text not null); " ;
  60.  
  61. private final Context context;
  62. private DatabaseHelper DBHelper;
  63. private SQLiteDatabase db;
  64.  
  65.  
  66. public MyDBManager(Context context){
  67. this.context = context;
  68. DBHelper = new DatabaseHelper(context);
  69. }
  70.  
  71. private static class DatabaseHelper extends SQLiteOpenHelper{
  72.  
  73. DatabaseHelper(Context context){
  74. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  75. }
  76.  
  77. // Creates the database
  78. @Override
  79. public void onCreate(SQLiteDatabase db){
  80. db.execSQL(DATABASE_CREATE);
  81. }
  82.  
  83. @Override
  84. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
  85. // on triggered if the database version has increased.
  86. // used if you want to change structure of column: eg add new column.
  87. }
  88. } // end helper class
  89.  
  90.  
  91.  
  92. // Methods used to access and change data in DB:
  93. public MyDBManager open() throws SQLException{
  94. db = DBHelper.getWritableDatabase();
  95. return this;
  96. }
  97.  
  98.  
  99. public void close(){
  100. DBHelper.close();
  101. }
  102.  
  103. public long insertData(String username, String password){
  104. ContentValues initialValues = new ContentValues();
  105. initialValues.put(KEY_USERNAME, username);
  106. initialValues.put(KEY_PASSWORD, password);
  107.  
  108. return db.insert(DATABASE_TABLE, null, initialValues);
  109. }
  110.  
  111.  
  112. public Cursor getDetails(long rowId){
  113. Cursor c = db.query(true, DATABASE_TABLE, new String[]{
  114. KEY_ROWID,
  115. KEY_USERNAME,
  116. KEY_PASSWORD},
  117. KEY_ROWID + "=" + rowId,
  118. null,
  119. null,
  120. null,
  121. null,
  122. null);
  123.  
  124. if(c != null){
  125. c.moveToFirst();
  126. }
  127. return c;
  128. }}// End myDBManager Class.
  129.  
  130. TextView fname = (TextView) findViewById(R.id.mytextview1);
  131. TextView lname = (TextView) findViewById(R.id.mytextview2);
  132. // set textview values.
  133. fname.setText(first_name);
  134. lname.setText(last_name);
Add Comment
Please, Sign In to add comment