Create 语句中的变量
我正在尝试在创建脚本中使用一些变量来进行数据库设置。 我不太确定如何使用它们。 请解释如何正确格式化我的代码。 下面是我尝试的代码,以及我得到的错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
MySQL 不支持直接将变量插值到这样的语法中。
但是,您可以使用这些变量构建一个字符串,然后将其准备并执行为 动态 SQL。
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.
好的,第三次尝试。
我可以看到两个可能的问题。
你有正确的报价,而不是
SQL 中的通用单引号。
您需要定义变量
并在运行SQL之前执行
使用这些变量。
请参阅 http://www.connectionstrings.com/mysql
OK third try.
There are two possible problems I can see.
You have right quotes instead of
generic single quotes in your SQL.
You need to define the variables
and execute before running the SQL
that uses those variables.
See http://www.connectionstrings.com/mysql
如果您使用 MySQL 5+,您可以尝试将其放入存储函数中,然后运行该函数吗?
If you are using MySQL 5+ could you try placing it in a stored function, and then running the function?
你不能这样做。 mysql 不解析变量的 DDL 语句。
you can't do this. mysql doesn't parse DDL statements for variable.
试试这个
SET @username = 'xxxx';-- 商店号码放在这里
SET @password = 'xxxxxx';-- 商店密码放在这里
try this
SET @username = 'xxxx';-- store number goes here
SET @password = 'xxxxxx';-- store password goes here