dbTalk Databases Forums  

auto_increment

mailing.database.mysql-plusplus mailing.database.mysql-plusplus


Discuss auto_increment in the mailing.database.mysql-plusplus forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Joachim Person
 
Posts: n/a

Default auto_increment - 03-14-2005 , 01:35 PM






------=_NextPart_000_000D_01C528D5.5B6E5670
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

How can I handle auto_increments? For instance, having



user_prescription subscription;

subscription.user_id = user_id;

subscription.service_id = service_id;

query.insert(subscription);

query.execute();





and subscription also has a "id" field which is auto_increment, doesn't
work. The id field gets some seemingly random value. How can I make sure
auto_increment works?


------=_NextPart_000_000D_01C528D5.5B6E5670--



Reply With Quote
  #2  
Old   
Chris Frey
 
Posts: n/a

Default Re: auto_increment - 03-14-2005 , 02:50 PM






On Mon, Mar 14, 2005 at 08:35:27PM +0100, Joachim Person wrote:
Quote:
and subscription also has a "id" field which is auto_increment, doesn't
work. The id field gets some seemingly random value. How can I make sure
auto_increment works?
auto_increment happens at the database level. I don't believe mysql++
automatically updates the struct with the new id, you need to get it yourself
with the insert_id() call.

query.store();
int id = connection.insert_id();// do this right after the
// insert operation

- Chris


--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #3  
Old   
Earl Miles
 
Posts: n/a

Default Re: auto_increment - 03-14-2005 , 02:58 PM



Chris Frey wrote:
Quote:
On Mon, Mar 14, 2005 at 08:35:27PM +0100, Joachim Person wrote:

and subscription also has a "id" field which is auto_increment, doesn't
work. The id field gets some seemingly random value. How can I make sure
auto_increment works?


auto_increment happens at the database level. I don't believe mysql++
automatically updates the struct with the new id, you need to get it yourself
with the insert_id() call.

query.store();
int id = connection.insert_id();// do this right after the
// insert operation
I think this may be problematic; if my understanding is correct, the id field
will be filled in by the user_prescription constructor, which is just
going to be an int, which means it'll be an essentially random value
since I doubt the constructor will initialize it to anything.

To get auto_increment to work, you have to either 1) not insert the
value into the table at all, or have the value inserted as NULL.

I *think* -- and this is where it gets quite hazy for me, because I
have not actually used either the specialized structures OR the Null
class (though I do need to use the Null class at some point) you
will need to do something like this:

subscription.id = Null<int>;

However, I'm not 100% sure that works with the specialized type classes
without going into the code and seeing if they respect the Null class.
I should imagine they do, but imagining will not make it so =)

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #4  
Old   
Warren Young
 
Posts: n/a

Default Re: auto_increment - 03-15-2005 , 12:58 AM



Joachim Person wrote:
Quote:
query.insert(subscription);
I don't try to use SSQLS inserts when I want to use auto_increment
columns. I manually build my own SQL statement; when you leave the
auto_increment column out of the insert, MySQL picks the value for that
column for you.

I'm not sure that there's an easy way for us to make MySQL++
intelligently handle this for you.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #5  
Old   
Chris Frey
 
Posts: n/a

Default Re: auto_increment - 03-15-2005 , 02:39 AM



On Mon, Mar 14, 2005 at 12:58:28PM -0800, Earl Miles wrote:
Quote:
I *think* -- and this is where it gets quite hazy for me, because I
have not actually used either the specialized structures OR the Null
class (though I do need to use the Null class at some point) you
will need to do something like this:

subscription.id = Null<int>;

However, I'm not 100% sure that works with the specialized type classes
without going into the code and seeing if they respect the Null class.
I should imagine they do, but imagining will not make it so =)
I tried doing this from the mysql command line, like:

INSERT INTO Category (CategoryID, Name) VALUES (NULL, "hello");

And it worked fine. (CategoryID being auto-increment in my table)

This seems to be vaguely documented on
http://dev.mysql.com/doc/mysql/en/in...functions.html
under LAST_INSERT_ID().

- Chris


--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #6  
Old   
Warren Young
 
Posts: n/a

Default Re: auto_increment - 03-15-2005 , 09:05 PM



Warren Young wrote:

Quote:
when you leave the
auto_increment column out of the insert, MySQL picks the value for that
column for you.
Sorry, I am in fact using NULL for the column value in this instance.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.