问题
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