views:

131

answers:

5

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

A: 

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

EdmundG
I got the connection string from http://dev.mysql.com/doc/refman/5.0/en/federated-use.html and it works fine when I don't have the variables in there. If you could put the whole thing together for me that would be awesome
Justin Giboney
take a look at my edits
Justin Giboney
I'm not sure that's going to work. Wouldn't you have to wrap the entire command in a string and use an EXEC call?
Michael Todd
Of course, that comment is coming from someone in the SqlServer world; perhaps it's different in MySQL.
Michael Todd
Justin Giboney
It should work without an exec however, sometimes you need to declare the variables, execute that statement, before you can use the variables. In SQL Server you use GO, not sure what the MySQL equivalent is.
EdmundG
http://www.connectionstrings.com/mysql is for connections strings in ADO.NET and have nothing to do with the question.
nos
A: 

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

Evernoob
+1  A: 

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

longneck
A: 

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_filevarchar(255), PRIMARY KEY (id)) ENGINE=FEDERATED DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 CONNECTION='Uid='+@username'; Pwd='+@password+'; Server=**; Port=3306; Database =**';

Justin
A: 

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 stmt;
Bill Karwin