• <i id='c7bmD'><tr id='c7bmD'><dt id='c7bmD'><q id='c7bmD'><span id='c7bmD'><b id='c7bmD'><form id='c7bmD'><ins id='c7bmD'></ins><ul id='c7bmD'></ul><sub id='c7bmD'></sub></form><legend id='c7bmD'></legend><bdo id='c7bmD'><pre id='c7bmD'><center id='c7bmD'></center></pre></bdo></b><th id='c7bmD'></th></span></q></dt></tr></i><div id='c7bmD'><tfoot id='c7bmD'></tfoot><dl id='c7bmD'><fieldset id='c7bmD'></fieldset></dl></div>
    <legend id='c7bmD'><style id='c7bmD'><dir id='c7bmD'><q id='c7bmD'></q></dir></style></legend>
    <tfoot id='c7bmD'></tfoot>

      <bdo id='c7bmD'></bdo><ul id='c7bmD'></ul>

      <small id='c7bmD'></small><noframes id='c7bmD'>

        Drupal 数据库 API 查询 - row.update 如果存在,否则为 row.insert

        时间:2024-04-15

            <tbody id='j4Bum'></tbody>

          <small id='j4Bum'></small><noframes id='j4Bum'>

              <tfoot id='j4Bum'></tfoot>

                <legend id='j4Bum'><style id='j4Bum'><dir id='j4Bum'><q id='j4Bum'></q></dir></style></legend>
                  <bdo id='j4Bum'></bdo><ul id='j4Bum'></ul>
                  <i id='j4Bum'><tr id='j4Bum'><dt id='j4Bum'><q id='j4Bum'><span id='j4Bum'><b id='j4Bum'><form id='j4Bum'><ins id='j4Bum'></ins><ul id='j4Bum'></ul><sub id='j4Bum'></sub></form><legend id='j4Bum'></legend><bdo id='j4Bum'><pre id='j4Bum'><center id='j4Bum'></center></pre></bdo></b><th id='j4Bum'></th></span></q></dt></tr></i><div id='j4Bum'><tfoot id='j4Bum'></tfoot><dl id='j4Bum'><fieldset id='j4Bum'></fieldset></dl></div>
                  本文介绍了Drupal 数据库 API 查询 - row.update 如果存在,否则为 row.insert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在尝试在 drupal 中运行一个查询,如果条目已经存在,它将更新条目,或者如果不存在则插入一个新条目.代码现在看起来像这样:

                  I've been trying to run a query in drupal that'll update entries if they already exists or insert a new entry if it doesnt. The code looks like this at the moment:

                  db_query("IF EXISTS (SELECT %d FROM {uc_posten_packages.pid})
                  UPDATE {uc_posten_packages} SET title = '%s', label = '%s', cost = '%d', length = '%d', width ='%d', height = '%d', weight = '%d'  WHERE pid = %d
                  ELSE
                  INSERT INTO {uc_posten_packages} VALUES ('%d', '%s', '%s', '%d', '%d', '%d', '%d', '%d')",$id, $title, $label, $rate, $length, $width, $height, $weight, $id, $id, $title, $label, $rate, $length, $width, $height, $weight);
                  

                  我不明白为什么该查询会引发错误.错误中的所有数字都是正确的

                  I can't see why that query throws me an error. All the numbers in the error are correct

                  ...near 'IF EXISTS (SELECT 1 FROM uc_posten_packages.pid) UPDATE uc_posten_packages ' at line 1 query:
                  IF EXISTS (SELECT 1 FROM uc_posten_packages.pid) UPDATE uc_posten_packages SET title = 'vfbv', label = 'bbv', cost = '22', length = '232', width ='22', height = '22', weight = '22' WHERE pid = 1 ELSE INSERT INTO uc_posten_packages VALUES ('1', 'vfbv', 'bbv', '22', '232', '22', '22', '22')
                  

                  这个查询是否应该有效和/或在 drupal 中是否有更好的方法来处理这个问题?

                  Should this query work and/or is there some better way dealing with this in drupal?

                  推荐答案

                  您正在寻找的东西被亲切地称为upsert"(更新否则插入),并且有一个专门用于此的 drupal 函数:db_merge.这里写得很好:http://drupal.org/node/310085

                  What you're looking for is cordially called an "upsert" (update otherwise insert), and there is a drupal function for just this: db_merge. Good write-up here: http://drupal.org/node/310085

                  drupal_write_record() 不会自动执行更新插入".它始终仅根据您传入的内容插入或更新,但不能同时插入或更新.

                  drupal_write_record() does not automatically perform an "upsert." It always either inserts or updates based on what you pass in only, but not both.

                  这篇关于Drupal 数据库 API 查询 - row.update 如果存在,否则为 row.insert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:收集数据,然后将数据导入 Drupal 的最佳方法? 下一篇:字段名称相同时的 SQL 连接

                  相关文章

                  • <bdo id='0RNna'></bdo><ul id='0RNna'></ul>

                    <i id='0RNna'><tr id='0RNna'><dt id='0RNna'><q id='0RNna'><span id='0RNna'><b id='0RNna'><form id='0RNna'><ins id='0RNna'></ins><ul id='0RNna'></ul><sub id='0RNna'></sub></form><legend id='0RNna'></legend><bdo id='0RNna'><pre id='0RNna'><center id='0RNna'></center></pre></bdo></b><th id='0RNna'></th></span></q></dt></tr></i><div id='0RNna'><tfoot id='0RNna'></tfoot><dl id='0RNna'><fieldset id='0RNna'></fieldset></dl></div>

                      <small id='0RNna'></small><noframes id='0RNna'>

                    1. <legend id='0RNna'><style id='0RNna'><dir id='0RNna'><q id='0RNna'></q></dir></style></legend>
                      <tfoot id='0RNna'></tfoot>