简体   繁体   English

C#-更新需要猜测AUTO_INCREMENT的ID

[英]C# - Updating an ID that requires guessing an AUTO_INCREMENT

Ok, so in my MySQL Database, I have a table called groups. 好的,所以在我的MySQL数据库中,我有一个名为groups的表。 In that table, there is one column called id and one column called group_id. 在该表中,有一列称为id,一列称为group_id。 Both are needed. 两者都是需要的。 Groups are inserted via a C# Emulator. 通过C#仿真器插入组。 I need to know, how can I edit my code to make it match id and group_id in the database? 我需要知道,如何编辑代码使其与数据库中的id和group_id匹配? id is an AUTO_INCREMENT in my groups table and is the primary key. id是我的组表中的AUTO_INCREMENT,并且是主键。

Here is my C# code, 这是我的C#代码,

queryreactor.setQuery(string.Concat(new object[]
                {
                    "INSERT INTO groups (`name`, `desc`,`badge`,`owner_id`,`created`,`room_id`,`colour1`,`colour2`) VALUES(@name, @desc, @badge, ",
                    Session.GetHabbo().Id,
                    ", UNIX_TIMESTAMP(), ",
                    RoomId,
                    ",'",
                    Colour1,
                    "','",
                    Colour2,
                    "')"
                }));
                queryreactor.addParameter("name", Name);
                queryreactor.addParameter("desc", Desc);
                queryreactor.addParameter("badge", Badge);

Anyone any ideas? 有任何想法吗? I know MySQL doesn't support 2 AUTO_INCREMENT columns. 我知道MySQL不支持2个AUTO_INCREMENT列。 But, I basically just need to match the id column and group_id column. 但是,我基本上只需要匹配id列和group_id列。

I don't get it why you need two columns just to have exactly same values, because you can always use ID column for both group_Id and ID. 我不明白为什么只需要两个列就具有完全相同的值,因为您总是可以将ID列同时用于group_Id和ID。

If you still want to have such columns, then a simple solution would be to fire an update query after your Insert query where your group_id gets updated with your newly inserted id column. 如果您仍然希望拥有此类列,那么一个简单的解决方案是在“插入”查询之后激发一个更新查询,其中您的group_id将使用新插入的id列进行更新。

something like 就像是

update groups
set group_id = id
where group_id is null

or even better, write a trigger on your groups table so that your group_id column gets updated automatically with that of id column on an insert statement of the row 甚至更好的是,在groups表上编写触发器 ,以便您的group_id列自动更新为该行的insert语句上的id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM