在android中的sqlite示例程序
我是Newbee数据库的概念,特别是我需要相关的数据库的概念..我想要一个工作的例子,使用的SQLite数据库的android ..我走过了android开发人员的网站,但发现很less的东西关于数据库的例子..因为我是新的数据库概念任何人都可以build议我一些很好的资源,了解创build(),插入(),查询()…等..它会更好,如果我得到任何示例程序使用android ..
提前致谢
这是非常简单的下面的代码…创build3类命名为例 – 1.我使用常量接口,引用完整的代码,并find它的用途
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.提供者类
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, null, null, 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, null, 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 selection, 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, selection, 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 selection, String[] selectionArgs) { SQLiteDatabase db = news.getWritableDatabase(); int count; long id; switch (uriMatcher.match(uri)) { case QUADUSER: count = db.update(TABLE_NAME, values, selection, selectionArgs); break; case QUADUSER_ID: id = Long.parseLong(uri.getPathSegments().get(1)); count = db.update(TABLE_NAME, values, appendRowId(selection, id), 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类 。
在另一个类,我可以插入和检索数据到数据库…有以下
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, null, null,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); } }
尝试一些这样的事情…它会帮助你