Skip to content

Instantly share code, notes, and snippets.

@aalonzolu
Created May 12, 2017 15:37
Show Gist options
  • Save aalonzolu/b8f8fca90b2181b14f561fef5a2a3587 to your computer and use it in GitHub Desktop.
Save aalonzolu/b8f8fca90b2181b14f561fef5a2a3587 to your computer and use it in GitHub Desktop.
Execute SQL in all databases
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