大佬教程收集整理的这篇文章主要介绍了android中的sqlite示例程序,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
提前致谢
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类@H_404_2@.
在另一个类中,我可以将数据插入并检索到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); } }
试试这样的事……它会对你有所帮助
以上是大佬教程为你收集整理的android中的sqlite示例程序全部内容,希望文章能够帮你解决android中的sqlite示例程序所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。