一、mysql創建數據表的語句
//其中id,question_NO等均爲需要創建的數據庫表的語句;
create table if not exists `表名`(
id int primary key auto_increment not null,
question_NO int not null,
one_class varchar(255),
two_class varchar(255),
question_types varchar(255),
select_question_types varchar(255),
question_stem longtext,
options longtext,
score varchar(20),
answer varchar(20),
analysis longtext,
stem_remark varchar(255),
answer_remark varchar(255)
) ENGINE = InnoDB DEFAULT CHARSET = utf8;
二、與框架thinkphp配合使用
public function createDatabase($dataBase){
try{
$sql="create table if not exists `$dataBase`
(
id int primary key auto_increment not null,
question_NO int not null,
one_class varchar(255),
two_class varchar(255),
question_types varchar(255),
select_question_types varchar(255),
question_stem longtext,
options longtext,
score varchar(20),
answer varchar(20),
analysis longtext,
stem_remark varchar(255),
answer_remark varchar(255)
) ENGINE = InnoDB DEFAULT CHARSET = utf8;";
$res=Db::execute($sql);
trace("創建數據表成功,表名爲:$dataBase",'info');
return json_encode(['errcode'=>0,'errmsg'=>"創建成功"]);
}catch(Exception $e){
trace("創建數據表失敗,原因是:".$e->getMessage(),"error");
return json_encode(['errcode'=>10001,'errmsg'=>"創建錯誤"]);
}
}
注意:在與框架配合使用的時候應該注意的是:Db::execute($sql)
執行的結果返回的總是爲0,也就是創建失敗,但這個時候數據表已經創建成功了,所以我們通過try{}catch(Exception $e){}
來進行處理結果。