mysql - 您的 SQL 语法有错误;在 Joomla 中创建触发器

标签 mysql joomla triggers

我正在使用 mysql,我想在 Joomla 3.4.8 中创建一个触发器。

我有一个可以在 mySQLWorkbench 中运行的 SQL 代码: enter image description here

如果我在 Joomla Controller 中运行它:

$db = JFactory::getDbo();
$sql = $db->getQuery(true);
$sql = 'LOCK TABLES id0ap_virtuemart_products WRITE; 
        DROP TRIGGER IF EXISTS update_vm_products ;
        DELIMITER //
        CREATE TRIGGER update_vm_products 
        AFTER UPDATE
        ON id0ap_virtuemart_products FOR EACH ROW
        BEGIN
        INSERT INTO id0ap_virtuemart_notifications
        VALUES ("", NEW.virtuemart_product_id, "product","update");
        END //
        DELIMITER ;
        UNLOCK TABLES;';
$db->setQuery($sql);
$db->execute();

我得到:

1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DROP TRIGGER IF EXISTS update_vm_products ; DELIMITER // ' at line 2 SQL=LOCK TABLES id0ap_virtuemart_products WRITE; DROP TRIGGER IF EXISTS update_vm_products ; DELIMITER // CREATE TRIGGER update_vm_products AFTER UPDATE ON id0ap_virtuemart_products FOR EACH ROW BEGIN INSERT INTO id0ap_virtuemart_notifications VALUES ("", NEW.virtuemart_product_id, "product","update"); END // DELIMITER ; UNLOCK TABLES;

我找不到问题所在。但当我试图解决这个问题时,我发现了一些奇怪的事情。如果我修改sql删除DELIMITERS和DROP部分:

$sql = 'CREATE TRIGGER update_vm_products 
        AFTER UPDATE
        ON id0ap_virtuemart_products FOR EACH ROW
        BEGIN
        INSERT INTO id0ap_virtuemart_notifications
        VALUES ("", NEW.virtuemart_product_id, "product","update");
        END ;';

无论您只是尝试创建触发器,它都会起作用并创建触发器,只有在添加 DROP 部分时才会失败。任何想法?有没有面向对象的方法来做到这一点?

提前致谢!

最佳答案

我不是joomla专家,但似乎joomla使用任何mysql api来执行sql语句,每次调用只能执行1条sql语句。尝试单独执行每个sql语句。

关于mysql - 您的 SQL 语法有错误;在 Joomla 中创建触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36984177/

相关文章:

mysql - 无法添加或更新子行: a foreign key constraint fails - liferay

joomla - 移动了一个 joomla 站点,错误 : load error: failed to find error. less 和 master.less

css - 围绕居中的品牌形象包装 Bootstrap 导航栏列表项

mysql - 如何在Mysql中使用触发器

c# - 在文本匹配 MVVM 上更改 TextBox BorderBrush 的颜色

java - MySQL JDBC Web 程序 : error in DB config

php - 查看最新版本的删除历史记录

mysql - 基于角色的访问控制 MySql 实现

php - 需要评估 Joomla 是否适合 Web 应用程序需求

c++ - 尽管每帧调用一次,但如何仅执行一次方法代码