Sometimes, if you want to update or migrate some SQL server data, you can only use the SQL statement to select the entire number of statements. Even the cursor of the advanced point cannot be converted? It's annoying to pick up VS and write a small program that keeps changing and compiling?
I want to use a script to write something?
This article describes how to use python,
AdodbapiDatabase Operations:
Adodbapi LibraryThere are several class libraries that python can operate on mssqlserver. Currently, few reliable classes support python3.x. The adodbapi uses ADO operations. (cannot cross-platform? Yes ?) So first install
Python for Windows extensions
With the pywin32 extension, you can do a lot of things, use it to operate EXCEL, etc. It is even more concise than VBScript, such as '''self. xlBook = self. xlApp. workbooks. open (filename) sht = self. xlBook. worksheets (sheet)
Sht. Cells (row, col). Value = value '''Detailed process:
First add reference:Import adodbapi
Adodbapi. adodbapi. verbose = False # adds details to the sample printout
Import adodbapi. ado_consts as adcCreate a connection:Cfg = {'server': '192. 168.29.86 \ eclexpress ', 'Password': 'xxxx', 'db': 'pscitemp '}
Constr = r "Provider = SQLOLEDB.1; Initial Catalog = % s; Data Source = % s; user ID = % s; Password = % s ;"\
% (Cfg ['db'], Cfg ['server'], 'sa ', Cfg ['Password'])
Conn = adodbapi. connect (constr) Where Cfg is a key-value dictionary, constr formatting syntax is commonly used in python2.x, you can use the following in 3.x.Execute the SQL statement:Cur = conn. cursor ()
SQL = ''' select * from softextBook where title = '{0}' and remark3! = '{1 }'
'''. Format (bookName, flag)
Cur.exe cute (SQL) data = cur. fetchall ()
Cur. close () The three quotation marks are similar to the "@" before the C # string. in python, one or two strings can be included, and the format syntax is similar.Execute the stored procedure:# Assume that proName has three parameters. The last parameter is null ret = cur. callproc ('procname', (parm1, parm2, None) conn. commit ()Close connectionConn. close () is simple? For more code examples, see unit test code in the installation directory: C: \ Python31 \ Lib \ site-packages \ adodbapi \ tests