我已經創建了一個PHP
腳本,它將檢查連接的數據庫是否有一些預定義的表格,如果表格不存在,腳本將創建這些表格。檢查開關盒的執行是否成功
在這個腳本中,我用switch ... case
來創建這個表格,這個表格沒有任何錯誤。
現在我想要做的是檢查switch ... case
結束了沒有任何錯誤。如果沒有錯誤,那麼我想發射另外一組或者說sql
聲明,這些聲明會創建一些每天要運行的任務。
這是我的表檢查和創造PHP
部分,
<?php
//This is the array that contains all the table names
$tableNames = array(
'catdb', 'clientdb', 'qcerrors', 'searchterm', 'teams', 'tempdb', 'userlogin', 'userroles', 'usertimetrack'
);
//I'm using this for loop to loop through the array and create the tables
//if they doesn't exists.
for ($i = 0; $i < count($tableNames); $i++) {
$tableCheck = $dbConnect->query("SHOW TABLES LIKE '" . $tableNames[$i] . "'");
if ($tableCheck->rowCount() > 0) {
echo $tableNames[$i] . " .... Exists <br />";
} else {
$tblName = $tableNames[$i];
switch ($tblName) {
case "catdb":
try {
$tblCreate = "CREATE TABLE IF NOT EXISTS `catdb` (
`catId` int(100) NOT NULL AUTO_INCREMENT,
`uId` int(100) NOT NULL,
`Catagory` varchar(100) NOT NULL,
`createDate` varchar(100) NOT NULL,
PRIMARY KEY (`catId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1";
$dbConnect->exec($tblCreate);
echo $tblName . " .... Created OK <br />";
} catch (PDOException $e) {
echo $tblCreate . "<br>" . $e->getMessage();
}
break;
case "clientdb":
try {
$tblCreate = "CREATE TABLE IF NOT EXISTS `clientdb` (
`clientId` int(100) NOT NULL AUTO_INCREMENT,
`catId` varchar(100) NOT NULL,
`uId` int(100) NOT NULL,
`Client` varchar(100) NOT NULL,
`cDate` varchar(100) NOT NULL,
PRIMARY KEY (`clientId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1";
$dbConnect->exec($tblCreate);
echo $tblName . " .... Created OK <br />";
} catch (PDOException $e) {
echo $tblCreate . "<br>" . $e->getMessage();
}
break;
case "qcerrors":
try {
$tblCreate = "CREATE TABLE IF NOT EXISTS `qcerrors` (
`qcId` int(100) NOT NULL AUTO_INCREMENT,
`qcError` varchar(100) NOT NULL,
`createdBy` varchar(100) DEFAULT NULL,
`createdOn` varchar(100) DEFAULT NULL,
PRIMARY KEY (`qcId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1";
$dbConnect->exec($tblCreate);
echo $tblName . " .... Created OK <br />";
} catch (PDOException $e) {
echo $tblCreate . "<br>" . $e->getMessage();
}
break;
case "searchterm":
try {
$tblCreate = "CREATE TABLE IF NOT EXISTS `searchterm` (
`sId` int(100) NOT NULL AUTO_INCREMENT,
`sDate` varchar(100) NOT NULL,
`sUid` int(100) NOT NULL,
`searchedBy` int(100) NOT NULL,
PRIMARY KEY (`sId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1";
$dbConnect->exec($tblCreate);
echo $tblName . " .... Created OK <br />";
} catch (PDOException $e) {
echo $tblCreate . "<br>" . $e->getMessage();
}
break;
case "teams":
try {
$tblCreate = "CREATE TABLE IF NOT EXISTS `teams` (
`tId` int(100) NOT NULL AUTO_INCREMENT,
`TeamName` varchar(100) NOT NULL,
`tlName` varchar(100) DEFAULT NULL,
`tlSet` varchar(100) NOT NULL,
PRIMARY KEY (`tId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1";
$dbConnect->exec($tblCreate);
echo $tblName . " .... Created OK <br />";
} catch (PDOException $e) {
echo $tblCreate . "<br>" . $e->getMessage();
}
break;
case "tempdb":
try {
$tblCreate = "CREATE TABLE IF NOT EXISTS `tempdb` (
`tempId` int(100) NOT NULL AUTO_INCREMENT,
`uId` int(100) NOT NULL,
`tId` int(100) NOT NULL,
`catId` int(100) NOT NULL,
`clientId` int(100) NOT NULL,
`startTime` varchar(100) NOT NULL,
`Status` varchar(100) NOT NULL,
PRIMARY KEY (`tempId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1";
$dbConnect->exec($tblCreate);
echo $tblName . " .... Created OK <br />";
} catch (PDOException $e) {
echo $tblCreate . "<br>" . $e->getMessage();
}
break;
case "userlogin":
try {
$tblCreate = "CREATE TABLE IF NOT EXISTS `userlogin` (
`uId` int(100) NOT NULL AUTO_INCREMENT,
`uCreateDate` varchar(100) NOT NULL,
`createdBy` int(100) NOT NULL,
`fName` varchar(100) NOT NULL,
`lName` varchar(100) NOT NULL,
`uName` varchar(100) NOT NULL,
`pWord` varchar(100) NOT NULL,
`uTeam` int(100) NOT NULL,
`uRole` varchar(100) NOT NULL,
PRIMARY KEY (`uId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1";
$dbConnect->exec($tblCreate);
echo $tblName . " .... Created OK <br />";
} catch (PDOException $e) {
echo $tblCreate . "<br>" . $e->getMessage();
}
break;
case "userroles":
try {
$tblCreate = "CREATE TABLE IF NOT EXISTS `userroles` (
`urId` int(100) NOT NULL AUTO_INCREMENT,
`userRole` varchar(100) NOT NULL,
PRIMARY KEY (`urId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1";
$dbConnect->exec($tblCreate);
echo $tblName . " .... Created OK <br />";
} catch (PDOException $e) {
echo $tblCreate . "<br>" . $e->getMessage();
}
break;
case "usertimetrack":
try {
$tblCreate = "CREATE TABLE IF NOT EXISTS `usertimetrack` (
`utId` int(100) NOT NULL AUTO_INCREMENT,
`jDate` varchar(100) NOT NULL,
`usrId` int(100) NOT NULL,
`Category` varchar(100) NOT NULL,
`utClient` varchar(100) NOT NULL,
`jType` varchar(100) NOT NULL,
`startTime` varchar(100) NOT NULL,
`endTime` varchar(100) NOT NULL,
`timeSpent` varchar(100) NOT NULL,
`Volume` int(100) NOT NULL,
`qcErrorId` varchar(100) NOT NULL,
`noOfProductLines` int(100) DEFAULT NULL,
`Remarks` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`utId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1";
$dbConnect->exec($tblCreate);
echo $tblName . " .... Created OK <br />";
} catch (PDOException $e) {
echo $tblCreate . "<br>" . $e->getMessage();
}
break;
default:
echo $tblName . " this table is not part of this system.<br />";
}
}
}
?>
現在上面的代碼與運行出來後我想下面的語句SQL
將創建任務運行任何錯誤。
<?php
$timeNow = date("Y-m-d H:i:s"); //To get the current time
//Set the event scheduler status from OFF to ON
$dbConnect->exec("SET GLOBAL event_scheduler = ON");
//Creates the daily task 1 to truncate searchterm table
$createTask1 = "CREATE DEFINER= '" . $dbUser . "'@`%` EVENT `searcTerm Table Clean` ON SCHEDULE EVERY 1 DAY STARTS '" . $timeNow . "' ON COMPLETION NOT PRESERVE ENABLE DO TRUNCATE TABLE searchterm";
$dbConnect->exec($createTask1);
//Creates the daily task 1 to truncate tempdb table
$createTask2 = "CREATE DEFINER= '" . $dbUser . "'@`%` EVENT `EmptyData` ON SCHEDULE EVERY 1 DAY STARTS '" . $timeNow . "' ON COMPLETION NOT PRESERVE ENABLE DO TRUNCATE TABLE tempdb";
$dbConnect->exec($createTask2);
?>
我沒有搜索谷歌和SO,但沒有看到一個答案,這就是爲什麼我決定問這個問題。我希望我已經向社區明確了我的問題,如果有的話,我們可以澄清一下做什麼以檢查switch ... case
,並以任何錯誤結束。
MyISAM?真? – GordonM
那你能告訴我一個更好的嗎,請使用'MySQL'中的默認引擎。 – Sand
InnoDB是MySQL的默認設置,並且已有多年。你正在使用相對較新版本的MySQL,對吧?此外,拉丁語1可能會在未來困擾你,爲什麼不使用UFT8MB4? – GordonM