Base de datos opa: ¿cómo saber si existe un valor en la base de datos?

I'd like to know if a record is present in a database, using a field different from the key field.

I've try the following code :

function start()
{
  jlog("start db query")
  myType d1 = {A:"rabbit", B:"poney"};
  /myDataBase/data[A == d1.A] = d1
  jlog("db write done")
   option opt = ?/myDataBase/data[B == "rabit"]
  jlog("db query done")

  match(opt)
  {
   case {none} : <>Nothing in db</>
   case {some:data} : <>{data} in database</>
  }
}

Server.start(
   {port:8092, netmask:0.0.0.0, encryption: {no_encryption}, name:"test"},
   [
     {page: start, title: "test" }
   ]
)

But the server hang up, and never get to the line jlog("db query done"). I mispell "rabit" willingly. What should I've done ?

Muchas Gracias

preguntado el 26 de agosto de 12 a las 18:08

1 Respuestas

Indeed it fails with your example, I have a "Match failure 8859742" exception, don't you see it?

pero no uses ?/myDataBase/data[B == "rabit"] (which should have been rejected at compile time - bug report sent) but /myDataBase/data[B == "rabit"] which is a DbSet. The reason is when you don't use a primary key, then you can have more than one value in return, ie a set of values.

You can convert a dbset to an iter with DbSet.iterator. Then manipulate it with Iter: http://doc.opalang.org/module/stdlib.core.iter/Iter

Respondido 26 ago 12, 21:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.