Android-sqlite Commercial Database Operations
A specific data operation
Increase
/**
* Insert User Instance
* @param puser
* @return
*/
Public long Insertuser (User puser)
{
if (msqlitedatabase! = NULL && Puser! = null)
{
Insert a record
Contentvalues _contentvalues = new Contentvalues ();
_contentvalues.put (Column_username, Puser.getmusername ());
_contentvalues.put (Column_user_address, puser.getmuseraddress ());
return mSqLiteDatabase.insert(TABLE_NAME, null, _contentValues); } else { return -1; }}
By deleting
/**
* Delete User Instances
* @param ID
* @return
*/
public int deleteuser (int id)
{
if (msqlitedatabase! = null && ID > 0)
{
Return Msqlitedatabase.delete (table_name, "_id=?", new String[]{string.valueof (ID)});
} return -1; }
Change
/**
* Update the user's instance
* @param puser
* @param ID
* @return
*/
public int Updatauser (User puser, int id)
{
if (msqlitedatabase! = NULL && Puser! = null)
{
Contentvalues _contenvalues = new Contentvalues ();
_contenvalues.put (Column_username, Puser.getmusername ());
_contenvalues.put (Column_user_address, puser.getmuseraddress ());
return mSqLiteDatabase.update(TABLE_NAME, _contenValues,"_id=?" , new String[]{String.valueOf(id)}); } return -1;}
Check
/**
* Check all records
*/
Public Cursor SelectAll ()
{
if (msqlitedatabase! = null)
{
Return Msqlitedatabase.query (
TABLE_NAME,
New string[]{"_id", Column_username, column_user_address},
Null
Null
Null
Null
NULL);
}
return null;
}
/**
* Query user data based on criteria
* @param selection
* @param Selectionargs
* @param groupBy
* @param have
* @param
* @return
*/
Public Cursor Selectuser (string selection, string[] Selectionargs, String groupBy, string have, string-by-clause)
{
if (msqlitedatabase! = null)
{
Return Msqlitedatabase.query (
TABLE_NAME,
New string[]{"_id", Column_username, column_user_address},
Selection
Selectionargs,
GroupBy,
Having
);
}
Else
{
return null;
}
}
Batch update and delete
Using native database statements
Exesql
Rawsql
Querywithfactory High Efficiency ratio
two sqlite3 database engine
Download Sqliter3 source code, analyze file structure, and then conduct source analysis
The result is that under the Android2.3.3\external\sqlite\dist folder, all the extended third-party class libraries are under the external folder,
Unified System Compilation
three sqlite3 connection mode
The way of JDBC
Like the native library of JNI, the bottom layer is also implemented by C + +
Under the Android2.3.3\libcore\sqlite-jdbc\src\main\native and Java folders,
Four primary foreign key relationships for tables
Business projects require rigorous primary foreign key relationships
The tool used is Sqlitestudio
Five-table View
But you can use the view of a table when the SQL statement is not familiar
Six Export Database
Export the database to the code at the Exesql execution of the project
Seven Business
Similar to the concept of atomic commit, where a transaction is a whole process
Try to put it in the Try-catch.
BeginTransaction ()
The middle is the business logic, the transaction operation
Endtransaction ()
Commit ()
eight-side questions
1 talking about several forms of Android data storage
Sharepreference, file, network, SQLite3 database
2 can sqlite support stored procedures, support the primary foreign key association?
Support for primary foreign key associations
3 How to integrate your setting program with the system's setting program
Application system under the Preferencescreen tag in the XML file of their setting program intent
Android-sqlite Commercial Database operations