dbDelta not creating tables

时间:2012-12-26 作者:mehulved

我浏览了很多线程、codex页面并尝试处理很多事情,但我的代码似乎没有创建表。我也不知道我错在哪里。我检查了数据库中的booking\\u db\\u版本,当我在文件中更新它时,它会得到更新。

这是密码

global $booking_db_version;
$booking_db_version = "1.0.0";

function booking_install() {
    global $wpdb;
    global $booking_db_version;
    global $tableprefix;
    $installed_version = get_option(\'booking_db_option\');

    $tableprefix = $wpdb->prefix . \'booking_\';

    require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');

    if ( $installed_version !== $booking_db_version ) {
        /* Create table for packages */
        $packagetable = $tableprefix . \'packages\';
        $sql = "create table  $packagetable (
            id mediumint(9) NOT NULL AUTO_INCREMENT, 
            name text NOT NULL, 
            description text NOT NULL, 
            city1 text NOT NULL, 
            city2 text NOT NULL,
            PRIMARY KEY  (id)
        );";
        dbDelta($sql);

        /* Create table for hotels */
        $hoteltable = $tableprefix . \'hotels\';
        $sql = "create table $hoteltable (
            id mediumint(9) NOT NULL AUTO_INCREMENT, 
            name text NOT NULL, 
            city text NOT NULL, 
            price decimal(10,2) NOT NULL,
            PRIMARY KEY  (id)
        );";
        dbDelta($sql);

        /* Create table for addons */
        $addontable = $tableprefix . \'addons\';
        $sql = "create table $addontable (
            id mediumint(9) NOT NULL AUTO_INCREMENT, 
            name text NOT NULL, 
            addongroup text NOT NULL, 
            price decimal(10,2) NOT NULL,
            PRIMARY KEY  (id)
        );";
        dbDelta($sql);

        /* Create table for addon groups */
        $addongrouptable = $tableprefix . \'addon_groups\';
        $sql = "create table $addongrouptable (
            id mediumint(9) NOT NULL AUTO_INCREMENT, 
            name text NOT NULL, 
            perhead text NOT NULL,
            PRIMARY KEY  (id)
        );";
        dbDelta($sql);

        update_option(\'booking_db_version\', $booking_db_version);
    }
}
register_activation_hook(__FILE__, \'booking_install\');

5 个回复
最合适的回答,由SO网友:Pontus Abrahamsson 整理而成

来自WordPress codex关于dbDelta:

dbDelta函数检查当前的表结构,将其与所需的表结构进行比较,并根据需要添加或修改表,因此可以非常方便地进行更新(有关如何使用dbDelta的更多示例,请参阅wp admin/upgrade-schema.php)。然而,请注意,dbDelta函数相当挑剔。例如:

您必须在SQL语句中将每个字段放在自己的行上必须使用关键字key,而不是其同义词索引,并且必须至少包含一个关键字有了这些注意事项,下面是函数中的下一行,它们将实际创建或更新表。您需要在$sql变量中替换您自己的表结构。

我更改了您的sql:"create table $packagetable (

对此:"CREATE TABLE " . $packagetable . " (

这是您的代码的工作副本:

global $booking_db_version;
$booking_db_version = "1.0.0";

function booking_install() {
    global $wpdb;
    global $booking_db_version;
    global $tableprefix;
    $installed_version = get_option(\'booking_db_option\');

    $tableprefix = $wpdb->prefix . \'booking_\';

    require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');

    if ( $installed_version !== $booking_db_version ) {
        // Create table for packages 
        $packagetable = $tableprefix . \'packages\';
        $sql = "CREATE TABLE " . $packagetable . " (
            id INT NOT NULL AUTO_INCREMENT, 
            name TEXT NOT NULL, 
            description TEXT NOT NULL, 
            city1 TEXT NOT NULL, 
            city2 TEXT NOT NULL,
            PRIMARY KEY  (id)
        ) ". $charset_collate .";";
        dbDelta($sql);

        // Create table for hotels 
        $hoteltable = $tableprefix . \'hotels\';
        $sql = "CREATE TABLE " . $hoteltable . " (
            id mediumint(9) NOT NULL AUTO_INCREMENT, 
            name text NOT NULL, 
            city text NOT NULL, 
            price decimal(10,2) NOT NULL,
            PRIMARY KEY  (id)
        ) ". $charset_collate .";";
        dbDelta($sql);

        // Create table for addons 
        $addontable = $tableprefix . \'addons\';
        $sql = "CREATE TABLE " . $addontable . " (
            id mediumint(9) NOT NULL AUTO_INCREMENT, 
            name text NOT NULL, 
            addongroup text NOT NULL, 
            price decimal(10,2) NOT NULL,
            PRIMARY KEY  (id)
        ) ". $charset_collate .";";
        dbDelta($sql);

        // Create table for addon groups 
        $addongrouptable = $tableprefix . \'addon_groups\';
        $sql = "CREATE TABLE " . $addongrouptable . " (
            id mediumint(9) NOT NULL AUTO_INCREMENT, 
            name text NOT NULL, 
            perhead text NOT NULL,
            PRIMARY KEY  (id)
        ) ". $charset_collate .";";
        dbDelta($sql);

        update_option(\'booking_db_version\', $booking_db_version);
    }
}
register_activation_hook(__FILE__, \'booking_install\');

SO网友:mirza

你可以试试这个function:

$table_name = "ratings";

$table_columns = "id INT(6) UNSIGNED AUTO_INCREMENT,
                    rate tinyint(1) NOT NULL,
                    ticket_id bigint(20) NOT NULL,
                    response_id bigint(20) NOT NULL,
                    created_at TIMESTAMP";

$table_keys = "PRIMARY KEY (id),
                    KEY ratings_rate (rate),
                    UNIQUE KEY ratings_response_id (response_id)";

create_table($table_name, $table_columns, $table_keys);

SO网友:Kallol

使用“CREATE TABLE”而不是“CREATE TABLE”为我解决了这个问题。

SO网友:Adi Prasetyo

除了所有这些要点之外,还应该触发激活挂钩。

虽然您开发了插件并编写了正确的代码,但仍然需要reactivate your plugin 触发挂钩,以便在插件激活时创建表。

SO网友:shirin niki

SQL关键字(如CREATE TABLE和UPDATE)必须为大写。因此,将create table的行更改为:

"CREATE TABLE " . $packagetable . "( 
以及

id mediumint(9) NOT NULL AUTO_INCREMENT,
收件人:

id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
或者这个:

name text NOT NULL, 
收件人:

name TEXT NOT NULL, 
等等

结束

相关推荐

插件卸载:为什么在$wpdb->查询($DROP_SQL)之后运行DBDelta

我正在读专业的wordpress。他们卸载插件的代码是//build our query to delete our custom table $sql = \"DROP TABLE \" . $table_name . \";\"; //execute the query deleting the table $wpdb->query($sql); require_once(ABSPATH .’wp-admin/includes/upgrade.php’)