gpt4 book ai didi

sql - Create 语句中的变量

转载 作者:可可西里 更新时间:2023-11-01 08:01:41 26 4
gpt4 key购买 nike

我正在尝试在数据库设置的创建脚本中使用一些变量。我不确定如何使用它们。请解释如何正确格式化我的代码。下面是我尝试的代码,以及我得到的错误:

SET @username = 'xxxx'; -- store number goes here
SET @password = 'xxxxxx'; -- store password goes here

CREATE TABLE IF NOT EXISTS `my_table` (
`id` int(11) auto_increment,
`release_date` datetime,
`front_image_file` varchar(255),
PRIMARY KEY (`id`)
) ENGINE=FEDERATED
DEFAULT CHARSET=latin1
AUTO_INCREMENT=1
CONNECTION='mysql://`@username`:`@password`@host_name:3306/database_name/table_tame' ;

错误

#1432 - Can't create federated table. The data source connection string 'mysql://`@username`:`@password`@host_name:3306/databa' is not in the correct format

我也试过没有 `

尝试 EdmundG 的解决方案

SET @username = 'xxxx'; -- store number goes here
SET @password = 'xxxxxx'; -- store password goes here

CREATE TABLE IF NOT EXISTS `my_table` (
`id` int(11) auto_increment,
`release_date` datetime,
`front_image_file` varchar(255),
PRIMARY KEY (`id`)
) ENGINE=FEDERATED
DEFAULT CHARSET=latin1
AUTO_INCREMENT=1
CONNECTION='Uid=@username; Pwd=@password; Server=****; Port=3306; Database =****; Table=****;' ;

没用,还是说格式不正确

最佳答案

MySQL 不支持直接将变量插值到这样的语法中。

但是,您可以使用这些变量构建一个字符串,然后准备并执行它作为 dynamic SQL .

SET @username = 'xxxx'; -- store number goes here
SET @password = 'xxxxxx'; -- store password goes here

SET @sql = CONCAT('CREATE TABLE IF NOT EXISTS `my_table` (',
' `id` int(11) auto_increment,',
' `release_date` datetime,',
' `front_image_file` varchar(255),',
' PRIMARY KEY (`id`)',
') ENGINE=FEDERATED',
' DEFAULT CHARSET=latin1',
' AUTO_INCREMENT=1',
' CONNECTION=''mysql://', @username, ':', @password,
'@host_name:3306/database_name/table_tame');

PREPARE stmt FROM @sql;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

关于sql - Create 语句中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1273701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com