2013-05-08 23 views
1

我已經看過很多內容提供者的例子。並且在創建數據庫時表名不匹配。在表格下面的例子中創建SQLite數據庫被宣佈爲「國家」這是此內容供應商的正確表名? Android

public static final String SQLITE_TABLE = "Country"; 

然而,表名在URI

// create content URIs from the authority by appending path to database table 
    public static final Uri CONTENT_URI = 
    Uri.parse("content://" + AUTHORITY + "/countries"); 

表的名稱描述爲「國家」是在URI中的權限之後,所以在這個例子中,表名被認爲是「國家」,這與創建數據庫期間所聲明的不同,其中

其中哪些是正確的表名,Countrycountries

public class MyContentProvider extends ContentProvider{ 

    private MyDatabaseHelper dbHelper; 

private static final int ALL_COUNTRIES = 1; 
private static final int SINGLE_COUNTRY = 2; 

// authority is the symbolic name of your provider 
// To avoid conflicts with other providers, you should use 
// Internet domain ownership (in reverse) as the basis of your provider authority. 
private static final String AUTHORITY = "com.as400samplecode.contentprovider"; 

// create content URIs from the authority by appending path to database table 
public static final Uri CONTENT_URI = 
Uri.parse("content://" + AUTHORITY + "/countries"); 

// a content URI pattern matches content URIs using wildcard characters: 
// *: Matches a string of any valid characters of any length. 
// #: Matches a string of numeric characters of any length. 
private static final UriMatcher uriMatcher; 
static { 
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 
uriMatcher.addURI(AUTHORITY, "countries", ALL_COUNTRIES); 
uriMatcher.addURI(AUTHORITY, "countries/#", SINGLE_COUNTRY); 
} 


public class CountriesDb { 

public static final String KEY_ROWID = "_id"; 
public static final String KEY_CODE = "code"; 
public static final String KEY_NAME = "name"; 
public static final String KEY_CONTINENT = "continent"; 

private static final String LOG_TAG = "CountriesDb"; 
public static final String SQLITE_TABLE = "Country"; 

    private static final String DATABASE_CREATE = 
    "CREATE TABLE if not exists " + SQLITE_TABLE + " (" + 
    KEY_ROWID + " integer PRIMARY KEY autoincrement," + 
    KEY_CODE + "," + 
    KEY_NAME + "," + 
    KEY_CONTINENT + "," + 
" UNIQUE (" + KEY_CODE +"));"; 

    public static void onCreate(SQLiteDatabase db) { 
    Log.w(LOG_TAG, DATABASE_CREATE); 
    db.execSQL(DATABASE_CREATE); 
    } 

    public static void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    Log.w(LOG_TAG, "Upgrading database from version " + oldVersion + " to " 
    + newVersion + ", which will destroy all old data"); 
    db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE); 
    onCreate(db); 
    } 

} 

回答

0

我不明白的問題,此代碼

內容提供商只是一個公共接口到數據庫表。條款不必完美匹配。

這是一個標準做法,使您的表名單數。根據表名複數製作功能也是一種常見做法。有時候,這甚至會由代碼生成器自動完成。

對於來自Content Providers的SQLite和Android uris,我們的工具並不複雜。這就是爲什麼,在uriMatcher的第二行,真正使用的措辭沒有太大的意義對我們人類:

uriMatcher.addURI(AUTHORITY, "countries/#", SINGLE_COUNTRY); 
+0

所以,我可以爲這個變量,而不是「國家」選擇任意的名字嗎? – Kevik 2013-05-08 03:49:13