【问题标题】:which is the correct table name for this content provider? Android该内容提供者的正确表名是哪个?安卓
【发布时间】:2013-05-08 00:19:13
【问题描述】:

我查看了许多内容提供者的示例。并且在创建数据库时表名不匹配。在下面的示例中,表在创建 SQLite 数据库时声明为“国家/地区”

  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);
  }

 }

【问题讨论】:

    标签: android sqlite android-contentprovider


    【解决方案1】:

    我认为这段代码没有问题

    内容提供者只是该数据库表的公共接口。条款不必完全匹配。

    将表名设为单数是一种标准做法。根据表名复数创建函数也是一种常见的做法。有时,这甚至是由代码生成器自动完成的。

    但是,对于来自 Content Providers 的 SQLite 和 Android uri,我们的工具并没有那么复杂。这就是为什么在 uriMatcher 的第二行中,使用的措辞对我们人类来说真的没有多大意义:

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

    【讨论】:

    • 那么,我可以为这个变量选择任何名称而不是“国家”吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多