- In some cases, the file is not a valid multi-threaded data access if Ø is Ø -related applications that may change if the complex data structures, etc. Ø
- So, Android has built-in SQLite database support to bring
- Database package suite for creating them are private
- Database should not be used to store files
- Tip: In the SDK in the samples / NotePad can be found under the examples on how to use the database
- SQLite is a lightweight software library
- Implement a fully adapt to severe environmental database Ø Ø atomic weight of rugged durability Ø Ø independence
- Volume size of only a few thousand bytes
- Some SQL commands only partially supported, for example: ALTER, TABLE
- See http://www.sqlite.org for more information
- *****************************************
Create Database
- Context.createDatabase (String name, int version, int mode, CursorFactory factory) to create a new database and returns a SQLiteDatabase object
- If the database can not be created, then throw FileNotFoundException exception
- New method to create SQLite database
- ****************************************
SQLiteDatabase myDataBase=this.openOrCreateDatabase("myDataBase.db", MODE_PRIVATE, new CursorFactory(){ // Create a new database, the name myDatabase, Model MODE_PRIVATE, mouse factory // The factory classes, an optional factory class , When a query is called to instantiate a cursor @Override public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) { // TODO Auto-generated method stub return null; } });
*******************************************
If the database is deleted successfully returned true, failure was false (such as a database does not exist)
- Delete Database
- Context.deleteDatabase (String name) Remove the specified name of the database
*******************************************
// Create a file named :myDataBase Database, Postfix .db
SQLiteDatabase my_DataBase=this.openOrCreateDatabase("myDateBase.db",
MODE_PRIVATE, null);
my_DataBase.close();// Don't forget to close the database
******************************************
Open the database
Context.openDatabase (String file, CursorFactory factory) to open an existing database and returns a SQLiteDatabase object
If the database does not exist throw FileNotFoundException exception
*******************************************
// Create a file named :myDataBase Database, Postfix .db
SQLiteDatabase my_DataBase=this.openOrCreateDatabase("myDateBase.db", MODE_PRIVATE, null);
my_DataBase.close();// Don't forget to close the database
*******************************************
Non-query SQL commands
SQLiteDatabase.execSQL (String sql) can be used to execute non-query SQL commands, these commands do not result
- Including: CREATE TABLE / DROP TABLE / INSERT, etc.
- For example:
- Create a file named "test" and a table with two parameters
-
- **********************************************
-
// Create a group named "test" and with two parameters table
my_DataBase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY,
someNumber INTERGER);");
- **********************************************
2. In the database to insert a tuple
/ In the database into a tuple
my_DataBase.execSQL("INSERT INTO test (_id,someNumber) values(1,8);");
*********************************************
3. Remove Table
// Delete table
my_DataBase.execSQL("DROP TABLE test");
*******************************************
Query SQL command - Cursor Cursors
- Android use the cursor (Cursors) to navigate browse query results
- Cursor (Cursors) to describe the object to be android.database.Cursor
- A cursor (Cursors) is a simple pointer, which results from a query to the next tuple a tuple (or the previous or the first or ... ...)
- Cursor (Cursors) Location Location in the moment it returned tuple data
// In order to create a Cursor( Cursor ), You must perform a query, either by SQL Use rawQuery() Method
// Or a more elaborate methods, like query() Method
Cursor cur=my_DataBase.rawQuery("SELECT * FORM test", null);
if(cur!=null){// The cursor is not empty
// Returns the columns of the given name based on the 0-based index, If this property returns the column does not exist -1
// Through their index to retrieve the property values
int numColumn=cur.getColumnIndex("someNumber");
if(cur.moveToFirst()){
//cur.moveToFirst() Let the cursor to the first row, if the cursor is pointing to the first row , Returns true
do {
int num=cur.getInt(numColumn);// Get the current row of the value of this property
/*Cursor Provides a different method to return a different data type cable
For example, getInt(int index)/getString(int index) And so on */
/* Do something */
} while (cur.moveToNext());
/* Moves the cursor to the next line, if the cursor has passed the end of the result set ,
That is, there are no rows can be moved, then the return false*/
// Other possible move previous() And first() Method
}
}
****************************************************
database application sample
Part code
- Enter the new contact information added to the database
// The new import contact information into the database
private void addToDataBase(String name_str2, String phone_str2, String email_str2, String address_str2) {
// TODO Auto-generated method stub
String sqlCmd="INSERT INTO myPhoneBook(Pname,Pphone,Pemail,Paddress) values('"+name_str2+"','"+phone_str2+"','"+email_str2+"','"+address_str2+"');";
try {
myPhoneBookDB.execSQL(sqlCmd);
ShowNoteInformation(" Added successfully !!");
} catch (Exception e) {
// TODO: handle exception
ShowNoteInformation(" Add failed, record already exists !!");
}
Log.i("MYTEST", sqlCmd);
}
*****************************************************
Database to find contact information
setMainScreen(R.layout.persionlist);
setActionListener(R.layout.persionlist);
// Instantiate the ArrayList
arraylistmap=new ArrayList
****************************************
The database to remove the phone records
ShowNoteInformation(" Delete the call log ");
myPhoneBookDB.execSQL("DELETE FROM myPhoneBook WHERE Pname='"+my_name+"';");
Log.i("MYTEST", "DELETE FROM myPhoneBook WHERE Pname='"+my_name+"';");
cleanShow();
No comments:
Post a Comment