因为我是数据库概念的新手任何人都可以建议我一些很好的资源来理解create(),insert(),query()……等等.如果我得到任何使用androID的示例程序会更好.
提前致谢
解决方法 它很简单,请按照下面的代码…创建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); } }
试试这样的事……它会对你有所帮助
总结以上是内存溢出为你收集整理的android中的sqlite示例程序全部内容,希望文章能够帮你解决android中的sqlite示例程序所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)