Created
May 12, 2017 15:37
-
-
Save aalonzolu/b8f8fca90b2181b14f561fef5a2a3587 to your computer and use it in GitHub Desktop.
Execute SQL in all databases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MySQLdb | |
connection = MySQLdb.connect( | |
host = 'localhost', | |
user = 'root', | |
passwd = 'secret') # create the connection | |
cursor = connection.cursor() # get the cursor | |
cursor.execute("SHOW DATABASES") # execute 'SHOW TABLES' (but data is not returned) | |
tables = cursor.fetchall() | |
#UPDATE %d.table SET col='value' WHERE id=1; | |
query = input("SQL: (%d for database name)>> ") | |
array_databases = [] | |
for (table_name,) in cursor: | |
tmp_query = query.replace("%d",table_name) | |
print(tmp_query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment