Variables in the creation record

I am trying to use some variables in a script to create database settings. I'm not quite sure how to use them. Please explain how to format my code correctly. Below is the code I'm trying to execute and the error I am getting:

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

Mistake

#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 `

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

does not work, still says that it is not formatted correctly

+3
source share
5 answers

MySQL does not support variable interpolation directly into such syntax.

, , , 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 stmt;
+5

MySQL 5+, , ?

0

. mysql DDL .

0

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

Source: https://habr.com/ru/post/1715105/


All Articles