通过Java API将循环中的记录添加到OrientDB

时间:2014-02-13 10:30:52

标签: java database nosql orientdb

我有以下代码:

    // OPEN THE DATABASE
    ODatabaseDocumentTx db = new ODatabaseDocumentTx("remote:localhost/test").open("admin", "admin");

    // CREATE A NEW DOCUMENT AND FILL IT
    for(int i = 0; i < 10; i++){
    ODocument doc = new ODocument("Foo");
        doc.field( "number", i );
        doc.save();
    }
    db.close();

当调用“select * from Foo”时,输出为:

rid:17:0版本:0 class:Foo number:0

为什么只有一个记录而不是10个数字0到9?

我做错了什么?

1 个答案:

答案 0 :(得分:1)

检查

package com.excers;

import java.util.List;

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;

public class DBTESTER {

    public static void main(String[] args) {
        // OPEN THE DATABASE

        // ODatabaseDocumentTx db = new ODatabaseDocumentTx
        // ("local:D:/OrientDB/orientdb-community-1.7-rc1/orientdb-community-1.7-rc1/databases/petshop").create();
        ODatabaseDocumentTx db = new ODatabaseDocumentTx(
                "local:D:/OrientDB/orientdb-community-1.7-rc1/orientdb-community-1.7-rc1/databases/petshop")
                .open("admin", "admin");

        // CREATE A NEW DOCUMENT AND FILL IT
        for (int i = 0; i < 10; i++) {
            ODocument doc = new ODocument("foryou");
            doc.field("number", i);
            doc.save();
        }
        List<ODocument> result = db.query(new OSQLSynchQuery<ODocument>(
                "select * from foryou"));

        System.out.println("No of ODocuments:\t" + result.size());

        for (ODocument od : result) {
            System.out.println(od.field("number")+"\t");

        }

    }

}

输出

No of ODocuments:   10
0   1   2   3   4   5   6   7   8   9