Android data storage

    In my last post I wrote about creating pop-up menus, today we’ll talk about a more important topic such as data storage. There are several ways to store data in android: general settings, db, and so on. In this post I will talk about how to store data in the database.
    Android uses embedded SQLite as the database. SQLite is a very fast database, so its use on a mobile platform does not lead to a sharp decrease in performance. Let's move on to the code description. Google took care of our nerves and wrote a small class utility SQLiteOpenHelper.

    public class DbOpenHelper extends SQLiteOpenHelper{

      private static final int DB_VERSION = 1;
      private static final String DB_NAME = "test";

      public static final String TABLE_NAME = "users";
      public static final String LOGIN = "login";
      public static final String PASSW = "passw";
      private static final String CREATE_TABLE = "create table " + TABLE_NAME + " ( _id integer primary key autoincrement, "
          + LOGIN + " TEXT, " + PASSW + " TEXT)";

      public DbOpenHelper(Context context) {
        super(context, DB_NAME, null,DB_VERSION);
      }

      @Override
      public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE);
      }

      @Override
      public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
      }
    }

    * This source code was highlighted with Source Code Highlighter.


    When creating an instance of the DbOpenHelper class, it will be checked whether the database named test exists, if it exists, the onUpgrade method will be called, if not, then onCreate in which we create the users table (usually in this method tables are created and initialized with default values). The SQLiteOpenHelper class has getReadableDatabase and getWritableDatabase methods that return an instance of the SQLiteDatabase class. Using this instance, we will work with the database. It has all the methods we need: insert, update, query, delete, and so on.
    Let's write a small application that saves username and password to the database

    public class TestActivity extends Activity {

      EditText loginEditText = null;
      EditText passEditText = null;
      Button saveButton = null;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        loginEditText = (EditText) findViewById(R.id.login);
        passEditText = (EditText) findViewById(R.id.passw);
        saveButton = (Button) findViewById(R.id.btn1);

        saveButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
            DbOpenHelper dbOpenHelper = new DbOpenHelper(TestActivity.this);
            SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(DbOpenHelper.LOGIN,loginEditText.getText().toString());
            cv.put(DbOpenHelper.PASSW,passEditText.getText().toString());
            db.insert(DbOpenHelper.TABLE_NAME,null,cv);
            db.close();
            loginEditText.setText("");
            passEditText.setText("");
          }
        });
      }
    }

    * This source code was highlighted with Source Code Highlighter.


    In the example, we have a window in which there are two input fields and a button, and when the button is pressed, it is saved. First, we create an instance of the DbOpenHelper class, which creates the database itself and the tables. Then we get the SQLiteDatabase object, the insert method of which will record. ContentValues ​​is a kind of wrapper over the data that will be written to the database. In the put method, the first argument is the column name, and the second the data that will be written to the column, in SQL, it looks like this:
    INSERT INTO users ( 'login','passw') VALUES ('somelogin','somepass')
    As a homework, try to get the data from the database and display it on the screen.
    Project sources can be downloaded here .
    I will answer questions in the comments.
    PS Original on my blog
    PSS I want to start a large project for android, I need a programmer and designer.

    Also popular now: