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