brandonhamilton / rethinkdb-delphi

Delphi RethinkDB client driver
MIT License
13 stars 8 forks source link

Insert Example #1

Open Abdon3 opened 8 years ago

Abdon3 commented 8 years ago

Hi Brandon, would you be so kind as to share a few examples on how to insert in Delphi and any other of the command as well?

Thank You,

Abdon

brandonhamilton commented 8 years ago

Hi @Abdon3,

This driver closely matches the python driver implementation and all functions documented in the RethinkDB API can be used (with some minor syntax differences for the Delphi language).

Some examples of usage:

Creating a connection:

var conn: TRethinkDbConnection;
Begin
  conn := r.connect( 'localhost' );
End

Running queries:

var q: TRQLQuery; res: TRQLQueryResult;
Begin
  q := r.db('test').tableCreate('tv_shows');
  res := q.run( conn );
  If res is TRQLDocument
      Then Memo1.Lines.Add( (res as TRQLDocument).Value.ToString )
      Else
        Begin
          For V in (res as TRethinkDbCursor)
            Do Memo1.Lines.Add( V.ToString );
        End;
End;

Inserting data:

q := r.table('tv_shows').insert(r.expr([r.makeObject(['name', 'Star Trek TNG', 'episodes', 178 ]), r.makeObject(['name', 'Battlestar Galactica', 'episodes', 75 ]) ] ));

Querying data:

q := r.table('tv_shows').filter( r.row.getField('episodes').gt(100) );
EpoUnlimited commented 5 years ago

Hi Brandon,

For the insert, is it possible to insert a record with a subrecord like in this python's example:

r.table("authors").insert([ { "name": "William Adama", "tv_show": "Battlestar Galactica", "posts": [ {"title": "Decommissioning speech", "content": "The Cylon War is long over..."}, {"title": "We are at war", "content": "Moments ago, this ship received..."}, {"title": "The new Earth", "content": "The discoveries of the past few days..."} ] }, { "name": "Laura Roslin", "tv_show": "Battlestar Galactica", "posts": [ {"title": "The oath of office", "content": "I, Laura Roslin, ..."}, {"title": "They look like us", "content": "The Cylons have the ability..."} ] }, ...... ]);

And thank a lot for your work,

Eddy

ghost commented 5 years ago

By experimenting with RethinkDB I could see how promising Delphi is too. I'm still getting started, despite very few posts addressing its use in Pascal, how to enable authentication and etc ... But creating a Firebird sync for RethinkDB was very enjoyable. Congratulations to those involved! image

jirayu commented 1 year ago

How to list out a table as strings and collections of object or something I know how to do insert with variety of ways. I fail to insert using sample above. I tried so bad end up creating something like code below: function TRethink.Insert(TableName: string; const JSON: array of const): Boolean; var jsonObject: TJSONObject; begin try jsonObject := TJSONObject.Create; try for var i := 0 to High(JSON) div 2 do begin var key := string(JSON[i 2].VUnicodeString); var value := JSON[i 2 + 1]; case value.VType of vtUnicodeString: jsonObject.AddPair(key, TJSONString.Create(string(value.VUnicodeString))); vtString: jsonObject.AddPair(key, TJSONString.Create(string(value.VString))); vtInteger: jsonObject.AddPair(key, TJSONNumber.Create(value.VInteger)); vtBoolean: jsonObject.AddPair(key, TJSONBool.Create(value.VBoolean)); vtExtended: jsonObject.AddPair(key, TJSONNumber.Create(value.VExtended^)); vtCurrency: jsonObject.AddPair(key, TJSONNumber.Create(value.VCurrency^)); vtInt64: jsonObject.AddPair(key, TJSONNumber.Create(value.VInt64^)); end; end; r.db(Curdb).table(TableName).insert(jsonObject).run(Conn); Result := True; finally jsonObject.Free; end; except Result := False; end; end;