Ready to work. Need to install pymssql. Specifically how to install please own Baidu. A lot of tutorials. The simplest is the PIP install Myssql but there may be a version incompatibility issue. Causes the import to not Improt. It can also cause an error when packaging.
Three files required.
1. Parse the configuration file and connect to the database
#-*-coding:utf-8-*-__author__='Hero'ImportpymssqlImportXml.dom.minidomImportOSImportSyscurpath= Os.path.abspath (Os.path.dirname (__file__)) RootPath=Os.path.split (Curpath) [0]sys.path.append (RootPath)classExchangedata (object):Globalconfigtype_retestGlobalconfigtype_pingGlobalconfigtype_agetestGlobalconfigtype_ipx configtype_retest='Retest'configtype_ping='PING'configtype_agetest='agetest'configtype_ipx='IPX' ## #加载数据库连接数据 defLoaddbconfig (self,dbtype): XmlDoc= Xml.dom.minidom.parse (OS.GETCWD () +'\\conf\\/dbconfig.xml' #此处路径为配置文件的路径Root=xmldoc.documentelement sql_element=Root.getelementsbytagname (DbType) [0]returnsql_elementdef __exchangedatabydbconnection(self): Sqlinfo= Self.loaddbconfig ('MSSQL') Db_host= Sqlinfo.getelementsbytagname ('HOST') [0].firstchild.data db_database= Sqlinfo.getelementsbytagname ('DATABASE') [0].firstchild.data db_user= Sqlinfo.getelementsbytagname ('USERNAME') [0].firstchild.data db_pwd= Sqlinfo.getelementsbytagname ('PASSWORD') [0].firstchild.data Conn= Pymssql.connect (user=db_user,password=db_pwd,database=db_database,host=db_host) conn.autocommit (True)returnConn## #读取数据 # # # defGetdatabysql (SELF,SQLSTR):Try: Conn= self.__exchangedatabydbconnection() if notConn:Raise(Nameerror,'Database connection Failed') Else: Cur=conn.cursor () cur.execute (sqlstr) reslist=Cur.fetchall () conn.close ( )returnreslistexceptexception,e:Print(e)Raise(Nameerror,'database operation failed') ## #更改数据库 # # # defSetdatabysql (SELF,SQLSTR):Try: Conn= self.__exchangedatabydbconnection() if notConn:Raise(Nameerror,'Database connection Failed') returnFalseElse: Cur=conn.cursor () cur.execute (SQLSTR) conn.commit () Conn.close () returnTrueexceptexception,e:Print(e)Raise(Nameerror,'database operation failed') returnFalseif __name__=='__main__': PN='byaux-ctn0003'sqlstr="SELECT * from producttype where Productno = '"+pn+"'"Edata=exchangedata () reslist= Edata.getdatabysql (sqlstr=sqlstr) forDataRowinchreslist:Print(Datarow[0])
2. To carry out the operation of additional deletions, documents
1 #CODING=GBK2 3 fromExchangedataImport*4 Import Time5 6 ## #数据库相关操作7 classDatabaseop (object):8 ## #根据用户名密码, verify that the user rights return value is different, permissions are different # #9 defLoginaptester (Self,username,password):TenLoginsql ='SELECT rid from bdcomemployee WHERE bdcomloginname = \ ''+username+'\ ' and Bdcompassword = \ ''+password+'\ '' OneLoginsql =Self.dbtrim (loginsql) AEData =Exchangedata () -DataRows =edata.getdatabysql (loginsql) - ifLen (DataRows) ==0orDatarows[0] isNone: the return0 - elifDatarows[0][0]==17: - return2 - Else: + return1 - 98 127 ## #修改密码 # # # - defresetdbpwd (self,username,oldpwd,newpwd):129Igstatus =Self.loginaptester (USERNAME,OLDPWD) the ifIgstatus>0:131SetSQL ='UPDATE bdcomemployee SET bdcompassword = \ ''+newpwd+'\ ' WHERE bdcomloginname = \ ''+username+'\ ' and Bdcompassword = \ ''+oldpwd+'\ '' the Self.dbtrim (SetSQL)133EData =Exchangedata ()134 returnedata.setdatabysql (SetSQL)135 Else:136 returnFalse;137 138 139 155 169 the defDbtrim (SELF,TRIMSTR):171 returnSTR (TRIMSTR). Replace ('\ t',"'). Replace ('\ n',"'). Replace ('\ r',"')172 173 if __name__=='__main__':174Dbop =Databaseop ()175Dbop.loginaptester ('Liuke_ap','Liuke')
3. Configuration files
1 <?xml version="1.0" encoding="UTF-8"?> 2 <JDBC>3 <MSSQL>4 <HOST>192.168.1.1</HOST>5 <DATABASE>abcd</DATABASE>6 <USERNAME>root</USERNAME>7 <PASSWORD>root</PASSWORD>8 </MSSQL>9 </JDBC>
Python connects to the database.