#-*- coding:utf-8 -*-

try:
    import MySQLdb as mdb
except:
    import pymysql as mdb
    mdb.install_as_MySQLdb()

from sys import argv

script, sql_host, sql_port, sql_user, sql_passwd, sql_db, sql_stmt = argv

connection = mdb.connect(host = sql_host, port = int(sql_port), user = sql_user, passwd = sql_passwd, db = sql_db)
cursor = connection.cursor()
try:
    cursor.execute(sql_stmt)
    records = cursor.fetchall()
    fmttedRecords = '\n'.join(' '.join(str(col) for col in record) for record in records)
    print fmttedRecords

finally:
    cursor.close()
    connection.close()

