我正在使用dbDelta的以下安装函数作为参考,因此我注意到了一些典型的问题,例如主键上的一个空格而不是两个空格。。。
function myplugin_install(){
global $wpdb;
$table_name = $wpdb->prefix . "vehicles";
$sql = "CREATE TABLE IF NOT EXISTS $table_name
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
description VARCHAR(25) NOT NULL,
PRIMARY KEY (id)
);
";
$sql .= "CREATE TABLE IF NOT EXISTS $table_name1
(
...
)";
$sql .= "CREATE TABLE IF NOT EXISTS $table_nameX
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
art_alarmierung VARCHAR(25) NOT NULL ,
PRIMARY KEY (id)
);";
require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
dbDelta($sql);
echo $wpdb->last_error;
}
dbDelata仅存储最后一个
$sql
变量和echo
$wpdb->last_error;
不显示任何错误?我的失败在哪里?
BR;麦贝克
最合适的回答,由SO网友:Stephen Harris 整理而成
跑dbDelta
对于每个SQL语句,分别执行以下操作:
function myplugin_install(){
global $wpdb;
$table_name = $wpdb->prefix . "vehicles";
require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
$sql = "CREATE TABLE IF NOT EXISTS $table_name
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
description VARCHAR(25) NOT NULL,
PRIMARY KEY (id)
);
";
dbDelta($sql);
$sql2 = "CREATE TABLE IF NOT EXISTS $table_name1
(
...
)";
dbDelta($sql2);
$sql3 = "CREATE TABLE IF NOT EXISTS $table_nameX
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
art_alarmierung VARCHAR(25) NOT NULL ,
PRIMARY KEY (id)
);";
dbDelta($sql3);
}