【问题标题】:How to programmatically put data to the Google Appengine database from remote executable?如何以编程方式将数据从远程可执行文件放入 Google Appengine 数据库?
【发布时间】:2011-04-14 16:20:19
【问题描述】:

我想预先填写并定期将数据放入 Google Appengine 数据库。

我想用 java 和 python 编写一个程序,连接到我的 GAE 服务并将数据上传到我的数据库。

我该怎么做?

谢谢

【问题讨论】:

    标签: java python google-app-engine


    【解决方案1】:

    请使用 RemoteAPI 以编程方式执行此操作。

    在python中,您可以先配置appengine_console.py,如here所述

    一旦你有了它,你就可以在 python shell 中启动并编写以下命令:

    $ python appengine_console.py yourapp

    >>> import yourdbmodelclassnamehere
    >>> m = yourmodelclassnamehere(x='',y='')
    >>> m.put()
    

    这里是java版本的代码,不言自明(直接借用remote api page on gae docs):

    package remoteapiexample;
    
    import com.google.appengine.api.datastore.DatastoreService;
    import com.google.appengine.api.datastore.DatastoreServiceFactory;
    import com.google.appengine.api.datastore.Entity;
    import com.google.appengine.tools.remoteapi.RemoteApiInstaller;
    import com.google.appengine.tools.remoteapi.RemoteApiOptions;
    import java.io.IOException;
    
    public class RemoteApiExample {
        public static void main(String[] args) throws IOException {
            String username = System.console().readLine("username: ");
            String password = 
                new String(System.console().readPassword("password: "));
            RemoteApiOptions options = new RemoteApiOptions()
                .server("<your app>.appspot.com", 443)
                .credentials(username, password);
            RemoteApiInstaller installer = new RemoteApiInstaller();
            installer.install(options);
            try {
                DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
                System.out.println("Key of new entity is " + 
                    ds.put(new Entity("Hello Remote API!")));
            } finally {
                installer.uninstall();
            }
        }
    }
    

    【讨论】:

    • 应该给他Java version
    • 请在我接受之前为 API 命名并将 java 版本添加到您的答案中。这是java版本:code.google.com/appengine/docs/java/tools/remoteapi.html
    • 也许我不了解 StackOverflow 的“文化”,但问题是在“java or python”而不是“java and”中询问代码> 蟒蛇”。说在他们添加 Java 版本之前你不会接受答案对我来说似乎不公平。
    • @djondal:编辑问题很愚蠢;你为什么想要在两者中编写相同的程序?
    • 我还没有决定使用什么语言来填充数据库。我只是在玩工具。
    猜你喜欢
    • 2011-03-25
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多