The operation of data is often involved in Android development. Android itself provides four ways of storing data. Includes:sharepreference,sqlite,content provider,file.
In Android, data is private, and the correct way to achieve data sharing is to use content Provider.
SQLite: SQLite is a lightweight database that supports basic SQL syntax and is a commonly used method of data storage. Android provides a class named Sqlitedatabase for this database, encapsulating some APIs for manipulating the database.
sharedpreference: In addition to SQLite database, another common way of data storage, its essence is an XML file, often used to store simpler parameter settings.
File : that is, a commonly used document (I/O) storage method, often used to store a large number of data, but the disadvantage is that updating the data will be a difficult thing.
ContentProvider: An Android system that enables all applications to share a way of storing data, because the data is usually private among applications, so this storage is less used, but it is an essential storage method. For example, audio, video, pictures, and address books can be stored in this way in general. Each content provider provides a public URI (wrapped as a URI object), and if the application has data to share, it needs to use the content provider to define a URI for the data, and then the other applications pass the content The provider is passed in to the URI to manipulate the data.
This article is mainly about some sqlite operation and enclose the code.
SQLite is a lightweight database designed for embedded devices with only five data types, respectively:
null: null value
Integer : integers
Real : floating point numbers
TEXT: string
BLOB: Large data
In SQLite, the Boolean and date types are not specifically designed because a Boolean can replace true and false with integers 0 and 1, whereas a date type can have the value of text, real, and integer in a particular format in place of the display. To facilitate the operation of the date type, SQLite provides a set of functions, as detailed in the following:http://www.sqlite.org/lang_datefunc.html, this simple data type design is more in line with the requirements of embedded devices.
Import android.content.ContentValues;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase; /** * The operation of checking the deletion of the table.
The list here is only a list of UID as an example.
* * public class Demodbutil {//The table name to be manipulated public static final String demo_db_name = "Demo_db_name";
Sqlitedatabase object Private Sqlitedatabase sqlitedatabase;
/** * takes the parameter construction method, initializes the sqlitedatabase. * @param sqlitedatabase Sqlitedatabase Instance * * Public demodbutil (Sqlitedatabase sqlitedatabase) {this.sqlitedatabase
= Sqlitedatabase;
}/** * Add data.
* @param uid to add the UID */public void Add (String uid) {contentvalues values = new Contentvalues ();
Values.put ("UID", UID);
Sqlitedatabase.insert (demo_db_name, null, values);
/** * Deletes all data in the table and table.
*/public void Delete () {sqlitedatabase.delete (demo_db_name, NULL, NULL);
/** * Deletes the data that corresponds to the UID. * @param uid a uid/public void Delete (String uid) {sqlitedatabase.delete (demo_db_name, "UID =?", New string[]
{UID}); /** * Updates the table to replace all of the table's UID with Newuid.
*/public void update () {contentvalues values = new Contentvalues ();
Values.put ("UID", "Newuid");
Sqlitedatabase.update (demo_db_name, values, NULL, NULL);
/** * replaces the corresponding UID of the table with the Newuid.
* @param a UID/public void update (String uid) in the UID table {contentvalues values = new Contentvalues ();
Values.put ("UID", "Newuid");
Sqlitedatabase.update (Demo_db_name, values, "UID =?", new String[]{uid});
/** * All data in the query table, get the value of the UID corresponding.
*/public void query () {Cursor Cursor = sqlitedatabase.query (demo_db_name, NULL, NULL, NULL, NULL, NULL, NULL);
while (Cursor.movetonext ()) {String uid = cursor.getstring (Cursor.getcolumnindex ("UID"));
} if (null!= cursor) {cursor.close ();
}/** * Gets the value of the corresponding UID. * @param a UID/public void query (String uid) {Cursor Cursor = sqlitedatabase.query (Demo_db_name, NULL, UID =?
", New String[]{uid}, NULL, NULL, NULL); while (cursor.MoveToNext ()) {String Queryuid = cursor.getstring (Cursor.getcolumnindex ("UID"));
} if (null!= cursor) {cursor.close ();
}/** * Gets the last record.
*/public void Querylastrecord () {String lasttimesql = ' SELECT * from Demo_db_name ORDER BY id desc LIMIT 1 ';
Cursor Cursor = sqlitedatabase.rawquery (lasttimesql, NULL);
while (Cursor.movetonext ()) {String Queryuid = cursor.getstring (Cursor.getcolumnindex ("UID"));
} if (null!= cursor) {cursor.close ();
}
}
}
The above is mainly for the table of a simple deletion and modification of the operation, can be modified according to the actual situation to meet the needs of the project.
The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.