本文整理汇总了Python中pyPgSQL.PgSQL.connect方法的典型用法代码示例。如果您正苦于以下问题:Python PgSQL.connect方法的具体用法?Python PgSQL.connect怎么用?Python PgSQL.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyPgSQL.PgSQL
的用法示例。
在下文中一共展示了PgSQL.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def __init__(self, connectArgs):
connectArgs = string.split(connectArgs,':')
host = None
if connectArgs and connectArgs[0]: #if host is there
if '|' in connectArgs[0]: #if specified port
host = connectArgs[0].replace('|', ':')
if connectArgs and connectArgs[-1] == 'verbose':
self.verbose = 1
connectArgs = connectArgs[:-1]
else:
self.verbose=0
if connectArgs and connectArgs[-1] == 'cache':
self.useCacheMod = 1
connectArgs = string.join(connectArgs[:-1], ':')
else:
connectArgs = string.join(connectArgs, ':')
self.useCacheMod =0
self.connectArgs = connectArgs
if self.useCacheMod:
from PyPgSQLcache import getConnection
self.conn = getConnection(connectArgs)
else:
if host is not None:
self.conn = PgSQL.connect(connectArgs, host = host)
else:
self.conn = PgSQL.connect(connectArgs)
self.bindVariables = 0
示例2: __init__
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def __init__(self, db_name, db_host):
from pyPgSQL import PgSQL
if len(db_host) == 0:
db_user = pwd.getpwuid(os.geteuid())[0]
self.connection = PgSQL.connect(database=db_name, user=db_user)
else:
self.connection = PgSQL.connect(database=db_name, host=db_host)
self.connection.autocommit = AUTOCOMMIT
示例3: connect
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def connect(self):
""" Connect to the database"""
if len(self.dbhost):
self.dbh = PgSQL.connect(database=self.dbname, host=self.dbhost,
user=self.dbuser, password=self.dbpass,
client_encoding="utf-8",
unicode_results=1)
else:
self.dbh = PgSQL.connect(database=self.dbname,
user=self.dbuser,
password=self.dbpass,
client_encoding="utf-8",
unicode_results=1)
示例4: __init__
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def __init__(self, dsn = None, dbapi = None):
if dsn == None:
dsn ="localhost:gnumed:gm-dbo:pg"
if dbapi == None:
use_pgdb = '-pgdb' in sys.argv
try:
from pyPgSQL import PgSQL
dbapi = PgSQL
l = dsn.split(':')
if len(l) == 4:
l = [l[0]] + [''] + l[1:]
dsn = ':'.join(l)
except:
print sys.exc_info()[0], sys.exc_info()[1]
use_pgdb = 1
if use_pgdb:
import pgdb
dbapi = pgdb
self.dsn = dsn
try:
self.conn = dbapi.connect(dsn)
return
except:
print sys.exc_info()[0], sys.exc_info()[1]
self.conn = PgSQL.connect(dsn)
示例5: launchCachingProcess
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def launchCachingProcess(self):
starttime = datetime.datetime.now()
Logger.ZEyeLogger().write("Port ID caching started")
try:
pgsqlCon = PgSQL.connect(host=netdiscoCfg.pgHost,user=netdiscoCfg.pgUser,password=netdiscoCfg.pgPwd,database=netdiscoCfg.pgDB)
pgcursor = pgsqlCon.cursor()
pgcursor.execute("SELECT ip,name,vendor FROM device ORDER BY ip")
try:
pgres = pgcursor.fetchall()
for idx in pgres:
while self.getThreadNb() >= self.max_threads:
time.sleep(1)
pgcursor.execute("SELECT snmpro FROM z_eye_snmp_cache where device = '%s'" % idx[1])
pgres2 = pgcursor.fetchone()
devip = idx[0]
devname = idx[1]
vendor = idx[2]
if pgres2:
devcom = pgres2[0]
else:
devcom = self.defaultSNMPRO
thread.start_new_thread(self.fetchSNMPInfos,(devip,devname,devcom,vendor))
""" Wait 1 second to lock program, else if script is too fast,it exists without discovering"""
time.sleep(1)
except StandardError, e:
Logger.ZEyeLogger().write("Port ID Caching: FATAL %s" % e)
except PgSQL.Error, e:
Logger.ZEyeLogger().write("Port ID Caching: Pgsql Error %s" % e)
sys.exit(1);
示例6: __init__
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def __init__(self, in_userid):
self.__tasks = []
self.db = PgSQL.connect (database="jackdesert_groove", user="jackdesert_groove", password="123567")
self.cur = self.db.cursor()
# self.reset_table(self.cur)
self.__userid = int(in_userid)
self.__utcoffset = int(self.retrieve_utcoffset())
示例7: _connect
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def _connect(host="", database="", user="", password=""):
"""Opens a connection to the database.
Normally, this function does not have to be called, because the other
functions of this module connect to the database automatically. If invoked
without parameters, it uses the default connection parameters for the DES
database. If, for some reason, you need to connect to a different database
or use different credentials, you can invoke this function with the desired
parameters. Further calls of functions from this module will then use that
connection.
"""
global _connection
# Use the default values for the connection, if the parameters are not given
if host == "":
host = _HOST
if database == "":
database = _DATABASE
if user == "":
user = _USER
if password == "":
password = _PASSWORD
# Make a connection to the database and check to see if it succeeded.
try:
_connection = PgSQL.connect(host=host, database=database, user=user,\
password=password)
except PgSQL.Error, msg:
errstr = "Connection to database '%s' failed\n%s" % (_DATABASE, msg)
raise Error(errstr.strip())
示例8: launchCfgGenerator
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def launchCfgGenerator(self):
while self.SNMPcc.isRunning():
self.logDebug("SNMP community caching is running, waiting 10 seconds")
time.sleep(10)
self.launchMsg()
try:
pgsqlCon = PgSQL.connect(host=zConfig.pgHost,user=zConfig.pgUser,password=zConfig.pgPwd,database=zConfig.pgDB)
pgcursor = pgsqlCon.cursor()
pgcursor.execute("SELECT ip,name FROM device ORDER BY ip")
try:
pgres = pgcursor.fetchall()
for idx in pgres:
devip = idx[0]
devname = idx[1]
devcom = self.SNMPcc.getReadCommunity(devname)
# Only launch process if SNMP cache is ok
if devcom == None:
self.logError("No read community found for %s" % devname)
else:
thread.start_new_thread(self.fetchMRTGInfos,(devip,devname,devcom))
except StandardError, e:
self.logCritical(e)
return
except PgSQL.Error, e:
self.logCritical("FATAL PgSQL %s" % e)
sys.exit(1);
示例9: main
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def main():
db = PgSQL.connect(database='casemgr')
curs = db.cursor()
curs.execute('create index wqdesc on workqueues (description);')
curs.execute('select unit_id from units where unit_id not in (select unit_id from workqueues where unit_id is not null)')
unit_ids = fetchids(curs)
curs.execute('select user_id from users where user_id not in (select user_id from workqueues where user_id is not null)')
user_ids = fetchids(curs)
print 'Units %d, Users %d' % (len(unit_ids), len(user_ids))
# Create workqueues
many(curs, 'insert wq', 'insert into workqueues (name,description, unit_id, user_id) values (%s,%s,%s,%s)', wq_rows(unit_ids, user_ids))
# Find shared queues
curs.execute("select queue_id from workqueues where unit_id is null and user_id is null and description = 'X'")
shared_queues = fetchids(curs)
# Add members to shared queues
print 'Shared queues %s' % len(shared_queues)
many(curs, 'insert wqm', 'insert into workqueue_members (queue_id, unit_id, user_id) values (%s,%s,%s)', wqm_rows(shared_queues, unit_ids, user_ids))
# Create tasks
curs.execute("select master_id from cases")
case_ids = fetchids(curs)
curs.execute("select queue_id from workqueues where description='X'")
queue_ids = fetchids(curs)
many(curs, 'insert tasks', 'insert into tasks (queue_id, task_description, case_id) values (%s, %s, %s)', task_rows(case_ids, queue_ids))
curs.execute('drop index wqdesc;')
db.commit()
示例10: getConnection
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def getConnection(connUser):
"""
Returns a database connection as defined by
connUser. If this module already has an open
connection for connUser, it returns it; otherwise,
it creates a new connection, stores it, and returns it.
"""
if not _users.has_key(connUser):
raise SkunkStandardError, "user %s is not initialized" % (connUser)
connectParams = _users[connUser]
if not _connections.has_key(connectParams):
try:
connectArgs = string.split(connectParams, ":")
host = None
if connectArgs[0]:
if "|" in connectArgs[0]: # if specified port
host = connectArgs[0].replace("|", ":")
_connections[connectParams] = PgSQL.connect(connectParams, host=host)
except PgSQL.Error:
# XXX Do not raise the connect string! The trace may be seen
# by users!!!
raise SkunkStandardError, ("cannot connect to PostgreSQL: %s" % (sys.exc_info()[1],))
return _connections[connectParams]
示例11: launchCachingProcess
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def launchCachingProcess(self):
while self.SNMPcc.isRunning():
self.logDebug("SNMP community caching is running, waiting 10 seconds")
time.sleep(10)
try:
pgsqlCon = PgSQL.connect(host=zConfig.pgHost, user=zConfig.pgUser, password=zConfig.pgPwd, database=zConfig.pgDB)
pgcursor = pgsqlCon.cursor()
pgcursor.execute("SELECT ip,name,vendor FROM device ORDER BY ip")
try:
pgres = pgcursor.fetchall()
for idx in pgres:
while self.getThreadNb() >= self.max_threads:
time.sleep(1)
devip = idx[0]
devname = idx[1]
vendor = idx[2]
devcom = self.SNMPcc.getReadCommunity(devname)
if devcom == None:
self.logError("No read community found for %s" % devname)
else:
thread.start_new_thread(self.fetchSNMPInfos,(devip,devname,devcom,vendor))
""" Wait 1 second to lock program, else if script is too fast,it exists without discovering"""
time.sleep(1)
except StandardError, e:
self.logCritical(e)
except PgSQL.Error, e:
self.logCritical("Pgsql Error %s" % e)
return
示例12: __init__
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def __init__(self, host, port, database, username, password) :
IDB.__init__(self)
self.dbh = PgSQL.connect(":".join([host, port, database, username, password]))
self.albumtreestore = gtk.TreeStore(gobject.TYPE_PYOBJECT,
str, gtk.gdk.Pixbuf, int, int)
self.canViewExtensions = [
'.jpg',
'.jpeg',
'.thm',
]
self.canTrackExtensions = [
'.bmp',
'.png',
'.jpeg',
'.jpg',
'.thm',
'.gif',
'.pcx',
'.pnm',
'.tiff',
'.tiff',
'.iff',
'.xpm',
'.ico',
'.cur',
'.ani',
]
示例13: launchCfgGenerator
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def launchCfgGenerator(self):
Logger.ZEyeLogger().write("MRTG configuration discovery started")
starttime = datetime.datetime.now()
try:
pgsqlCon = PgSQL.connect(host=netdiscoCfg.pgHost,user=netdiscoCfg.pgUser,password=netdiscoCfg.pgPwd,database=netdiscoCfg.pgDB)
pgcursor = pgsqlCon.cursor()
pgcursor.execute("SELECT ip,name FROM device ORDER BY ip")
try:
pgres = pgcursor.fetchall()
for idx in pgres:
pgcursor2 = pgsqlCon.cursor()
pgcursor2.execute("SELECT snmpro FROM z_eye_snmp_cache where device = '%s'" % idx[1])
pgres2 = pgcursor2.fetchone()
devip = idx[0]
devname = idx[1]
if pgres2:
devcom = pgres2[0]
else:
devcom = self.defaultSNMPRO
thread.start_new_thread(self.fetchMRTGInfos,(devip,devname,devcom))
except StandardError, e:
Logger.ZEyeLogger().write("MRTG-Config-Discovery: FATAL %s" % e)
return
except PgSQL.Error, e:
Logger.ZEyeLogger().write("MRTG-Config-Discovery: FATAL PgSQL %s" % e)
sys.exit(1);
示例14: __init__
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def __init__(self):
host = 'localhost'
dbname = 'clue'
user = 'clue'
passwd = 'clue'
self.cnx = PgSQL.connect('%s::%s:%s:%s' % (host, dbname, user, passwd))
self.cur = self.cnx.cursor()
示例15: handle_url
# 需要导入模块: from pyPgSQL import PgSQL [as 别名]
# 或者: from pyPgSQL.PgSQL import connect [as 别名]
def handle_url(bot, user, channel, url, msg):
return
if not config:
return
cx = PgSQL.connect(database=config["database"], host=config["host"], user=config["user"], password=config["password"])
cur = cx.cursor()
# find the oldest instance of given url on this channel
cur.execute("SELECT * FROM pyfibot.urls WHERE channel=%s AND url=%s ORDER BY timestamp;", (channel, url))
res = cur.fetchone()
if res:
url, channel, userhost, timestamp, urlid = res
pastetime = datetime.datetime.fromtimestamp(res[3].ticks())
now = datetime.datetime.now()
age = now - pastetime
agestr = ""
if age.days > 0:
agestr += "%d days " % age.days
secs = age.seconds
hours, minutes, seconds = secs // 3600, secs // 60 % 60, secs % 60
if hours > 0:
agestr += "%d h " % hours
if minutes > 0:
agestr += "%d m " % minutes
if seconds > 0:
agestr += "%d s" % seconds
# don't alert for the same person
if getNick(user) != getNick(userhost):
if channel != "#wow":
bot.say(channel, "%s: wanha. (by %s %s ago)" % (getNick(user), getNick(userhost), agestr))
cur = cx.cursor()
# always store URLs, this structure can handle it, sqlite can't
cur.execute("INSERT INTO pyfibot.urls (userid, channel, url, timestamp) VALUES(%s, %s, %s, NOW());", (user, channel, url))
cx.commit()
cur.close()
cx.close()