Android - 使用Content Provider
在用Content Provider時,通常會使用到一個class(使用者自訂)去extends SQLiteOpenHelper
其中會需要override public void onCreate(SQLiteDatabase db)
這個onCreate(SQLiteDatabase db)如果要被呼叫到
通常都是有使用到SQLiteOpenHelper裡的getWritableDatabase()
程式會檢查有無db,沒有就會呼叫此onCreate(SQLiteDatabase db) function!
例子 :
package com.android.GSensorActivity;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;
public class sensorProvider extends ContentProvider
{
public interface SensorSchema
{
String TABLE_NAME="SensorData";
String ORDER = "_order";
String DELTA = "delta";
String TIME = "time";
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "SensorDB";
private static final int DATABASE_VERSION = 1;
private DatabaseHelper(Context ctx)
{
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("DatabaseHelper","here");
}
// new a table
public void onCreate(SQLiteDatabase db)
{
Log.d("db onCreate()","yes");
// TODO Auto-generated method stub
String sql = "CREATE TABLE " + SensorSchema.TABLE_NAME + " ("
+ SensorSchema.ORDER + " INTEGER primary key autoincrement, "
+ SensorSchema.DELTA + " text not null ,"
+ SensorSchema.TIME + " text not null "+ ");";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS test");
onCreate(db);
}
}
static DatabaseHelper databaseHelper;
@Override
public int delete(Uri uri, String selection, String[] selectionArgs)
{
// TODO Auto-generated method stub
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.delete(SensorSchema.TABLE_NAME, selection, null);
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
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.insert(SensorSchema.TABLE_NAME, null, values);
db.close();
return null;
}
@Override
public boolean onCreate()
{
// TODO Auto-generated method stub
Log.d("onCreate(provider)","yes");
databaseHelper = new DatabaseHelper(getContext());
SQLiteDatabase db = databaseHelper.getWritableDatabase();
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
{
// TODO Auto-generated method stub
SQLiteDatabase db = databaseHelper.getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(SensorSchema.TABLE_NAME);
Cursor c = qb.query(db, projection, selection, selectionArgs, null,
null, null);
return c;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs)
{
// TODO Auto-generated method stub
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.update(SensorSchema.TABLE_NAME, values, selection, null);
return 0;
}
}
其中會需要override public void onCreate(SQLiteDatabase db)
這個onCreate(SQLiteDatabase db)如果要被呼叫到
通常都是有使用到SQLiteOpenHelper裡的getWritableDatabase()
程式會檢查有無db,沒有就會呼叫此onCreate(SQLiteDatabase db) function!
例子 :
package com.android.GSensorActivity;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;
public class sensorProvider extends ContentProvider
{
public interface SensorSchema
{
String TABLE_NAME="SensorData";
String ORDER = "_order";
String DELTA = "delta";
String TIME = "time";
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "SensorDB";
private static final int DATABASE_VERSION = 1;
private DatabaseHelper(Context ctx)
{
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("DatabaseHelper","here");
}
// new a table
public void onCreate(SQLiteDatabase db)
{
Log.d("db onCreate()","yes");
// TODO Auto-generated method stub
String sql = "CREATE TABLE " + SensorSchema.TABLE_NAME + " ("
+ SensorSchema.ORDER + " INTEGER primary key autoincrement, "
+ SensorSchema.DELTA + " text not null ,"
+ SensorSchema.TIME + " text not null "+ ");";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS test");
onCreate(db);
}
}
static DatabaseHelper databaseHelper;
@Override
public int delete(Uri uri, String selection, String[] selectionArgs)
{
// TODO Auto-generated method stub
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.delete(SensorSchema.TABLE_NAME, selection, null);
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
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.insert(SensorSchema.TABLE_NAME, null, values);
db.close();
return null;
}
@Override
public boolean onCreate()
{
// TODO Auto-generated method stub
Log.d("onCreate(provider)","yes");
databaseHelper = new DatabaseHelper(getContext());
SQLiteDatabase db = databaseHelper.getWritableDatabase();
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
{
// TODO Auto-generated method stub
SQLiteDatabase db = databaseHelper.getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(SensorSchema.TABLE_NAME);
Cursor c = qb.query(db, projection, selection, selectionArgs, null,
null, null);
return c;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs)
{
// TODO Auto-generated method stub
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.update(SensorSchema.TABLE_NAME, values, selection, null);
return 0;
}
}
留言
張貼留言