A small, MySQL Shell

Source: Internet
Author: User
Tags exit eval expression connect mysql range return python mysql
Mysql
#!/usr/bin/python
#-*-coding:cp936-*-

#################################
# Written by Caocao #
# caocao@eastday.com #
# http://nethermit.yeah.net #
#################################

Import sys, string, OS
From types Import *
Import MySQLdb

Print "Written by Caocao"
Print "Caocao@eastday.com"
Print "Http://nethermit.yeah.net"
Print

def iif (expression, Whentrue, Whenfalse):
If expression:
Return whentrue
Else
Return Whenfalse

Class Mysqltest:
def __init__ (self, host= "localhost", user= "root", passwd= "", Db= ""):
Self.connection=none
Self.host=host
Self.user=user
self.passwd=passwd
Self.db=db
Self.result=none

Print "-" *40
print "MySQL Shell v 1.0"
print "Usage:python mysql.shell.py [host] [user] [passwd (% is empty)] [db]"
Print "Connect ..."
Try
Self.connection=mysqldb.connect (Host=self.host, User=self.user, PASSWD=SELF.PASSWD, db=self.db)
Except
print "Can" T connect to MySQL server.\nplease Make sure your username or password are right. "
Sys.exit (1)
Print "-" *40
Print self.printcomment ("Connection", "Get_server_info")
Print self.printcomment ("Connection", "Get_host_info")
Print self.printcomment ("Connection", "Get_proto_info")
Print self.printcomment ("Connection", "info")
Print self.printcomment ("Connection", "Character_set_name")
Print self.printcomment ("Connection", "thread_id")
Print self.printcomment ("Connection", "stat")

def __del__ (self):
If Self.connection!=none:
Self.connection.close ()
Print "-" *40
Print "Quit ..."

def printcomment (self, instance, function):
Return '%s =%s '% (string.rjust (function,%), eval ("self." +instance+ "." +function+ "()"))

def printall (self):
Output, row= "", Self.result.fetch_row (0)
For I in range (Self.result.num_fields ()):
Output+=repr (Self.result.describe () [i][0]) + "\ n"
For I in range (Self.result.num_rows ()):
For j in Range (Self.result.num_fields ()):
Output+=iif (Type (Row[i][j]) is StringType, Row[i][j], repr (ROW[I][J))) + "\ n"
Return output

def runSQL (self, querystring= "show Databases"):
Print "-" *40
Try
Self.connection.query (QueryString)
Except
print "Can ' t run SQL."
Else
Self.result=self.connection.store_result ()
Print self.printcomment ("Connection", "Field_count")
Print self.printcomment ("Connection", "Affected_rows")
Print self.printcomment ("Connection", "insert_id")
Print self.printcomment ("result", "Num_fields")
Print self.printcomment ("result", "num_rows")
Print self.printcomment ("result", "field_flags")
Print "-" *40
Print Self.printall ()

If __name__== "__main__":
argarray=sys.argv
Del Argarray[0]
Test=eval ("Mysqltest (\%s\") "%" \ "\" ". Join (Argarray)). Replace ("% "," "))
While True:
Try
Command=string.strip (Raw_input ("PS mysql>"), "" "
Commandlow=string.lower (command)
Except Eoferror:
Break
Else
If commandlow== "Exit" or commandlow== "quit":
Break
Elif commandlow== "":
Continue
Else
Test.runsql (command)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.