1

I am using MySQL with MATLAB, and I want to get a name from user, and pass it to the table in mySQL, but it is rejecting a variable name in place of string

var_name=input('enter the name:');

mysql('insert into table (name) values (var_name)');

Any suggestions?

3
  • 1
    I don't have mysql but i think this would works for you: mysql( horzcat('insert into table ', name, ' values ', var_name)); Commented Jul 20, 2011 at 21:26
  • 1
    Hi Ibtesam, Welcome to StackOverflow! Both @Isaac and Amro have posted correct answers, so you have three to choose from now ;-). The reasoning behind all this is that MATLAB has no way of telling that you meant var_name to be a variable and not just some characters you wanted to pass to the mysql function... Commented Jul 20, 2011 at 21:33
  • @Jonas: Thanks for telling the behind story. I was too lazy to explain that. =] Commented Jul 20, 2011 at 23:52

3 Answers 3

3

FIRST read the comments to this question - you don't want to shoot yourself in the foot with a mysql injection security problem. You have been warned. Now, to solve your current problem, without addressing the security risk of the whole approach when it comes to building SQL queries, read on...

In principle Amro has already posted two solutions for you which work, but since you have not accepted it I'll explain further.

Your problem is that you are not telling MATLAB which parts of your query it should interpret as a literal string, and which parts it should interpret as a variable name. To solve this, you can just end the literal string where appropriate, i.e. after the opening brackets, and then start them again before the closing brackets.

In between those literal strings you want to add the contents of your variables, so you need to tell MATLAB to concat your literal strings with your variables, since the mysql command probably expects the whole query as a single string. So in essence you want to take the string 'insert into table(' and the string saved in the variable name and the string ') values (' and so on and glue them into one big string. Amro and Isaac have shown you two solutions of how to do this without much explanation:

horzcat('insert into table (', name, ') values (', var_name, ')')

uses the function horzcat, while

['insert into table (' name ') values (' var_name ')']

uses the fact that MATLAB treats strings as arrays of characters so that you can just use square brackets to form a large array containing the strings one after the other.

The third solution, offered by Amro, is a bit more sublte:

sprintf('insert into table (%s) values (%s)',name,var_name)

It tells the function sprintf (which is made for that purpose) "take the string which I supply as first parameter and replace occurences of %s with the strings I supply as the following parameters. This last technique is in particular useful if you also need to insert numbers into your string, because sprintf can also convert numbers to string and allows fine control over how they are formatted. You should have a close look at the help page for sprintf to know more :-).

Sign up to request clarification or add additional context in comments.

3 Comments

One more step - you probably need to convert the string in var_name to an SQL string literal, and sanitize the input to avoid Bobby Tables issues.
@Ibtesam: Andrew is right: have a look at bobby-tables.com or other sources about SQL injection attacks and make sure the mysql() function that you plan to use is not a security risk! It is okay if you yourself will be the only one typing in the var_name and name values, but if anyone else will be using this, be very carful!
@Ibtesam: one more to convince you: codinghorror.com/blog/2005/04/…
2

Try this instead:

mysql(['insert into table (' name ') values (' var_name ')']);

or even:

mysql(sprintf('insert into table (%s) values (%s)',name,var_name));

2 Comments

thanks @Amro for a prompt reply, but the thing is that anything written withing '' is treated as a string and not as a variable. I want to pass it a variable that contains the string.
@Ibtesam: this is string concatenation. Ex: str = 'hello '; disp([str 'world'])
1

I believe the problem you are having is the same as the one in this other question. It sounds like you want to create a command string that itself contains a ' delimited string, which would require you to escape each ' with another ' when you create your command string (note the first example in this string handling documentation). Note also you may want to use the 's' option for the INPUT function:

var_name = input('Enter the name: ','s');  %# Treats input like a string
commandString = sprintf('insert into table (name) values (''%s'')', var_name);
                            %# Note the two apostrophes --^
mysql(commandString);

If I were to enter Ken for the input, the string commandString would contain the following:

insert into table (name) values ('Ken')

And of course, as others have already mentioned, beware injection vulnerabilities.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.