How to insert a record using the Admin console Datastore Viewer using GQL

穿精又带淫゛_ 提交于 2019-12-10 14:38:29

问题


Is it possible to INSERT or UPDATE a entity in the datastore using the Admin > Datastore Viewer.

E.g. executing something like

INSERT INTO table VALUES (Foo='Bar')

回答1:


No you can't; GQL is a SQL-like language just for retrieving entities or keys.

You can INSERT, UPDATE or DELETE entities using the Datastore Viewer or from your application code.




回答2:


Not with GQL, but it is possible to INSERT and UPDATE entities with the Datastore Viewer.

To INSERT: After clicking on the Datastore Viewer, click the tab Create on top, select a Kind, press Next, fill the values and press Save Entity.

To UPDATE: In the Datastore Viewer, click on an ID/Name identifier, change the values and press Save Entity.




回答3:


insert with 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);
?>


来源:https://stackoverflow.com/questions/8475875/how-to-insert-a-record-using-the-admin-console-datastore-viewer-using-gql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!