【问题标题】:Postgres: insertion and deletionPostgres:插入和删除
【发布时间】:2010-11-25 10:40:15
【问题描述】:

我觉得编写代码的方法更简单。如何缩短删除/插入时间?

// to delete the old tags before inserting new tags                                                                                                                                                    
        $result = pg_query_params ( $dbconn,
                'DELETE FROM tags
                WHERE question_id = $1',
                array ( $question_id )
                );

        $result = pg_prepare ( $dbconn, "query_777",
                'INSERT INTO tags
                (tag, question_id)
                VALUES ($1, $2)'
                );

【问题讨论】:

  • 您的第二个查询缺少参数。

标签: php sql postgresql stored-procedures


【解决方案1】:

你无能为力。从技术上讲,您可以使用存储过程来做到这一点,然后只需:

select change_question_tags( ... );

但变化不大。

【讨论】:

  • 使用起来太复杂? eioba.com/a70583/…
  • 没有。存储过程很简单。但它不会改变代码的复杂性——它只是改变了复杂性所在的地方。当然 - 这也有好的方面,但你想让它更短 - 使用存储过程会使你的 PHP 代码更短,但不是总长度 od 代码,包括数据库存储过程。
【解决方案2】:

您可以使用一些 ORM/DB 抽象,然后执行类似的操作

$tags = new TagsTable();
$tags->delete($tags->find('question_id = ?', $1));
$tags->insert(array('tag' => $1, 'question_id' => $2));

Zend_Db_Table

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 2016-10-19
    • 1970-01-01
    • 2020-05-08
    • 2019-09-07
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多