简体   繁体   English

SQL 脚本不会在 Joomla 中的组件安装中插入值

[英]SQL-script does not insert values in component installation in Joomla

I'm working on creating a Joomla component for Joomla 2.5, but I'm having trouble with the installation script.我正在为 Joomla 2.5 创建 Joomla 组件,但我在安装脚本时遇到了问题。 In the install.mysql.utf8.sql file I have the following content:install.mysql.utf8.sql文件中,我有以下内容:

DROP TABLE IF EXISTS `#__products`;

CREATE TABLE `#__products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL,
  `description` varchar(128) NOT NULL,
  `price` int(11) NOT NULL,
  `published` tinyint(1) NOT NULL DEFAULT 1,
  `url` varchar(255),
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

# Insert default values
INSERT INTO `#__products` (`name`, `description`, `price`, `url`) VALUES
('product1', 'description1', 1000, 'image.png'),
('product2', 'description2', 2000, 'image.png'),
('product3', 'description3', 4000, 'image.png');

When I install the component, Joomla says it was successfull.当我安装组件时,Joomla 说它成功了。 However, when I check the database the default values have not been added to the product table even though the table got created.但是,当我检查数据库时,即使创建了表,默认值也没有添加到产品表中。 If I run the INSERT directly in mysql it works just fine (have to change the name of the table of course).如果我直接在 mysql 中运行INSERT它工作得很好(当然必须更改表的名称)。

If i were to include a typo in the SQL-script I get an error, so I don't see how it's a problem with my MySQL syntax.如果我在 SQL 脚本中包含一个错字,我会收到一个错误,所以我看不出我的 MySQL 语法有什么问题。 Does anyone have any clues of what might be going on here, and how to solve it?有没有人对这里可能发生的事情有任何线索,以及如何解决它?

Thanks.谢谢。

I finally got it to work.我终于让它工作了。 The only thing I did was to remove the DROP TABLE IF EXISTS at the top of the SQL-file, and change the CREATE -statement to start with a CREATE TABLE IF NOT EXISTS .我所做的唯一一件事就是删除 SQL 文件顶部的DROP TABLE IF EXISTS ,并将CREATE语句更改为以CREATE TABLE IF NOT EXISTS开头。 That was it.就是这样。

I'm not sure why this works, but it could be that the insert does not work because the table hasn't been properly deleted/created when the insert is being executed.我不确定为什么会这样,但可能是插入不起作用,因为在执行插入时没有正确删除/创建表。 Hope this will help someone else!希望这会帮助别人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM