List:MySQL++« Previous MessageNext Message »
From:Chris Morgan Date:March 18 2010 2:20am
Subject:Re: insert query without primary key
View as plain text  
MySQL treats a zero as a special value when it is in an insert statement for an
auto-increment column (unless you've changed the default MySQL config). It will replace
zero with the new auto-increment value.  You'll still have to use insert_id() to
reconcile the SSQLS after the insert.

Chris

-- Sent from my Palm Pre
On Mar 17, 2010 19:15, Andrej van der Zee <mavdzee@stripped> wrote: 

If I set host_id to zero, it will simply insert a 0 for host_id. It's just another value,
nothing special about 0. 

--- On Thu, 18/3/10, Chris Morgan <christopher.j.morgan@stripped> wrote:

From: Chris Morgan <christopher.j.morgan@stripped>
Subject: Re: insert query without primary key
To: "Andrej van der Zee" <mavdzee@stripped>, "plusplus@stripped"
<plusplus@stripped>
Date: Thursday, 18 March, 2010, 3:03

I think it will work if you set the _host_id to zero before the insert.

Chris

-- Sent from my Palm Pre
On Mar 17, 2010 18:52, Andrej van der Zee <mavdzee@stripped> wrote: 

Hi,

In my project I use the sql_create_compete_XXX macro for creating C++ database table
abstractions. For example table host:

sql_create_complete_3(host, 1, 3,
  mysqlpp::sql_int_unsigned, _host_id, "host_id",
  mysqlpp::sql_smallint_unsigned, _session_id, "session_id",
  mysqlpp::sql_smallint_unsigned, _sort_order, "sort_order");

In a .cpp file I have:

const char* host::table_ = "host";
const char* host::names[] = { 
  "host_id",
  "session_id",
  "sort_order"
};

Now host_id is the primary key with auto increment. When I insert a new host in the table,
I want to primary key to be auto-inserted. I now I can do this by defining another
sql_create_complete_2
 without the host_id, but I rather create this insert-query from the same class. The
reason is that I want to derive from the host class like this:

class sub_host : public host
{
  sub_host(unsigned session_id, unsigned sort_order) :
     host(session_id, sort_order)
  { }

  void db_insert()
  {
    // ... Get mysqlpp::query ...

    query.insert(*this);
    query.execute();
    _host_id = query.insert_id();
  }
};

But the insert(*this) query adds the host_id to the SQL-query for obvious reasons. Is
there another way to quickly generate an insert SQL-query without its primary
auto-increment column?

Thank you,
Andrej





      





-- 

MySQL++ Mailing List

For list archives: http://lists.mysql.com/plusplus

To unsubscribe:    http://lists.mysql.com/plusplus?unsub=1








      

Thread
insert query without primary keyAndrej van der Zee18 Mar
  • Re: insert query without primary keyChris Morgan18 Mar
    • Re: insert query without primary keyAndrej van der Zee18 Mar
      • Re: insert query without primary keyChris Morgan18 Mar
        • Re: insert query without primary keyAndrej van der Zee18 Mar
        • Re: insert query without primary keyWarren Young18 Mar