MySQL Forums
Forum List  »  Newbie

Re: how to save query results locally
Posted by: Barry Galbraith
Date: October 18, 2017 03:06PM

There is an "export/import" section on the toolbar of the results window. You can use that to save your results to .csv file.
But that isn't what you really want is it?

The example you posted in your original post is for mysql commandline.
Mysql -user -pass -e"select LastName from tbEmployees" > C:/output

That is meant to be run at a Windows commandline, aka "a DOS prompt".
But even that won't work. There is no database selected in your example.

So, a couple of things.
You need mysql commandline client installed on your machine. It is installed as part of Workbench.

So, open a Windows commandline.
Microsoft Windows [Version 10.0.15063]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\Hein>

Now start MySQL commandline

Microsoft Windows [Version 10.0.15063]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\Hein>mysql -uroot -p

Enter root password when prompted.
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.6.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

You now need to USE your database

mysql>USE db_name

Now you can run your query, and redirect the output to a file. Note that you need to read/write privileges on the folder you are redirecting to. Your example is root of C drive, and you propbably won't have privileges there.

mysql>select LastName from tbEmployees > C:/output.txt

This example runs MySQL commandline in interactive mode.
But you can run it non-interactive.

The -u switch is for the user.
-p switch is the user's password Note no space between -p and the password.
Then the database name.
-e switch is sql command to run.
Then redirect the output to a file in a folder you have privilege on.

You will get a warning that using the password on the commandline is insecure

mysql -uroot -proot_password db_name -e"select LastName from tbEmployees" > C:/output

The mysql connection defaults to localhost. If mysql server isn't on your local machine, you'll need the -h switch and the name (or ip address) of the host.

The MySQL host will also have to have your user and host you are connecting from enabled as a user, with privilege on the database you want you want to query.

If you want help with Workbench, best ask in the Workbench forum.

Good luck,
Barry.

Options: ReplyQuote


Subject
Written By
Posted
October 17, 2017 12:36PM
Re: how to save query results locally
October 18, 2017 03:06PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.