我是Newbee to
Database概念,特别是我需要相关的数据库概念..我想要一个使用sqlite数据库for android的例子.我走过android开发者网站,但发现很少有关于数据库示例的东西..
因为我是数据库概念的新手任何人都可以建议我一些很好的资源来理解create(),insert(),Query()……等等.如果我得到任何使用android的示例程序会更好.
因为我是数据库概念的新手任何人都可以建议我一些很好的资源来理解create(),insert(),Query()……等等.如果我得到任何使用android的示例程序会更好.
提前致谢
解决方法
它很简单,请按照下面的代码…
创建3个名为例如的类 –
1.我使用了Constants接口,参考完整代码并找到它的用途
创建3个名为例如的类 –
1.我使用了Constants接口,参考完整代码并找到它的用途
package com.mypackage.quaddeals; import android.net.Uri; import android.provider.BaseColumns; public interface Constants extends BaseColumns { public static final String TABLE_NAME = "user_deal"; public static final String AUTHORITY = "com.mypackage.quaddeals"; public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME); public static final String TITLE = "title"; public static final String CITYID = "cityid"; }
2.BruPress数据类……
package com.mypackage.quaddeals; import static android.provider.BaseColumns._ID; import static com.mypackage.quaddeals.Constants.CITYID; import static com.mypackage.quaddeals.Constants.TITLE; import static com.mypackage.quaddeals.Constants.TABLE_NAME; import android.content.Context; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteOpenHelper; import android.util.Log; public class Bru_Press_Data extends sqliteOpenHelper { public static final String DATABASE_NAME = "QuadDeals.db"; public static final int DATABASE_VERSION = 1; /** Create a helper object for the Events database */ public Bru_Press_Data(Context ctx) { super(ctx,DATABASE_NAME,null,DATABASE_VERSION); } @Override public void onCreate(sqliteDatabase db) { try { db.execsql("CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + TITLE + " TEXT UNIQUE," + CITYID + " TEXT);"); } catch (Exception e) { Log.v("Bru_Press_Data","exception in table created"); } } @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { db.execsql("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } }
3.Provider类
package com.mypackage.quaddeals; import static android.provider.BaseColumns._ID; import static com.mypackage.quaddeals.Constants.CONTENT_URI; import static com.mypackage.quaddeals.Constants.AUTHORITY; import static com.mypackage.quaddeals.Constants.TABLE_NAME; import android.app.SearchManager; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; import android.net.Uri; import android.text.TextUtils; import android.util.Log; public class Bru_Press_Provider extends ContentProvider { private static final int QUADUSER = 1; private static final int QUADUSER_ID = 2; /** The MIME type of a directory of events */ private static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.example.brown"; /** The MIME type of a single event */ private static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.example.brown"; private static final String CONTENT_TYPE1 = "vnd.android.cursor.dir/vnd.example.brown"; /** The MIME type of a single event */ private static final String CONTENT_ITEM_TYPE1 = "vnd.android.cursor.item/vnd.example.brown"; // private static final String CONTENT_TYPE2 = "vnd.android.cursor.dir/vnd.example.brown"; /** The MIME type of a single event */ // private static final String CONTENT_ITEM_TYPE2 = "vnd.android.cursor.item/vnd.example.brown"; private Bru_Press_Data news; private UriMatcher uriMatcher; private Cursor cursor; private sqliteDatabase db; String[] str; @Override public boolean onCreate() { uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(AUTHORITY,"user_deal",QUADUSER); uriMatcher.addURI(AUTHORITY,"user_deal/#",QUADUSER_ID); news = new Bru_Press_Data(getContext()); return true; } @Override public Cursor query(Uri uri,String[] projection,String selection,String[] selectionArgs,String orderBy) { sqliteDatabase db; Cursor cursor = null; if (uriMatcher.match(uri) == QUADUSER_ID) { long id = Long.parseLong(uri.getPathSegments().get(1)); selection = appendRowId(selection,id); } if (uriMatcher.match(uri) == QUADUSER) { // Get the database and run the query db = news.getReadableDatabase(); cursor = db.query(TABLE_NAME,projection,selection,selectionArgs,orderBy); } cursor.setNotificationUri(getContext().getContentResolver(),uri); return cursor; } @Override public String getType(Uri uri) { switch (uriMatcher.match(uri)) { case QUADUSER: return CONTENT_TYPE; case QUADUSER_ID: return CONTENT_ITEM_TYPE; default: throw new IllegalArgumentException("Unknown URI " + uri); } } @Override public Uri insert(Uri uri,ContentValues values) { sqliteDatabase db = news.getWritableDatabase(); Uri newUri = null; long id = -1; // Validate the requested uri if (uriMatcher.match(uri) == QUADUSER) { try { id = db.insertOrThrow(TABLE_NAME,values); // Notify any watchers of the change newUri = ContentUris.withAppendedId(CONTENT_URI,id); getContext().getContentResolver().notifyChange(newUri,null); } catch (NullPointerException e) { Log.v("Error","OnInsert"+e); } } else if (uriMatcher.match(uri) != QUADUSER) { throw new IllegalArgumentException("Unknown URI bru press " + uri); } return newUri; } @Override public int delete(Uri uri,String[] selectionArgs) { sqliteDatabase db = news.getWritableDatabase(); int count; long id; Log.v("Delete",selection); switch (uriMatcher.match(uri)) { case QUADUSER: count = db.delete(TABLE_NAME,selectionArgs); break; case QUADUSER_ID: id = Long.parseLong(uri.getPathSegments().get(1)); count = db.delete(TABLE_NAME,appendRowId(selection,id),selectionArgs); break; default: throw new IllegalArgumentException("Unknown URI " + uri); } getContext().getContentResolver().notifyChange(uri,null); return count; } @Override public int update(Uri uri,ContentValues values,String[] selectionArgs) { sqliteDatabase db = news.getWritableDatabase(); int count; long id; switch (uriMatcher.match(uri)) { case QUADUSER: count = db.update(TABLE_NAME,values,selectionArgs); break; case QUADUSER_ID: id = Long.parseLong(uri.getPathSegments().get(1)); count = db.update(TABLE_NAME,selectionArgs); break; default: throw new IllegalArgumentException("Unknown URI " + uri); } // Notify any watchers of the change getContext().getContentResolver().notifyChange(uri,null); return count; } /** Append an id test to a sql selection expression */ private String appendRowId(String selection,long id) { return _ID + "=" + id + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); } }
注意:您希望在Manifest文件中定义此Provider类
.
在另一个类中,我可以将数据插入并检索到DB …具体如下
class A extends Activity{ { addevent(cityname,cityid); // to insert } private void addevent(String a1,String a2) { try { ContentValues values = new ContentValues(); values.put(Constants.TITLE,a1); values.put(Constants.CITYID,a2); getContentResolver().insert(Constants.CONTENT_URI,values); } catch (Exception e) { Log.v("Error",""+e); } }
从Db中检索
Cursor c = getContentResolver().query(Constants.CONTENT_URI,null); int a = c.getCount(); Log.v("Count",""+a); int columnIndex = c.getColumnIndex(Constants.TITLE); int columnIndex2 = c.getColumnIndex(Constants.CITYID); if (c.moveToNext()) { cityName = c.getString(columnIndex); city_id= c.getString(columnIndex2); } }
试试这样的事……它会对你有所帮助