In my last
post I wrote about creating pop-up menus, but today we will talk about a more important topic such as data storage. In android there are several ways to store data: general settings, database and so on. In this post I will talk about how to store data in the database.
As a database, android uses embedded SQLite. SQLite is a very fast base, so its use on the mobile platform does not lead to a dramatic decrease in performance. Let us turn to the description of the code. 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 a database exists with the name test, if it exists, then the onUpgrade method will be called, if not, then onCreate in which we create the users table (usually in this method they create tables and initialize them with default values). The SQLiteOpenHelper class has getReadableDatabase and getWritableDatabase methods that return an instance of the SQLiteDatabase class. With 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 login 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 class DbOpenHelper, which creates the base itself and the tables. Then we get a SQLiteDatabase object, the insert method of which will write. ContentValues is a kind of wrapper over the data that will be recorded in the database. In the put method, the first argument is the name of the column, and the second is the data itself that will be written to the column, in SQL language it looks like this:
INSERT INTO users ( 'login','passw') VALUES ('somelogin','somepass')
As a homework, try to get 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
blogPSS I want to start a big project for android, I need a programmer and a designer.