一、在AndroidManifest.xml文件中添加
成都创新互联成立与2013年,先为濮阳县等服务建站,濮阳县等地企业,进行企业商务咨询服务。为濮阳县企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
二、创建数据库SqliteOpenHelper
public class DbHelper extends SQLiteOpenHelper { private static String name = "mydb.db"; private static int version = 1;// 初始的版本号是一 public DbHelper(Context context) { super(context, name, null, version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase database) { // TODO Auto-generated method stub String sql = "create table student (id integer primary key autoincrement ,name varchar(64) ,address varchar(64))"; database.execSQL(sql);// 对数据库的表的创建 } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
三、创建ContentProvider
public class StudentProvider extends ContentProvider { private final String TAG = "StudentProvider"; private DbHelper helper = null; private static final UriMatcher URI_MATCHER = new UriMatcher( UriMatcher.NO_MATCH); private static final int STUDENT = 1;// 操作单条记录 private static final int STUDENTS = 2;// 操作多条记录 static { URI_MATCHER.addURI( "com.example.android_contentprovider2.StudentProvider", "student", STUDENTS); URI_MATCHER.addURI( "com.example.android_contentprovider2.StudentProvider", "student/#", STUDENT); } public StudentProvider() { // TODO Auto-generated constructor stub } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { // TODO Auto-generated method stub int count = -1;// 影响数据库的行数 try { int flag = URI_MATCHER.match(uri); SQLiteDatabase database = helper.getWritableDatabase(); switch (flag) { case STUDENT: // content://com.example.android_contentprovider2.StudentProvider/student/1 // delete from student where id = ? //id 通过客户端传递过来的 long id = ContentUris.parseId(uri); String where_value = " id = " + id; if (selection != null && !selection.equals("")) { where_value += " and " + selection; } count = database.delete("student", where_value, selectionArgs); break; case STUDENTS: count = database.delete("student", selection, selectionArgs); break; } } catch (Exception e) { // TODO: handle exception } return count; } @Override public String getType(Uri uri) { // TODO Auto-generated method stub int flag = URI_MATCHER.match(uri); switch (flag) { case STUDENT: return "vnd.android.cursor.item/student"; case STUDENTS: return "vnd.android.cursor.dir/students"; } return null; } @Override public Uri insert(Uri uri, ContentValues values) { // TODO Auto-generated method stub // insert into student () (?,?); Uri resultUri = null; int flag = URI_MATCHER.match(uri); switch (flag) { case STUDENTS: SQLiteDatabase database = helper.getWritableDatabase(); long id = database.insert("student", null, values);// 插入当前行的行号 resultUri = ContentUris.withAppendedId(uri, id); break; } Log.i(TAG, "---->>" + resultUri.toString()); return resultUri; } @Override public boolean onCreate() { // TODO Auto-generated method stub helper = new DbHelper(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO Auto-generated method stub Cursor cursor = null; try { SQLiteDatabase database = helper.getReadableDatabase(); int flag = URI_MATCHER.match(uri); switch (flag) { case STUDENT: long id = ContentUris.parseId(uri); String where_value = " id = " + id; if (selection != null && !selection.equals("")) { where_value += " and " + selection; } cursor = database.query("student", null, where_value, selectionArgs, null, null, null, null); break; case STUDENTS: cursor = database.query("student", null, selection, selectionArgs, null, null, null); break; } } catch (Exception e) { // TODO: handle exception } return cursor; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO Auto-generated method stub int count = -1; try { // update table set name = ? ,address = ? where id = ? SQLiteDatabase database = helper.getWritableDatabase(); long id = ContentUris.parseId(uri); int flag = URI_MATCHER.match(uri); switch (flag) { case STUDENT: String where_value = " id = " + id; if (selection != null && !selection.equals("")) { where_value += " and " + selection; } count = database.update("student", values, where_value, selectionArgs); break; } } catch (Exception e) { // TODO: handle exception } return count; } }