..
In the previous lesson, we focused our attention on the internal storage type for example a text file where you can go to read and write. This method is suitable to store a limited amount of information, whereas if it has to do with data sets of considerable size, the writing and reading files is not the best choice.
This type of problem becomes particularly clear if there is a need to search through saved data. We think in fact find a certain word in a text file containing a very large data sets and the search word is placed in the bottom of the file. Obviously, the search would take some time as it would be necessary to scroll through the entire file.
In these situations you must use a database that dramatically improves performance for common operations can be performed on a set of data, such as research, data and aggornamento of a cancellation.
Android provides us with a SQLite database used by our application. The language to use to carry out any operation on the database is obviously the 'SQL. If the reader intends to develop an application that uses a database that documents should be hard on the SQL language in general and in particular boobies methodologies to be adopted to optimize the tables. In fact, an optimized database that has serious conceptual errors can nullify the advantage of having a database, because the benefits (calculated at runtime), for various basic operations, fall dramatically.
Given that optimized the design of database and free of conceptual errors is beyond the scope of this guide, in this tion will provide the reader with only the guidelines for using a SQLite database within an application (for more information refer to the SQL language ' special section on this site).
Let's start by saying that each database that the programmer creates is accessible from any part of the application class, but is not visible outside the application itself.
There are several ways to create a database, but what is better is what we will show shortly. This is essentially to extend the class and then make a SQLiteOpenHelper overriding the onCreate method to create the tables that essentially represent the structure of our database. An example is shown in the following code:
mydatabase {public class extends SQLiteOpenHelper
private static final int VERSIONE_DATABASE = 2;
private static final int database_name = "Test";
private static final String STRINGA_CREAZIONE_TABELLA = "CREATE TABLE test (" name TEXT PRIMARY KEY, name TEXT); "
Mydatabase (Context context) {
super (context, database_name, null, VERSIONE_DATABASE);
}
@ Override
public void onCreate (SQLiteDatabase db) {
db.execSQL (STRINGA_CREAZIONE_TABELLA);
}
}
As we can see three constants are declared: one for the version of the database, one for the name of the database and one containing the query for creating a table. Without what we call the constructor of our class, in which we are going to simply call the super method (which some parameters are passed) that the class constructor richimerà Auto-SQLiteOpenHelper going so that we have inherited to create the database.
Finally in the method onCreate we create our table by invoking the method execSQL passing the string as an attribute on the creation of the table.
We leave it to readers - and possibly future articles - the task of deepening the argument concerning populating, editing, and query the database in Android applications.
| |
Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €. |
| |
SQL and Database (Course)
Create and manage relational databases. From 39 €. |
| |
Visual Basic 6 (Course)
Make Desktop Applications with VB6. From 39 €. |