| 首页 | 技术文章 | 软件下载 | 博客 | 论坛 | 精品教程 | 黑客动画 | 视频资源 | 在线服务 | 黑客游戏 | 

您现在的位置: 中国X黑客小组 >> 技术文章 >> 编程技术 >> 黑客编程 >> 文章正文 用户登录 新用户注册
  一个小巧的MySQL Shell         ★★★ 【字体:
一个小巧的MySQL Shell
作者:caocao    文章来源:CnXHacker.Net    点击数:    更新时间:2004-11-5    

#!/usr/bin/python
# -*- coding: cp936 -*-

#################################
#牋燱ritten by caocao牋牋牋牋牋?
#牋燾aocao@eastday.com牋牋牋牋牋#
#牋爃ttp://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:
牋爏elf.connection=MySQLdb.connect(host=self.host, user=self.user, passwd=self.passwd, db=self.db)
牋except:
牋爌rint "Can't connect to mysql server.\nPlease make sure your username or password is right."
牋爏ys.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:
牋爏elf.connection.close()
牋print "-"*40
牋print "Quit..."

def printComment(self, instance, function):
牋return "%s = %s" % (string.rjust(function, 18), eval("self."+instance+"."+function+"()"))

def printAll(self):
牋output, row="", self.result.fetch_row(0)
牋for i in range(self.result.num_fields()):
牋爋utput+=repr(self.result.describe()[i][0])+"\n"
牋for i in range(self.result.num_rows()):
牋爁or 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:
牋爏elf.connection.query(queryString)
牋except:
牋爌rint "Can't run sql."
牋else:
牋爏elf.result=self.connection.store_result()
牋爌rint self.printComment("connection", "field_count")
牋爌rint self.printComment("connection", "affected_rows")
牋爌rint self.printComment("connection", "insert_id")
牋爌rint self.printComment("result", "num_fields")
牋爌rint self.printComment("result", "num_rows")
牋爌rint self.printComment("result", "field_flags")
牋爌rint "-"*40
牋爌rint self.printAll()

if __name__=="__main__":
argArray=sys.argv
del argArray[0]
test=eval(("mysqlTest(\"%s\")" % "\",\"".join(argArray)).replace("%", ""))
while True:
牋try:
牋燾ommand=string.strip(raw_input("PS mysql>"), " ")
牋燾ommandLow=string.lower(command)
牋except EOFError:
牋燽reak
牋else:
牋爄f commandLow=="exit" or commandLow=="quit":
牋牋break
牋爀lif commandLow=="":
牋牋continue
牋爀lse:
牋牋test.runSQL(command)

文章录入:IceRiver    责任编辑:IceRiver 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
    瑞星公司08月19日发布 每日计
    瑞星公司07月19日发布 每日计
    为示爱攻破政府网站贴情书 
    瑞星公司06月19日发布 每日计
    19岁网贼偷游戏点卡获利15万
    Microsoft IE javascrip
    微软发布5月安全补丁 修正数
    javascript模拟的DOS窗口
    用javascript格式化你的
    瑞星公司05月19日发布 每日计
      网友评论:(只显示最新5条。评论内容只代表网友观点,与本站立场无关!)
    Powered by ICE RIVER - STUDIO
    » CnXHacker.CoM   © CopyRight 2002-2006, CnXHacker.CoM™, Inc. All Rights Reserved.