Android中ContentProvider详解
在 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 meth
补充:移动开发 , Android ,