使用python的MySQLdb模块 使用正则判断输入sql是否是在相应方法下使用 功能:查询select、插入insert,删除delete,update功能,desc,show,执行sql文件
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2015-12-08 00:49:16 # @Author : eddy ([email protected]) # @Link : http://my.oschina.net/eddylinux # @Version : 1.0 import MySQLdb import re class Conn_Mysql(): def __init__(self,db): self.host = '192.168.X.X' self.user = 'test123' self.password = '123456' self.db = db def desc(self,SQL): try: db = MySQLdb.connect(self.host,self.user,self.password,self.db) cursor = db.cursor() if re.match('(desc.*?)', SQL,re.I): cursor.execute(SQL) data = cursor.fetchall() print "SQL Result:",list(data) else: print 'please DESC' db.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" %(e.args[0], e.args[1]) def show(self,SQL): try: db = MySQLdb.connect(self.host,self.user,self.password,self.db) cursor = db.cursor() if re.match('(show.*?)', SQL,re.I): cursor.execute(SQL) data = cursor.fetchall() print "SQL Result:",list(data) else: print 'please SHOW' db.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" %(e.args[0], e.args[1]) def select(self,SQL): try: db = MySQLdb.connect(self.host,self.user,self.password,self.db) cursor = db.cursor() if re.match('(select.*?)', SQL,re.I): cursor.execute(SQL) data = cursor.fetchall() print "SQL Result:",list(data) else: print 'please SELECT SQL' db.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" %(e.args[0], e.args[1]) def update(self,SQL): try: db = MySQLdb.connect(self.host,self.user,self.password,self.db) cursor = db.cursor() if re.match('(update.*?)', SQL,re.I): cursor.execute(SQL) db.commit() else: print 'please UPDATE SQL' except MySQLdb.Error,e: print "Mysql Error %d: %s" %(e.args[0], e.args[1]) def insert(self,SQL): try: db = MySQLdb.connect(self.host,self.user,self.password,self.db) cursor = db.cursor() if re.match('(insert.*?)', SQL,re.I): cursor.execute(SQL) else: print 'please INSERT SQL' db.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" %(e.args[0], e.args[1]) def delete(self,SQL): try: db = MySQLdb.connect(self.host,self.user,self.password,self.db) cursor = db.cursor() if re.match('(delete.*?)', SQL,re.I): cursor.execute(SQL) else: print 'please DELETE SQL' db.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" %(e.args[0], e.args[1]) def sql_file(self,file): try: db = MySQLdb.connect(self.host,self.user,self.password,self.db) cursor = db.cursor() with open(file,'r') as sql_file: for sql in sql_file: cursor.execute(sql) db.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" %(e.args[0], e.args[1]) if __name__ == '__main__': my_sql_file1 = Conn_Mysql('user') my_sql_file1.sql_file('add_user.sql') my_select = Conn_Mysql('user') my_select.select('select * from user;') my_sql_file1 = Conn_Mysql('user') my_sql_file2.sql_file('delete_user.sql') my_insert = Conn_Mysql('user') my_insert.insert('insert into user values("eddy","123456");') my_delete = Conn_Mysql('user') my_delete.delete('delete from user where username="eddy";') my_update = Conn_Mysql('user') my_update.update('update user set password="ok" where username="yangys";') my_select.select('select * from user;') my_desc = Conn_Mysql('user') my_desc.desc('desc user') 结果 SQL Result: [('admin', '123qwe!@#'), ('yangys', 'ok'), ('haozw', '123456'), ('luojie', '1234567'), ('yys6', 'password1'), ('yys5', 'password1'), ('yys4', 'password1'), ('yys3', 'password1'), ('yys2', 'password1'), ('yys1', 'password1'), ('yys1', 'password1'), ('yys2', 'password1'), ('yys3', 'password1'), ('yys4', 'password1'), ('yys5', 'password1'), ('yys6', 'password1')] SQL Result: [('admin', '123qwe!@#'), ('yangys', 'ok'), ('haozw', '123456'), ('luojie', '1234567')] SQL Result: [('username', 'varchar(20)', 'YES', '', None, ''), ('password', 'varchar(20)', 'YES', '', None, '')] [Finished in 0.6s]