python pymssql__basic-features.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python pymssql__basic-features.py相关的知识,希望对你有一定的参考价值。

"""
Basic features (strict DB-API compliance)

http://pymssql.org/en/latest/pymssql_examples.html
"""
from os import getenv
import pymssql

server = getenv("PYMSSQL_TEST_SERVER")
user = getenv("PYMSSQL_TEST_USERNAME")
password = getenv("PYMSSQL_TEST_PASSWORD")

conn = pymssql.connect(server, user, password, "tempdb")
cursor = conn.cursor()
cursor.execute("""
IF OBJECT_ID('persons', 'U') IS NOT NULL
    DROP TABLE persons
CREATE TABLE persons (
    id INT NOT NULL,
    name VARCHAR(100),
    salesrep VARCHAR(100),
    PRIMARY KEY(id)
)
""")
cursor.executemany(
    "INSERT INTO persons VALUES (%d, %s, %s)",
    [(1, 'John Smith', 'John Doe'),
     (2, 'Jane Doe', 'Joe Dog'),
     (3, 'Mike T.', 'Sarah H.')])
# you must call commit() to persist your data if you don't set autocommit to True
conn.commit()

cursor.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
row = cursor.fetchone()
while row:
    print("ID=%d, Name=%s" % (row[0], row[1]))
    row = cursor.fetchone()

conn.close()

以上是关于python pymssql__basic-features.py的主要内容,如果未能解决你的问题,请参考以下文章

python pymssql__initial-conn.py

python pymssql__basic-features.py

pymssql,无法执行EXEC命令sql

pymssql连接sql server报错:pymssql._pymssql.OperationalError

Python pymssql 插入错误

使用 pymssql 使用 Python 导入 .bak MySQL 数据库