本文整理汇总了Python中pymssql.connect方法的典型用法代码示例。如果您正苦于以下问题:Python pymssql.connect方法的具体用法?Python pymssql.connect怎么用?Python pymssql.connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymssql
的用法示例。
在下文中一共展示了pymssql.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def main():
options = parse_args()
mssql, total, host = connect_db(options)
if options.mode =='test':
run_tests(mssql, options, host)
elif not options.mode or options.mode == 'time2connect':
return_nagios( options,
stdout='Time to connect was %ss',
label='time',
unit='s',
result=total )
else:
execute_query(mssql, options, host)
示例2: db_connect
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def db_connect(args):
if args.type == "mysql" or args.type == "mariadb":
import mysql.connector
try:
connection = mysql.connector.connect(
user=args.user,
password=args.password,
database=args.db)
except mysql.connector.Error as err:
print(colorize("red", "[ERROR] {}".format(err)))
return None
elif args.type == "mssql":
import pymssql
try:
connection = pymssql.connect(server="localhost", database=args.db)
except pymssql.Error as err:
print(colorize("red", "[ERROR] {}".format(err)))
return None
elif args.type == "pgsql":
import psycopg2
try:
connection = psycopg2.connect(
"dbname='{}' user='{}' password='{}'".format(
args.db, args.user, args.password))
except psycopg2.Error as err:
print(colorize("red", "[ERROR] {}".format(err)))
return None
elif args.type == "oracle":
import cx_Oracle
try:
connection = cx_Oracle.connect(
args.user, args.password, cx_Oracle.makedsn(
'127.0.0.1', 1521, args.db), mode=cx_Oracle.SYSDBA)
except cx_Oracle.Error as err:
print(colorize("red", "[ERROR] {}".format(err)))
return None
return connection
示例3: makeConnection
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def makeConnection(self):
if self.driver in ('odbc', 'pyodbc', 'pypyodbc'):
self.debugWriter.write(
"ODBC connect string: " + self.odbc_conn_str)
timeout = self.timeout
if timeout:
kw = dict(timeout=timeout)
else:
kw = dict()
conn = self.module.connect(self.odbc_conn_str, **kw)
if timeout:
conn.timeout = timeout
else:
conn_descr = self.make_conn_str(self)
if isinstance(conn_descr, dict):
conn = self.dbconnection(**conn_descr)
else:
conn = self.dbconnection(conn_descr)
cur = conn.cursor()
cur.execute('SET ANSI_NULLS ON')
cur.execute("SELECT CAST('12345.21' AS DECIMAL(10, 2))")
self.decimalSeparator = str(cur.fetchone()[0])[-3]
cur.close()
return conn
示例4: _connect
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def _connect(self, database, **kwargs):
if not pymssql:
raise ImproperlyConfigured('pymssql must be installed')
if kwargs.pop('use_legacy_datetime', False):
self.field_overrides['datetime'] = 'datetime'
self.field_overrides['date'] = 'nvarchar(15)'
self.field_overrides['time'] = 'nvarchar(10)'
return pymssql.connect(database=database, **kwargs)
示例5: get_conn
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def get_conn(self):
"""
Returns a mssql connection object
"""
conn = self.get_connection(self.mssql_conn_id) # pylint: disable=no-member
# pylint: disable=c-extension-no-member
conn = pymssql.connect(
server=conn.host,
user=conn.login,
password=conn.password,
database=self.schema or conn.schema,
port=conn.port,
)
return conn
示例6: rotate
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def rotate(record, newpassword):
""" Grab any required fields from the record """
user = record.login
oldpassword = record.password
result = False
host = record.get('cmdr:host')
db = record.get('cmdr:db')
connection = ''
try:
# Connect to the database
connection = pymssql.connect(host=host,
user=user,
password=oldpassword,
db=db)
with connection.cursor() as cursor:
print("Connected to %s"%(host))
# Create a new record
sql = 'ALTER LOGIN %s WITH PASSWORD="%s";'%(user, newpassword)
cursor.execute(sql)
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
record.password = newpassword
result = True
except:
print("Error during connection to Microsoft SQL server")
finally:
if connection:
connection.close()
return result
示例7: connect
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def connect(self):
self.initConnection()
try:
self.connector = pymssql.connect(host="%s:%d" % (self.hostname, self.port), user=self.user, password=self.password, database=self.db, login_timeout=conf.timeout, timeout=conf.timeout)
except (pymssql.InterfaceError, pymssql.OperationalError), msg:
raise SqlmapConnectionException(msg)
示例8: connect
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def connect(self):
self.initConnection()
try:
self.connector = pymssql.connect(host="%s:%d" % (self.hostname, self.port), user=self.user, password=self.password, database=self.db, login_timeout=conf.timeout, timeout=conf.timeout)
except pymssql.OperationalError, msg:
raise SqlmapConnectionException(msg)
示例9: connect
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def connect(self, type=None):
"""Initiate the connect with SAP B1 and MS SQL server.
"""
if type == "COM":
SAPbobsCOM = __import__(current_app.config['DIAPI'], globals(), locals(), [], -1)
self.constants = getattr(SAPbobsCOM, "constants")
Company = getattr(SAPbobsCOM, "Company")
company = Company()
company.Server = current_app.config['SERVER']
company.UseTrusted = False
company.language = eval("self.constants." + current_app.config['LANGUAGE'])
company.DbServerType = eval("self.constants." + current_app.config['DBSERVERTYPE'])
company.CompanyDB = current_app.config['COMPANYDB']
company.UserName = current_app.config['B1USERNAME']
company.Password = current_app.config['B1PASSWORD']
company.Connect()
log = "Open SAPB1 connection for " + company.CompanyName
current_app.logger.info(log)
return SAPB1COMAdaptor(company=company)
elif type == "CURSOR":
sqlSrvConn = pymssql.connect(current_app.config['SERVER'],
current_app.config['DBUSERNAME'],
current_app.config['DBPASSWORD'],
current_app.config['COMPANYDB'])
log = "Open SAPB1 DB connection"
current_app.logger.info(log)
return MSSQLCursorAdaptor(sqlSrvConn=sqlSrvConn)
else:
return None
示例10: comAdaptor
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def comAdaptor(self):
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'sapb1COMAdaptor'):
ctx.sapb1COMAdaptor = self.connect(type="COM")
return ctx.sapb1COMAdaptor
示例11: cursorAdaptor
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def cursorAdaptor(self):
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'msSQLCursorAdaptor'):
ctx.msSQLCursorAdaptor = self.connect(type="CURSOR")
return ctx.msSQLCursorAdaptor
示例12: get_new_connection
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def get_new_connection(self, conn_params):
return Database.connect(**conn_params)
示例13: __get_dbms_version
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def __get_dbms_version(self, make_connection=True):
"""
Returns the 'DBMS Version' string, or ''. If a connection to the
database has not already been established, a connection will be made
when `make_connection` is True.
"""
if not self.connection and make_connection:
self.connect()
with self.connection.cursor() as cursor:
cursor.execute("SELECT SERVERPROPERTY('productversion')")
return cursor.fetchone()[0]
示例14: mssqlburp
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def mssqlburp(ip,port):
addr = (ip,int(port))
with open('mssql\user.txt') as user:
users = user.readlines()
user.close()
with open('mssql\pass.txt') as passs:
passwords = passs.readlines()
passs.close()
sock_1433 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock_1433.settimeout(1)
sock_1433.connect(addr)
host = ip + ':' + str(port)
for un in users:
un = un.strip()
for pwd in passwords:
pwd = pwd.strip()
try:
conn = pymssql.connect(host=host,user=un,password=pwd)
sys.stdout.write('%s:%d has MicroSoft SQL WeakPass, %s:%s\n' % (ip, port, un, pwd))
except Exception,e:
continue
except:
sock_1433.close()
示例15: connect_db
# 需要导入模块: import pymssql [as 别名]
# 或者: from pymssql import connect [as 别名]
def connect_db(options):
host = options.hostname
if options.instance:
host += "\\" + options.instance
elif options.port:
host += ":" + options.port
start = time.time()
mssql = pymssql.connect(host = host, user = options.user, password = options.password, database=options.table)
total = time.time() - start
return mssql, total, host