插入大量的值在使用dbClient

本文关键字:dbClient 插入 | 更新日期: 2023-09-27 17:52:48

目前我正试图在我的新项目中插入很多值,但我不知道如何做到这一点。我的"最简单的"SQL查询如下,这个函数的工作原理如下:

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();     

但是现在我想插入多个值,使用以下查询:

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', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1'),
('private', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1'),
('private', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1'),
('private', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1'),
('private', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 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)使其工作....一个合理但混乱的解决方案可能是:

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.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');");

但是我不想在我的代码中重复'INSERT INTO'超过1000次。

插入大量的值在使用dbClient

选项:

  • 循环,多次使用不同参数的简单代码
  • 使用一个工具可以为您做到这一点(dapper有一些接近基本的技巧,大多数orm将允许简单的基于列表的插入)
  • 对于非常大的插入,"批量复制",如果你的提供商支持它-也许使用FastMember将列表转换为IDataReader

您只需要多个INSERT语句:

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', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1');
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', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1');