【问题标题】:Insert several rows from 1st table to the 2nd with ContentProvider使用 ContentProvider 从第一个表插入几行到第二个表
【发布时间】:2013-08-12 00:48:45
【问题描述】:

我想知道如何使用 ContentProvider 来实现这一点? 我使用单个选择语句将几行从表插入到另一个:

insert into table1 (field1, field2,...) 
     select field1, field2... from table2 where ...

如果我应该使用插入方法,我无法获得这几个新的 id..

编辑:如果这是 insert 方法,我应该返回什么 Uri

【问题讨论】:

    标签: android android-contentprovider bulkinsert insertion


    【解决方案1】:

    致电execSql

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
      switch (sUriMatcher.match(uri)){
        case URI_1:
         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
         db.execSql("insert into table1 (field1, field2,...) 
         select field1, field2... from table2 where ...");
         return uri;
    }
    

    【讨论】:

    • 这个你的例子必须返回新插入行的 uri "public Uri insert..." 供 ContentProvider 观察数据库变化..
    • 您可以返回传递给插入的相同 URI。更新了我的答案。
    • 返回相同的URI没有问题。
    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多