Use Python to operate the MSSQL database.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.