【问题标题】:How to insert a record using the Admin console Datastore Viewer using GQL如何使用 GQL 使用管理控制台数据存储查看器插入记录
【发布时间】:2012-01-18 12:56:10
【问题描述】:

是否可以使用管理 > 数据存储查看器插入或更新数据存储中的实体。

例如执行类似

INSERT INTO table VALUES (Foo='Bar')

【问题讨论】:

    标签: google-app-engine gql


    【解决方案1】:

    不使用 GQL,但可以使用 Datastore Viewer 插入和更新实体。

    INSERT:点击Datastore Viewer后,点击顶部的标签Create,选择Kind,按Next ,填写值并按保存实体

    更新:在数据存储查看器中,单击ID/名称标识符,更改值并按保存实体.

    【讨论】:

    • 当我插入时,如上所述,我遇到了问题。问题是“ID/Name”始终是 id=nnn 而不是 name=nnnn。有没有办法用 name=nnnn 添加一行?我尝试在“命名空间:”框中输入一个数字,但没有成功。
    【解决方案2】:

    不,你不能; GQL 是一种类似 SQL 的语言,仅用于检索实体或键。

    您可以使用数据存储查看器或从您的应用程序代码插入、更新或删除实体。

    【讨论】:

    【解决方案3】:

    用 Google_Client.php 插入:

    <?php
    //https://developers.google.com/apis-explorer/#p/datastore/v1beta2/
    const APP_NAME='a-test-com';
    const SERVICE_ACCOUNT_NAME='511908282087@developer.gserviceaccount.com';
    $_PRIVATE_KEY=file_get_contents('data/34672-privatekey.p12');
    require_once 'google-api-php-client/Google_Client.php';
    
    $client=new Google_Client();
    $credentials=new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME,
                                                 array('https://www.googleapis.com/auth/userinfo.email',
                                                       'https://www.googleapis.com/auth/datastore'
                                                      ),
                                                 $_PRIVATE_KEY
                                                );
    $client->setAssertionCredentials($credentials);
    
    $postBody=json_encode(array('mode'=>'NON_TRANSACTIONAL','mutation'=>array('upsert'=>array(
              array('key'=>array('partitionId'=>array('datasetId'=>'s~'.APP_NAME),
                                 'path'=>array(array('kind'=>'Guestbook',
                                                     'name'=>'my_guestbook'
                                                    )
                                              )
                                ),
                    'properties'=>array('guest_name'=>array('stringValue'=>'my name1 my name1'))
                   )
              ))));
    $httpRequest=new Google_HttpRequest('datastore/v1beta2/datasets/'.APP_NAME.'/commit', 'POST', null, $postBody);
    $head=array('content-type'=>'application/json; charset=UTF-8',
                'content-length'=>Google_Utils::getStrLen($postBody)
               );
    $httpRequest->setRequestHeaders($head);
    $httpRequest=Google_Client::$auth->sign($httpRequest);
    $result=Google_REST::execute($httpRequest);
    var_export($result);
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-22
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      相关资源
      最近更新 更多