Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8060437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:59:25+00:00 2026-06-05T09:59:25+00:00

I have a .sql file containing a bunch of SQL queries, with each query

  • 0

I have a .sql file containing a bunch of SQL queries, with each query spanning multiple lines. I want to execute these queries in MySQL via Python using MySQLdb.

sqlite3 has “a nonstandard shortcut” for this purpose called executescript(), but there doesn’t seem to be any equivalent function in MySQLdb.

I noticed this old question from 2 years ago which asks the same thing, but I found the answers unsatisfying. The answers are basically:

Use subprocess to run the mysql command and send it your .sql file.

This works, but it is rather inelegant, and it introduces unwanted complexity with error handling and such.

If each query is on a single line, just execute each line separately.

But in my case, they span multiple lines, so this won’t work.

If each query is not on a single line, somehow join them.

But, how? I mean, I can hack up something easily enough so there’s no need for you to reply with half-baked answers here, and maybe that’s what I’ll end up doing, but is there already an established library that does this? I’d feel more comfortable with a comprehensive and correct solution rather than a hack.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:59:27+00:00Added an answer on June 5, 2026 at 9:59 am

    MySQLdb seems to allow this out of the box, you just have to call cursor.nextset() to cycle through the returned result sets.

    db = conn.cursor()
    db.execute('SELECT 1; SELECT 2;')
    
    more = True
    while more:
        print db.fetchall()
        more = db.nextset()
    

    If you want to be absolutely sure the support for this is enabled, and/or disable the support, you can use something like this:

    MYSQL_OPTION_MULTI_STATEMENTS_ON = 0
    MYSQL_OPTION_MULTI_STATEMENTS_OFF = 1
    
    conn.set_server_option(MYSQL_OPTION_MULTI_STATEMENTS_ON)
    # Multiple statement execution here...
    conn.set_server_option(MYSQL_OPTION_MULTI_STATEMENTS_OFF)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .sql file from a MySQL dump containing tables, definitions and data
I have a .SQL file containing a large number of queries. They are being
I have an export SQL file containing tables and data from MySQL and I
I have a .sql script file that I want to run once every 24
I have written a PHP script which generates an SQL file containing all tables
I have a file containing several SQL statements that I'd like to use to
I have a file containing a lot of SQL statements, such as: CREATE TABLE
I have a csv format file, which I want to import to sql server
I have a .sql file containing thousands of individual insert statements. It takes forever
I have a text file containing the following string: --jonesj (release 00) some sql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.