how to create batch file -- i want run  mysql query and write the result into .text file

Posted on 2009-12-23
Last Modified: 2012-05-08
how to create batch file -- i want run  mysql query and write the result into .text file
Question by:bowebell
    LVL 23

    Expert Comment


    Author Comment

    No i want excute qurery directly and write the result into text file ?

    if possible i need to pass arguments to the script file too?
    LVL 23

    Expert Comment

    LVL 21

    Accepted Solution

    Check following bat file:

    @echo off
    echo Parameter Passed - %1
    mysql -uroot -ppass -e "query" > a.txt

    This will output result of query to a.txt.
    LVL 67

    Assisted Solution

    For a single query, not to long, theGhost's answer is appropriate. If you have something longer, you could consider to use the way as posted in the last link, but IMHO better is this:

    @echo off
    (echo select *
     echo from table1
     echo where field1 = '%1';
    ) | mysql -uroot -ppass > result.txt

    because you do not need any temporary files.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
    VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now