A small mysql shell

xiaoxiao2021-03-06  115

#! / 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 by 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 makess.". "

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, 18), EVAL ("Self." "" 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 (/"% ")"% "/"""".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)

转载请注明原文地址:https://www.9cbs.com/read-99840.html

New Post(0)