Create 语句中的变量

发布于 2024-08-02 00:33:49 字数 1212 浏览 8 评论 0原文

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

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=****;' ;

不起作用,仍然说格式不正确

I am trying to use some variables in a creation script for database setups. I am not exactly sure how to use them. Please explain how to properly format my code. Below is the code I try, and the error I get:

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' ;

error

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

I also tried it without the `

Trying EdmundG's solution

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=****;' ;

didn't work, still says not formatted correctly

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

若无相欠,怎会相见 2024-08-09 00:33:49

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

但是,您可以使用这些变量构建一个字符串,然后将其准备并执行为 动态 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;

MySQL doesn't support variable interpolation directly into syntax like that.

You could, however, build a string with those variables and then prepare and execute it as 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;
无畏 2024-08-09 00:33:49

好的,第三次尝试。

我可以看到两个可能的问题。

  1. 你有正确的报价,而不是
    SQL 中的通用单引号。

  2. 您需要定义变量
    并在运行SQL之前执行
    使用这些变量。

请参阅 http://www.connectionstrings.com/mysql

OK third try.

There are two possible problems I can see.

  1. You have right quotes instead of
    generic single quotes in your SQL.

  2. You need to define the variables
    and execute before running the SQL
    that uses those variables.

See http://www.connectionstrings.com/mysql

路还长,别太狂 2024-08-09 00:33:49

如果您使用 MySQL 5+,您可以尝试将其放入存储函数中,然后运行该函数吗?

If you are using MySQL 5+ could you try placing it in a stored function, and then running the function?

空气里的味道 2024-08-09 00:33:49

你不能这样做。 mysql 不解析变量的 DDL 语句。

you can't do this. mysql doesn't parse DDL statements for variable.

回忆那么伤 2024-08-09 00:33:49

试试这个

SET @username = 'xxxx';-- 商店号码放在这里

SET @password = 'xxxxxx';-- 商店密码放在这里

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 =****';

try this

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 =****';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文