1. Introduction of PYMSSQL Package
Import pymssql
2. Establish the connection:
conn = Pymssql.connect (server=conf[' db_address '),
Port= "1433", user=conf[' Db_username '),
password=conf[' Db_password '),
database=conf[' db_name '),
charset= "UTF-8")
cursor = Conn.cursor ()
3. Execute the SQL statement:
Cursor.execute (SQL)
4. Get the data:
search_table = Cursor.fetchall ()
5. Modify the data:
Cursor.execute (SQL, (str (Datetime.today ())))
Conn.commit ()
To define a Python class
#toolClass To write data into fileClass OutFile: Dir_="' Sew_=' = = ' Def__init__(Self, dir, Sew=' = = '): Self. dir_= Dir Self. sew_= Sew DefWrite_line(Self, line, date): FP=Open(Self. dir_+"/"+ Date,' AB ') Print line FP.Write(Line) FP.Write(‘\ n‘) FP.Close() defWrite_vector(SelfVec, date): FP=Open(Self. dir_+"/"+ Date,' AB ')IfLen(VEC)>0:For I inRange(0,Len(VEC)-1): FP.Write(VEC[I]) FP.Write(Self. sew_) FP.Write(VEC[Len(VEC)-1]) FP.Write(‘\ n‘) FP.Close() defWrite_dict(Self, Dict, date): FP=Open(Self. dir_+"/"+ Date,' AB ')For key in Dict: FP.Write(Key) FP.Write(‘ : ‘) Leng=Len(Dict[Key])For I inRange(0, Leng-1): FP.Write(Dict[Key][i]) FP.Write(Self. sew_) FP.Write(Dict[Key][leng-1]) FP.Write(‘\ n‘) FP.Close() defWrite_dict_dict(Self, dict_dict, date): FP=Open(Self. dir_+"/"+ Date,' AB ')For key in Dict_dict: FP.Write(Key.Encode(' Latin-1 ')) Print key.Encode(' Latin-1 ')#fp.Write(Key.Decode(' Utf-8 ').Encode(' GBK ')) Dict= Dict_dict[Key] FP.Write(Self. sew_)For Key_word in Dict:#fp.Write(Key_word.Decode(' Utf-8 ').Encode(' gb2312 ')) FP.Write(Key_word.Encode(' Latin-1 '))#fp.Write(Key_word.Decode(' Utf-8 ').Encode(' GBK ')) FP.Write(‘:‘) FP.write (str ( Dict[key_word]) Fp. Write (Self.sew_) FP write ( ' \ N ' ) fp. Close () outfile = outfile ( ' report ' /span>