如何在magento中执行大型SQL查询?

时间:2013-04-10 12:57:55

标签: mysql magento

我想在db中存储大量内容,当我尝试执行此查询时,我的示例文本长度为16129个字符,它显示“错误:无法检索到请求的URL”,并且“收到了无数据”在铬。

此外,我使用LONGTEXT作为DB中文本的数据类型。

我还尝试直接在phpmyadmin中执行查询,它正常工作。

代码如下所示。

public function _getConnection($type = 'core_write') {
        return Mage::getSingleton('core/resource')->getConnection($type);
    }

public function testdbAction(){
        $db = $this->_getConnection();
        $current_time=now();
        $text="The European languages are members of the same family......  ...Europe uses the same vocabulary. The ";//text is 16129 characters in length
        $sql = "INSERT into test(`usercontent_id`,`app_id`,`module_id`,`customer_id`,`content`,`created_time`,`updated_time`,`item_id`,`index_id`,`position_id`) VALUES (NULL, 15, 9,2,'" .$text. "','" . $current_time . "','" . $current_time . "',1003,5,4)";
        $db->query($sql);        
    }

我该如何处理?任何建议或帮助..

2 个答案:

答案 0 :(得分:2)

尝试使用$db->exec($sql)代替$db->query($sql)

答案 1 :(得分:0)

对于使用数据库结构和数据进行操作,Magento有一个专门的地方。它被称为安装/升级脚本。它是为了跟踪模块版本以便于更新。因此,您应该使用脚本添加新数据。

这是一个例子。

模块的config.xml:


<modules>
    <My_Module>
        <version>0.0.1</version>
    </My_Module>
</modules>
<global>
    <resources>
        <my_module_setup>
            <setup>
                <module>My_Module</module>
                <class>Mage_Core_Model_Resource_Setup</class>
            </setup>
         </my_module_setup>
    </resources>
</global>

现在,您需要创建以下文件:

My_Module/sql/my_module_setup/install-0.0.1.php

现在,根据您的Magento版本,文件需要具有不同的名称。如果您使用的是Magento CE 1.4或更低版本(EE 1.8),那么您应该将其命名为mysql4-install-0.0.1.php

此脚本将在下一个网站请求时启动。类Mage_Core_Model_Resource_Setup将在install-0.0.1.php内执行代码。在安装脚本中,您可以使用$this object;

访问Setup类

现在,您可以使用以下代码编写脚本:


$this->startSetup();

$text = <<<TEXT
YOUR LONG TEXT GOES HERE
TEXT;

$this->getConnection()
    ->insert(
        $this->getTable('test'),
        array(       
             'usercontent_id' => null,
             'app_id' => 15,
             'content' => $text,
             //other fields in the same fashion
        )
    );

$this->endSetup();

就是这样。这是一种在Magento中向数据库添加自定义数据的简洁方法。

如果您想使用表单定期保存用户输入,那么我建议创建w模型,资源模型和集合,在config.xml中定义实体。有关更多信息,请参阅Alan Storm的文章,如this one

我希望我能正确理解你的问题。