는 아래의 코드를 따라 매우 간단합니다 ... 이 3 종은 예를 들어 이름 만들기 - 1. 내가 사용 상수 인터페이스, ...
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,
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 + ')'
: "");
}
}
주 전체 코드를 참조하고
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 데이터 클래스를 사용의 발견 : 당신이 정의하는 가정 매니페스트 파일의 공급자 클래스는 입니다. 내가 삽입하고 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);
}
}
이
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);
}
}
DB
에서 검색과 같은 몇 가지 일을 시도하기 위해 다음과 당신을 도울 것입니다
[안드로이드에서 데이터베이스 생성 및 사용하기] (http://www.devx.com/wireless/Article/40842/0), 그 좋은 것 –
안녕하세요, 자습서 읽기 sqlite에서 데이터 액세스하기 http : //www.hig herpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/ – Narasimha
+1, 17 만 회 이상의 조회가 아직 투표가 없습니다! – Jaguar