日期:2014-05-16  浏览次数:20420 次

android 的数据库操作,以及contentProvider使用

我们首先来看看SQLite在 Android中的使用,在Android中有最简单的使用方式,就是利Context对象的openorCreateDatabase
我们来看看一段代码:

package test.DB;

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必须重载并实现,

package test.DB;

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;
}
}

再创建一个使用者(消费者)

package test.DB;

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 DBA