Python Opearte MS-SQL Use Pymssql, opeartepymssql

Source: Internet
Author: User

Python Opearte MS-SQL Use Pymssql, opeartepymssql

See a lot of open source database will use MySQL, Python also use, but I have been used to the use of graphical interface, a strong sense of Operation MS-SQL

Seeing that Python also provides the MS-SQL connection method, you need to use PyMssql.

In the Windows dos cmd command, enter:

pip install pymssl

Pymssql documentation link http://pymssql.org/en/latest/pymssql_examples.html#iterating-through-results

imoort pymssqlconn=pymssql.connect("192.168.6.112","sa","123456","FactoryHome")cursor=conn.cursor()cursor.execute("select * from usera")row=cursor.fetchone()print(row[0])

Connect: Connect to the database address, port and other basic configurations

Cursor: This is a bit like a database Cursor, but I don't know if it will affect a large volume of data.

Fetchone, Fetchall is said to return a List. so it is easy to think of loop to get data

for row in cursor:    print ("%s -> %s ",(row[0], row[1]))

Execute is also used to insert Pymssql.

Import pymssqlconn = pymssql. connect ("192.168.6.112", "sa", "", "FactoryHome", "UTF-8" )cursor=conn.cursor()cursor.exe cute ("inset into Table_1 Values (1, 'ddddddd', 'zhang san ') ") conn. commit () conn. close ()

In the insert Chinese operation, it is best to bring the character set of UTF-8, It is not best to be sure to bring, without it will report an error.

Pymssql updates. Currently, execute is also used.

Import pymssqlconn = pymssql. connect ("192.168.6.112", "sa", "", "FactoryHome", "UTF-8" )cursor1_conn.cursor()cursor.exe cute ("Update Table_1 set Name = '王' where id = 3") conn. commit () conn. close ()

The same is true for Pymssql delete operations.

To be continued

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.