0

我在我的新项目中运行这个查询:

dbClient.setQuery("INSERT INTO `rooms` (`roomtype`, `caption`, `owner`, `description`, `category`, `state`, `users_now`, `users_max`, `model_name`, `public_ccts`, `score`, `tags`, `icon_bg`, `icon_fg`, `icon_items`, `password`, `wallpaper`, `floor`, `landscape`, `allow_pets`, `allow_pets_eat`, `allow_walkthrough`, `allow_hidewall`, `wallthick`, `floorthick`, `achievement`, `group_id`, `game_id`, `mute_settings`, `ban_settings`, `kick_settings`) VALUES " +
                                    "('private', 'VIP CADEAU: Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 0, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1');");
                                    dbClient.addParameter("username", Session.GetHabbo().Username);
                                    dbClient.runQuery();

但是现在,我想NULL从此查询中获取新生成的 'id' (= ) 并为其分配一个参数,以便在以下查询中将其用作@newid

dbClient.setQuery("INSERT INTO `items` (`user_id`, `room_id`, `base_item`, `extra_data`, `x`, `y`, `z`, `rot`, `wall_pos`, `rareid`) VALUES " +
"(@id, @newid, 99036, '0', 6, 6, 0.0002, 0, '', 0);");
                                    dbClient.addParameter("id", Session.GetHabbo().Id);
                                    dbClient.runQuery();

如何将第一个查询中生成的值分配给@newid?这个解决方案在我看来是合乎逻辑的,但它不起作用:

dbClient.addParameter("roomid", dbClient.setQuery("LAST_INSERT_ID();");

错误: http: //puu.sh/7GmCI/f5b6794d02.png

谢谢您的帮助!

@Tewr

dbClient.setQuery("INSERT INTO `rooms` (`id`, `roomtype`, `caption`, `owner`, `description`, `category`, `state`, `users_now`, `users_max`, `model_name`, `public_ccts`, `score`, `tags`, `icon_bg`, `icon_fg`, `icon_items`, `password`, `wallpaper`, `floor`, `landscape`, `allow_pets`, `allow_pets_eat`, `allow_walkthrough`, `allow_hidewall`, `wallthick`, `floorthick`, `achievement`, `group_id`, `game_id`, `mute_settings`, `ban_settings`, `kick_settings`) VALUES " +
                                    "(NULL, 'private', 'VIP CADEAU: Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 0, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1');");
                                    dbClient.addParameter("username", Session.GetHabbo().Username);
                                    dbClient.setQuery("SELECT LAST_INSERT_ID();");
                        dbClient.runQuery();
dbClient.addParameter("roomid", dbClient.getRow()[0].ToString());

之后,下面的代码使用我们之前定义的@roomid。

  dbClient.setQuery("INSERT INTO `items` (`user_id`, `room_id`, `base_item`, `extra_data`, `x`, `y`, `z`, `rot`, `wall_pos`, `rareid`) VALUES " +
"(@id, @roomid, 99036, '0', 6, 6, 0.0002, 0, '', 0);");
                                    dbClient.addParameter("id", Session.GetHabbo().Id);
                                    dbClient.runQuery();

但这会导致错误提示 @roomid 未定义。

4

1 回答 1

1

您的编译错误和现有代码提供了一些线索:

此行无法编译,因为您在末尾缺少括号,并且 setQuery() 不返回值:

dbClient.addParameter("roomid", dbClient.setQuery("LAST_INSERT_ID();");

因此,将其替换为:

dbClient.setQuery("SELECT LAST_INSERT_ID();")
dbClient.runQuery();
dbClient.addParameter("roomid", dbClient.getRow()[0].ToString());

既然您的代码中的这一行正在编译,它不能保证它会工作......看起来您的代码应该已经工作在首位。确保您的表使用AUTO_INCREMENT正确配置。

于 2014-03-23T15:58:51.623 回答