我目前正在为android应用程序构建一个数据库并编写存储过程。我是一个很长时间的程序员,几年前创建了一些数据库,但直到现在才真正使用存储过程。目前的问题是,在我继续将新的翻译字符串插入表translatedstrings之前,我没有找到正确的方法来检查给定的字符串(由stringid指定)和给定的语言(由languageid指定)是否不存在,以及已翻译的字符串是否已经存在。我查了许多类似的问题和其他网站,虽然有些似乎有完全相同的问题,他们的解决办法确实为他们工作,但不是为我。
下面是我当前的创建过程脚本:
create procedure `InsertTranslatedString`(in stringId int(10) unsigned, in translationSource tinyint(3) unsigned, in translationLanguageId int(10) unsigned,
in translatedString mediumtext, out insertedTranslatedStringId int(10) unsigned, out returnValue int(10))
reads sql data
modifies sql data
begin
-- if the string id does not exists
if (select count(1) from `Strings` where `stringId` = stringId) = 0
then
-- return error code
set returnValue = -1;
set insertedTranslatedStringId = 0;
-- if the language id does not exists
elseif (select count(1) from `Languages` where `languageId` = translationLanguageId) = 0
then
-- return error code
set returnValue = -2;
set insertedTranslatedStringId = 0;
-- if the translated string already exists
elseif (select count(1) from `TranslatedStrings` where `stringId` = stringId and `languageId` = translationLanguageId) > 0
then
-- return error code
set returnValue = -3;
set insertedTranslatedStringId = 0;
-- if we are ready to go
else
-- insert the actual translated string
insert into TranslatedStrings (`stringId`, `languageId`, `value`, `translationSource`, `createdDateTime`)
values (stringId, translationLanguageId, translatedString, translationSource, now());
select @newTranslatedStringId := last_insert_id();
-- give back output parameters
set insertedTranslatedStringId = @newTranslatedStringId;
set returnValue = 0;
end if;
end $$
程序的创建就像魅力。
我把这个过程称为 call InsertTranslatedString(76, 0, 1, 'German (Germany)', @insertedTranslatedStringId, @returnValue);
stringid 76和languageid 1都不存在。
当我打这个电话时,我注意到 if-elseif-elseif-else
方块跳过第一个 if
(stringid检查)即使它应该输入 then
阻止。然而,程序进入第一个阶段 elseif
(语言ID检查)效果很好。
我也尝试过这样的检查: if not exists (select 1 from 'Strings' where 'stringId' = stringId)
但是有一个 exists()
函数似乎总是返回true,不管 select
返回一行或无行;然后程序将直接跳到第二个 elseif
就因为我用了 elseif exists (select...
在那里。实际的 select
但是,如果单独运行它,则不会返回任何行。非常混乱。
以下是三个参考表:
CREATE TABLE `Languages` (
`languageId` int(10) unsigned NOT NULL,
`nameStringId` int(10) unsigned NOT NULL,
`languageCode` varchar(5) DEFAULT NULL,
`hiddenInSettings` tinyint(1) DEFAULT '0',
PRIMARY KEY (`languageId`),
KEY `FK_Languages_nameStringId` (`nameStringId`),
CONSTRAINT `FK_Languages_nameStringId` FOREIGN KEY (`nameStringId`) REFERENCES `Strings` (`stringId`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='contains all supported and automatically translated languages'
CREATE TABLE `Strings` (
`stringId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`originalValue` mediumtext NOT NULL,
`originalLanguageId` int(10) unsigned NOT NULL,
PRIMARY KEY (`stringId`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='contains all language-independent strings instances'
CREATE TABLE `TranslatedStrings` (
`stringId` int(10) unsigned NOT NULL,
`languageId` int(10) unsigned NOT NULL,
`value` mediumtext NOT NULL,
`translationSource` tinyint(3) unsigned DEFAULT '0',
`createdDateTime` datetime DEFAULT NULL,
`lastModifiedDateTime` datetime DEFAULT NULL,
`incorrectTranslationState` tinyint(3) unsigned DEFAULT '0',
`incorrectTranslationReporterId` int(10) unsigned DEFAULT NULL,
`incorrectTranslationReportedDateTime` datetime DEFAULT NULL,
PRIMARY KEY (`stringId`,`languageId`),
KEY `FK_TranslatedStrings_languageId` (`languageId`),
KEY `FK_TranslatedStrings_incorrectTranslationReporterId` (`incorrectTranslationReporterId`),
CONSTRAINT `FK_TranslatedStrings_incorrectTranslationReporterId` FOREIGN KEY (`incorrectTranslationReporterId`) REFERENCES `Users` (`userId`) ON DELETE CASCADE,
CONSTRAINT `FK_TranslatedStrings_languageId` FOREIGN KEY (`languageId`) REFERENCES `Languages` (`languageId`) ON DELETE CASCADE,
CONSTRAINT `FK_TranslatedStrings_stringId` FOREIGN KEY (`stringId`) REFERENCES `Strings` (`stringId`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='contains all localized and automatically translated strings'
当我创建表时,我没有创建这些表 KEY
顺便说一下,它们是由mysql服务器创建的。奇怪的是它们只和 CONSTRAINT
不是全部。
因为字符串只有一个列主键,没有外键,所以它对应的if语句没有被触发,我感到困惑。我做错了什么?
暂无答案!
目前还没有任何答案,快来回答吧!