nlight-jdev / jcouchdb

Automatically exported from code.google.com/p/jcouchdb
Other
0 stars 0 forks source link

DataAccessException: error creating document #56

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
May be it is a simple question.But i really didn't know how to solve it.

My code is :
public class CreateDB {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Database db=new Database("localhost",5984,"mycouchdb");
        Map<String,String> doc =new HashMap<String,String>();
        doc.put("foo","value for the foo attribute");
        doc.put("bar", "value for the bar attribute");
        db.createDocument(doc);

    }

}

It return the error:
Exception in thread "main" org.jcouchdb.exception.DataAccessException: error 
creating document {"foo":"value for the foo attribute","bar":"value for the bar 
attribute"}in database 'mycouchdb': code 404
    at org.jcouchdb.db.Database.createOrUpdateDocument(Database.java:592)
    at org.jcouchdb.db.Database.createDocument(Database.java:348)
    at CreateDB.main(CreateDB.java:16)

please give me a clue.Thanks!

Original issue reported on code.google.com by bupt.z...@gmail.com on 22 Aug 2010 at 2:30

GoogleCodeExporter commented 8 years ago
the 404 comes from the fact that the Database you are trying to create a 
document in does not exist. If you do something like

{{{

        final String dbName = "mycouchdb";
        Database db = new Database("localhost", 5984, dbName);

        Server server = db.getServer();
        if (!server.listDatabases().contains(dbName))
        {
            System.out.println("Create Database " + dbName);
            server.createDatabase(dbName);
        }

        Map<String, String> doc = new HashMap<String, String>();
        doc.put("foo", "value for the foo attribute");
        doc.put("bar", "value for the bar attribute");
        db.createDocument(doc);

}}}

it works fine.

Original comment by ff...@gmx.de on 22 Aug 2010 at 8:43