I am attempting to write a shell script that executes a query on a mysql database. It seems to run fine in the mysql panel, but not in the script.

The code is:

mysql -u username -ppassword -e 'delete from table where forumid = 25 and replace (title, ' ', '') like '%word%'';

When I run this script I get this error: Access denied for username@localhost to database ' , ) like %word%'

Any idea what is causing this error?

Thanks

Hi!

The problem is that you're not providing a database name! Try this: mysql -uusername -ppassword database_name ... Hope that helps!
-G

EDIT: And to clarify that error you're getting, since you're surrounding your "-e" with single quotes (' '), and you have single quotes inside the string, it ends your query at the next occurrence of single quotes:

'delete from table where forumid = 25 and replace (title, '

and thinks the next part:

' , ) like %word%'

is supposed to be the database name! Try using regular quotes in one of those instances. Backslashes might be used to escape the occurrence inside the query... not sure though without testing it... :)

-G

Gromit,

Thanks for the help. I simply forgot to include the use database statement in my example, but I wouldn't have thought of the ' causing the problem.

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.