Received: by 10.150.51.6 with SMTP id y6mr186765yby.24.1222114067116; Mon, 22
Sep 2008 13:07:47 -0700 (PDT)
Date: Mon, 22 Sep 2008 13:07:47 -0700 (PDT)
X-IP: 148.106.2.6
User-Agent: G2/1.0
X-Google-Token: d6T62gwAAAC5RQSrv4E0yFsP5ThxjZJv
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET
CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR
3.0.04506.648),gzip(gfe),gzip(gfe)
Message-ID:
Subject: upsert example by C interface
From: dbtouch
To: “Kdb+ Personal Developers”
X-Google-Approved: niall.dalton@gmail.com via email at 2008-09-22 20:08:41
Hi, KDB users
Can you show me an example to do upsert using C interface? I tried the
following example and the second upsert turned out to be insert.
1. create table as:
all_dt:(seq:int$();cchar:char$();cshort:short$();cint:int$
();cfloat:float$();csym:symbol$())
2. compile and run this program:
#include “k.h”
// dml.cpp : simple insert
//
#include “k.h”
#include “stdio.h”
int main(int argc, char* argv)
{
K err;
I kdbSocketHandle = khp(“localhost”, 5001);
if (kdbSocketHandle > 0)
{
// K row = knk(3, ks((S)“ibm”), kf (93.5), ki(300));
// k(-kdbSocketHandle, “.u.upd”, ks((S)“trade”), row, (K)0);
K row=knk(6, ki(1), kc(‘b’), kh(200), ki(2000), kf(99.9),
ks((S)“hello world”));
err=k(-kdbSocketHandle, “upsert”, ks((S)“all_dt”), row, (K)0);
K row2=knk(6, ki(1), kc(‘c’), kh(200), ki(2000), kf(99.9),
ks((S)“hello world”));
err=k(-kdbSocketHandle, “upsert”, ks((S)“all_dt”), row2, (K)0);
// K row=knk(3, ks((S)“q”), ki(10), ki(50));
// err=k(-kdbSocketHandle, “upsert”, ks((S)“a”), row, (K)0);
if (err->t==-128)
{
printf(“err: %s\n”, err->s);
}
k(kdbSocketHandle,“”,(K)0); // flush
}
}
3. show all_dt in q session: I still see 2 rows:
q)show all_dt
seq cchar cshort cint cfloat csym
----------------------------------------
1 b 200 2000 99.9 hello world
1 c 200 2000 99.9 hello world
X-Mailer: Apple Mail (2.929.2)
upsert is only different for keyed tables
should be
all_dt:([seq:int$()] cchar:char$();cshort:short$ ();cint:int$();cfloat:float$();csym:symbol$())
Regards,
Attila
Thanks, Attila. I will try it out. I thought I used keyed table in mysyntax.dbtouchOn Sep 22, 4:10?pm, Attila Vrabecz <attila.vrab…> wrote:> upsert is only different for keyed tables>> should be>> all_dt:([? ?seq:int$() ?] ? ? cchar:char$();cshort:short$> ();cint:int$();cfloat:float$();csym:symbol$())>> Regards,> ? ? Attila>> On 22 Sep 2008, at 21:07, dbtouch wrote:>>>>>> > Hi, KDB users>> > Can you show me an example to do upsert using C interface? I tried the> > following example and the second upsert turned out to be insert.>> > 1. create table as:> > all_dt:(seq:int$();cchar:char$();cshort:short$();cint:int$> > ();cfloat:float$();csym:symbol$())>> > 2. compile and run this program:> > #include “k.h”> > // dml.cpp : simple insert> > //> > #include “k.h”> > #include “stdio.h”> > int main(int argc, char* argv)> > {> > ? ?K err;> > ? ?I kdbSocketHandle = khp(“localhost”, 5001);> > ? ?if (kdbSocketHandle > 0)> > ? ?{> > ? ? ? ? ? ?// K row = knk(3, ks((S)“ibm”), kf (93.5), ki(300));> > ? ? ? ? ? ?// k(-kdbSocketHandle, “.u.upd”, ks((S)“trade”), row, (K)0);> > ? ? ? ? ? ?K row=knk(6, ki(1), kc(‘b’), kh(200), ki(2000), kf(99.9),> > ks((S)“hello world”));> > ? ? ? ? ? ?err=k(-kdbSocketHandle, “upsert”, ks((S)“all_dt”), row, (K)0);> > ? ? ? ? ? ?K row2=knk(6, ki(1), kc(‘c’), kh(200), ki(2000), kf(99.9),> > ks((S)“hello world”));> > ? ? ? ? ? ?err=k(-kdbSocketHandle, “upsert”, ks((S)“all_dt”), row2, (K)0);> > ? ? ? ? ? ?// K row=knk(3, ks((S)“q”), ki(10), ki(50));> > ? ? ? ? ? ?// err=k(-kdbSocketHandle, “upsert”, ks((S)“a”), row, (K)0);> > ? ? ? ? ? ?if (err->t==-128)> > ? ? ? ? ? ?{> > ? ? ? ? ? ? ? ? ? ?printf(“err: %s\n”, err->s);> > ? ? ? ? ? ?}> > ? ? ? ? ? ?k(kdbSocketHandle,“”,(K)0); // flush> > ? ?}> > }>> > 3. show all_dt in q session: I still see 2 rows:>> > q)show all_dt> > seq cchar cshort cint cfloat csym> > ----------------------------------------> > 1 ? b ? ? 200 ? ?2000 99.9 ? hello world> > 1 ? c ? ? 200 ? ?2000 99.9 ? hello world- Hide quoted text ->> - Show quoted text -</attila.vrab…>