How to insert a record using the Admin console Dat

2019-06-23 19:36发布

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')

3条回答
我命由我不由天
2楼-- · 2019-06-23 19:58

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.

查看更多
做个烂人
3楼-- · 2019-06-23 20:09

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.

查看更多
走好不送
4楼-- · 2019-06-23 20:14

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);
?>
查看更多
登录 后发表回答