在 Android中,對於數據的存貯,有幾種方式,有文件方式,有文件式數據庫方式,網絡數據,Android自帶瞭一個SQLite數據庫,做嵌入開發的人對這個跨平臺的本地文件數據庫一定不會陌生,在Android對於數據的訪問,提出瞭新的方式,ContentProvider方式,我們首先來看看SQLite在 Android中的使用,在Android中有最簡單的使用方式,就是利Context對象的openorCreateDatabase
我們來看看一段代碼:
Java代碼
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DBActivity extends Activity {
private SQLiteDatabase mdb = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDatabase();
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
menu.add(1, Menu.FIRST +1, 1, "GetName");
menu.add(1, Menu.FIRST +2, 2, "Test");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case Menu.FIRST+ 1:
{
TextView tv = (TextView)this.findViewById(R.id.view_main);
tv.setText(this.getName());
break;
}
case Menu.FIRST + 2:
{
this.setTitle("Del Item…");
break;
}
}
return true;
}
private void initDatabase()
{
mdb = this.openOrCreateDatabase("dbfile", 0, null);
String sql_create = "create table test (id int, name TEXT)";
mdb.execSQL(sql_create);
String sql_insert = "insert into test(id, name) values(3, 'name3')";
mdb.execSQL(sql_insert);
}
private String getName()
{
String name = null;
Cursor cur = mdb.rawQuery("select * from test", null);
cur.moveToFirst();
while (!cur.isLast())
{
name = name + cur.getString(1)+ "\r\n";
cur.moveToNext();
}
return name;
}
}
我們再來看看單獨使用SQLiteOpenHelper方式:
先由SQLiteOpenHelper繼承一個數據庫操作類,其中onCreate, onUpgrade必須重載並實現,
Java代碼
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public DatabaseHelper(Context ct,String dbName)
{
super(ct,dbName,null,1);
}
public boolean insert(String insert)
{
this.getWritableDatabase().execSQL(insert);
return true;
}
public boolean update(String update)
{
this.getWritableDatabase().execSQL(update);
return true;
}
public boolean delete(String del)
{
this.getWritableDatabase().execSQL(del);
return true;
}
public Cursor query(String query)
{
Cursor cur = this.getReadableDatabase().rawQuery(query, null);
return cur;
}
}
再創建一個使用者(消費者)
Java代碼
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DBActivity extends Activity {
private SQLiteDatabase mdb = null;
private DatabaseHelper helper = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDatabase();
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
menu.add(1, Menu.FIRST +1, 1, "GetName");
menu.add(1, Menu.FIRST +2, 2, "Test");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case Menu.FIRST+ 1:
{
TextView tv = (TextView)this.findViewById(R.id.view_main);
tv.setText(this.getName());
break;
}
case Menu.FIRST + 2:
{
this.setTitle("Del Item…");
break;
}
}
return true;
}
private void initDatabase()
{
helper = new DatabaseHelper(this,"dbfile");
//mdb = this.openOrCreateDatabase("dbfile", 0, null);
//String sql_create = "create table test (id int, name TEXT)";
//mdb.execSQL(sql_create);
String sql_insert = "insert into test(id, name) values(55, 'namexx')";
//mdb.execSQL(sql_insert);
helper.insert(sql_insert);
}
private String getName()
{
String name = null;
//mdb = helper.getReadableDatabase();
Cursor cur = helper.query("select * from test");
cur.moveToFirst();
do
{
name = name + cur.getString(1)+ "\n";
} while(cur.moveToNext());
return name;
}
}
在來看看使用ContentProvider
Java代碼
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.widget.Toast;
public class TestProvider extends ContentProvider {
private DatabaseHelper mdbHelper = null;
final static String TABLE_NAME = "test";
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
mdbHelper.getWritableDatabase().delete(TABLE_NAME, arg1, arg2);
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
mdbHelper.getWritableDatabase().insert(TABLE_NAME, "", values);
return null;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
mdbHelper = new DatabaseHelper(this.getContext(), "dbfile");
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Cursor cur = mdbHelper.getReadableDatabase().query(TABLE_NAME,projection,selection,null,null,null, null);
Toast.makeText(this.getContext(), "test Cur!", Toast.LENGTH_SHORT);
return cur;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
mdbHelper.getWritableDatabase().update(TABLE_NAME, values, selection, null);
return 0;
}
}
老規矩,AndroidManifest.xml來定義這個Provider
<provider android:label="test_provider" android:authorities="com.my.provider" android:name="TestProvider">
</provider>
這裡的android:authorities = "com.my.provider"是標識這個ContentProvider,調用者可以根據這個標識來找到它,
我們組合一個能找到它的Uri,
public class ProviderConst {
public static final Uri MY_TEST_URI = Uri.parse("content://com.my.provider/test");
}
content 指的是內容提供者ContentProvider。
//com.my.provider映射到我們已定義的那個ContentProvider標識
/test這個作為一個參數,傳給ContentProvider,可以根據這個參數來決定操作目標,比如數據庫中的哪張表,文件中的那一部分數據等。
我們來操作這個內容提供者:
Java代碼
private String getName()
{
String name = null;
Cursor cur = this.getContentResolver().query(ProviderConst.MY_TEST_URI,new String[]{"id","name"},null, null, null);
if (cur == null)
return null;
cur.moveToFirst();
do
{
name = name + cur.getString(1)+ "\n";
} while(cur.moveToNext());
return name;
}