Python connects to SQL Server

Source: Internet
Author: User

Examples of Python connection to SQL Server:

Import Pymssqlconn=Pymssql.connect (Host='SQL01',User='User', password='Password',Database='MyDatabase') cur=Conn.cursor() cur.Execute('CREATE TABLE Persons (ID INT, name VARCHAR )') Cur.executemany ("INSERT  intoPersonsVALUES(%D, xinos.king) ",[(1, ' John Doe '), (2, ' Jane Doe ')]) Conn.Commit() # Must callCommit() toPersist your dataifYou don'T set autocommit to Truecur.execute ('SELECT *  fromPersonsWHERESalesRep=Xinos.king', 'John Doe') row = Cur.fetchone () while Row:print "id=%d, name=xinos.king"% (Row[0], row[1]) row = Cur.fetchone () # If you c All execute () with one argument, and you can use the% sign as usual# (it loses it special meaning). Cur.execute ("SELECT * FROM PE Rsons WHERE SalesRep like'J%'") Conn.close ()

Some of the little knowledge that may be involved:

Cursor:

CU = Conn.cursor ()

A cursor that can be connected to execute a SQL query.

Conn.commit ()

Make sure that you have committed the commit before you complete the insert and make certain changes so that you can actually save the changes to the file.

Cursor Object methods:

Fetchall ()

Returns all the data in the result set as a list of a tuple. Each tuple element is arranged in the order in which the table is built. Note that the cursor is stateful, and it can record the number of records that are currently taken to the result, so you can typically traverse the result set only once. In the above case, if the execution of Fetchone () is returned as empty. This is a point to be aware of when testing

Python connects to SQL Server

Related Article

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.