5

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')
Ryan
  • 23,871
  • 24
  • 86
  • 132

3 Answers3

7

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.

stivlo
  • 83,644
  • 31
  • 142
  • 199
  • When I INSERT, as above, I hit a problem. The problem is "ID/Name" is always id=nnn and never name=nnnn. Is there a way to add a row with name=nnnn ? I have tried putting a number into the box "Namespace:" but that did not work. – eddyparkinson Nov 13 '13 at 01:13
4

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.

systempuntoout
  • 71,966
  • 47
  • 171
  • 241
  • Ahh of course. Mislead myslef by this GQL cheat sheet... that has nothing to do with Googles GQL! http://www.slideshare.net/sones/gql-cheat-sheet-latest – Ryan Dec 12 '11 at 14:48
0

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);
?>
diyism
  • 12,477
  • 5
  • 46
  • 46