import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
public class MyContentProvider extends ContentProvider {
private SQLiteDatabase db;
private static final String DATABASE_NAME = "mydb.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "mytable";
private static final String CREATE_TABLE_STMT =
"CREATE TABLE " + TABLE_NAME + " (_id INTEGER PRIMARY KEY, name TEXT);";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(ContentProvider provider) {
super(provider.getContext(), DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_STMT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Implement schema migration logic if needed
}
}
@Override
public boolean onCreate() {
DatabaseHelper helper = new DatabaseHelper(this);
db = helper.getWritableDatabase();
return (db == null) ? false : true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
return db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
}
@Override
public String getType(Uri uri) {
// Implement if needed, or throw UnsupportedOperationException
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = db.insert(TABLE_NAME, "", values);
return Uri.parse(CONTENT_URI + "/" + rowID);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return db.delete(TABLE_NAME, selection, selectionArgs);
}
@Override
public
评论已关闭