我一直在搞乱我在过去几天写的一些新代码,但我一直遇到麻烦,我完全不知道我做错了什么。
我目前的代码如下:
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.GetUser().Username);
dbClient.addParameter("roomid", dbClient.getRow()[0].ToString());
dbClient.runQuery();
dbClient.setQuery("SELECT LAST_INSERT_ID();");
dbClient.runQuery();
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.GetUser().Id);
dbClient.addParameter("roomid", dbClient.getRow()[0].ToString());
dbClient.runQuery();
dbClient.setQuery("INSERT INTO `items` (`user_id`, `room_id`, `base_item`, `extra_data`, `x`, `y`, `z`, `rot`, `wall_pos`, `rareid`) VALUES " +
"(@id, @roomid, 71000250, '', 11, 1, 0, 0, '', 0);");
dbClient.addParameter("id", Session.GetUser().Id);
dbClient.addParameter("roomid", dbClient.getRow()[0].ToString());
dbClient.runQuery();
但是在运行此代码时,我不断收到MySQL错误,即我的参数@roomid未定义,即使我已经在第12行定义了它......
答案 0 :(得分:1)
我删除了部分代码,留下了:
dbClient.setQuery("SELECT LAST_INSERT_ID();");
dbClient.runQuery();
dbClient.setQuery("INSERT INTO....");
dbClient.addParameter("roomid", dbClient.getRow()[0].ToString());
dbClient.runQuery();
dbClient.setQuery("INSERT INTO ....");
dbClient.addParameter("roomid", dbClient.getRow()[0].ToString());
dbClient.runQuery();
首次使用dbClient.getRow()[0].ToString()
时,dbClient
仍然包含SELECT LAST_INSERT_ID();
查询的结果。
但是第二次执行新查询时,清除结果。因此,您可能希望将其存储在变量中,例如:
dbClient.setQuery("SELECT LAST_INSERT_ID();");
dbClient.runQuery();
string roomId=dbClient.getRow()[0].ToString();
dbClient.setQuery("INSERT INTO....");
dbClient.addParameter("roomid", roomId);
dbClient.runQuery();
dbClient.setQuery("INSERT INTO ....");
dbClient.addParameter("roomid", roomId);
dbClient.runQuery();