当前位置: 首页>>代码示例>>Python>>正文


Python DbModule.connect方法代码示例

本文整理汇总了Python中rdkit.Dbase.DbModule.connect方法的典型用法代码示例。如果您正苦于以下问题:Python DbModule.connect方法的具体用法?Python DbModule.connect怎么用?Python DbModule.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rdkit.Dbase.DbModule的用法示例。


在下文中一共展示了DbModule.connect方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: GetTableNames

# 需要导入模块: from rdkit.Dbase import DbModule [as 别名]
# 或者: from rdkit.Dbase.DbModule import connect [as 别名]
def GetTableNames(dBase, user='sysdba', password='masterkey', includeViews=0, cn=None):
  """ returns a list of tables available in a database

    **Arguments**

      - dBase: the name of the DB file to be used

      - user: the username for DB access

      - password: the password to be used for DB access

      - includeViews: if this is non-null, the views in the db will
        also be returned

    **Returns**

      - a list of table names (strings)

  """
  if not cn:
    try:
      cn = DbModule.connect(dBase, user, password)
    except Exception:
      print('Problems opening database: %s' % (dBase))
      return []
  c = cn.cursor()
  if not includeViews:
    comm = DbModule.getTablesSql
  else:
    comm = DbModule.getTablesAndViewsSql
  c.execute(comm)
  names = [str(x[0]).upper() for x in c.fetchall()]
  if RDConfig.usePgSQL and 'PG_LOGDIR_LS' in names:
    names.remove('PG_LOGDIR_LS')
  return names
开发者ID:abradle,项目名称:rdkit,代码行数:37,代码来源:DbInfo.py

示例2: GetColumnNames

# 需要导入模块: from rdkit.Dbase import DbModule [as 别名]
# 或者: from rdkit.Dbase.DbModule import connect [as 别名]
def GetColumnNames(dBase, table, user='sysdba', password='masterkey', join='', what='*', cn=None):
  """ gets a list of columns available in a DB table

    **Arguments**

      - dBase: the name of the DB file to be used

      - table: the name of the table to query

      - user: the username for DB access

      - password: the password to be used for DB access

      - join: an optional join clause  (omit the verb 'join')

      - what: an optional clause indicating what to select

    **Returns**

      -  a list of column names

  """
  if not cn:
    cn = DbModule.connect(dBase, user, password)
  c = cn.cursor()
  cmd = 'select %s from %s' % (what, table)
  if join:
    if join.strip().find('join') != 0:
      join = 'join %s' % (join)
    cmd += ' ' + join
  c.execute(cmd)
  c.fetchone()
  desc = c.description
  res = [str(x[0]) for x in desc]
  return res
开发者ID:abradle,项目名称:rdkit,代码行数:37,代码来源:DbInfo.py

示例3: GetDbNames

# 需要导入模块: from rdkit.Dbase import DbModule [as 别名]
# 或者: from rdkit.Dbase.DbModule import connect [as 别名]
def GetDbNames(user='sysdba', password='masterkey', dirName='.', dBase='::template1', cn=None):
  """ returns a list of databases that are available

    **Arguments**

      - user: the username for DB access

      - password: the password to be used for DB access

    **Returns**

      - a list of db names (strings)

  """
  if DbModule.getDbSql:
    if not cn:
      try:
        cn = DbModule.connect(dBase, user, password)
      except Exception:
        print('Problems opening database: %s' % (dBase))
        return []
    c = cn.cursor()
    c.execute(DbModule.getDbSql)
    if RDConfig.usePgSQL:
      names = ['::' + str(x[0]) for x in c.fetchall()]
    else:
      names = ['::' + str(x[0]) for x in c.fetchall()]
    names.remove(dBase)
  elif DbModule.fileWildcard:
    import os.path, glob
    names = glob.glob(os.path.join(dirName, DbModule.fileWildcard))
  else:
    names = []
  return names
开发者ID:abradle,项目名称:rdkit,代码行数:36,代码来源:DbInfo.py

示例4: GetColumns

# 需要导入模块: from rdkit.Dbase import DbModule [as 别名]
# 或者: from rdkit.Dbase.DbModule import connect [as 别名]
def GetColumns(dBase,table,fieldString,user='sysdba',password='masterkey',
               join='',cn=None):
  """ gets a set of data from a table

    **Arguments**

     - dBase: database name

     - table: table name
     
     - fieldString: a string with the names of the fields to be extracted,
        this should be a comma delimited list

     - user and  password:

     - join: a join clause (omit the verb 'join')
       

    **Returns**

     - a list of the data

  """
  if not cn:
    cn = DbModule.connect(dBase,user,password)
  c = cn.cursor()
  cmd = 'select %s from %s'%(fieldString,table)
  if join:
    if join.strip().find('join') != 0:
      join = 'join %s'%(join)
    cmd +=' ' + join
  c.execute(cmd)
  return c.fetchall()
开发者ID:Acpharis,项目名称:rdkit,代码行数:35,代码来源:DbUtils.py

示例5: GetColumnNamesAndTypes

# 需要导入模块: from rdkit.Dbase import DbModule [as 别名]
# 或者: from rdkit.Dbase.DbModule import connect [as 别名]
def GetColumnNamesAndTypes(dBase, table, user='sysdba', password='masterkey', join='', what='*',
                           cn=None):
  """ gets a list of columns available in a DB table along with their types

    **Arguments**

      - dBase: the name of the DB file to be used

      - table: the name of the table to query

      - user: the username for DB access

      - password: the password to be used for DB access

      - join: an optional join clause (omit the verb 'join')

      - what: an optional clause indicating what to select

    **Returns**

      - a list of 2-tuples containing:

          1) column name

          2) column type

  """
  if not cn:
    cn = DbModule.connect(dBase, user, password)
  c = cn.cursor()
  cmd = 'select %s from %s' % (what, table)
  if join:
    cmd += ' join %s' % (join)
  c.execute(cmd)
  return GetColumnInfoFromCursor(c)
开发者ID:abradle,项目名称:rdkit,代码行数:37,代码来源:DbInfo.py

示例6: DatabaseToText

# 需要导入模块: from rdkit.Dbase import DbModule [as 别名]
# 或者: from rdkit.Dbase.DbModule import connect [as 别名]
def DatabaseToText(dBase,table,fields='*',join='',where='',
                  user='sysdba',password='masterkey',delim=',',cn=None):
  """ Pulls the contents of a database and makes a deliminted text file from them

    **Arguments**
      - dBase: the name of the DB file to be used

      - table: the name of the table to query

      - fields: the fields to select with the SQL query

      - join: the join clause of the SQL query
        (e.g. 'join foo on foo.bar=base.bar')

      - where: the where clause of the SQL query
        (e.g. 'where foo = 2' or 'where bar > 17.6')

      - user: the username for DB access

      - password: the password to be used for DB access

    **Returns**

      - the CSV data (as text) 

  """
  if len(where) and where.strip().find('where')==-1:
    where = 'where %s'%(where)
  if len(join) and join.strip().find('join') == -1:
    join = 'join %s'%(join)
  sqlCommand = 'select %s from %s %s %s'%(fields,table,join,where)
  if not cn:
    cn = DbModule.connect(dBase,user,password)
  c = cn.cursor()
  c.execute(sqlCommand)
  headers = []
  colsToTake = []
  # the description field of the cursor carries around info about the columns
  #  of the table
  for i in range(len(c.description)):
    item = c.description[i]
    if item[1] not in DbInfo.sqlBinTypes:
      colsToTake.append(i)
      headers.append(item[0])

  lines = []
  lines.append(delim.join(headers))

  # grab the data
  results = c.fetchall()
  for res in results:
    d = _take(res,colsToTake)
    lines.append(delim.join(map(str,d)))

  return '\n'.join(lines)
开发者ID:Acpharis,项目名称:rdkit,代码行数:57,代码来源:DbUtils.py

示例7: _AddDataToDb

# 需要导入模块: from rdkit.Dbase import DbModule [as 别名]
# 或者: from rdkit.Dbase.DbModule import connect [as 别名]
def _AddDataToDb(dBase,table,user,password,colDefs,colTypes,data,
                 nullMarker=None,blockSize=100,cn=None):
  """ *For Internal Use*

    (drops and) creates a table and then inserts the values

  """
  if not cn:
    cn = DbModule.connect(dBase,user,password)
  c = cn.cursor()
  try:
    c.execute('drop table %s'%(table))
  except:
    print('cannot drop table %s'%(table))
  try:
    sqlStr = 'create table %s (%s)'%(table,colDefs)
    c.execute(sqlStr)
  except:
    print('create table failed: ', sqlStr)
    print('here is the exception:')
    import traceback
    traceback.print_exc()
    return
  cn.commit()
  c = None
  
  block = []
  entryTxt = [DbModule.placeHolder]*len(data[0])
  dStr = ','.join(entryTxt)
  sqlStr = 'insert into %s values (%s)'%(table,dStr)
  nDone = 0
  for row in data:
    entries = [None]*len(row)
    for col in xrange(len(row)):
      if row[col] is not None and \
         (nullMarker is None or row[col] != nullMarker):
        if colTypes[col][0] == types.FloatType:
          entries[col] = float(row[col])
        elif colTypes[col][0] == types.IntType:
          entries[col] = int(row[col])
        else:
          entries[col] = str(row[col])
      else:
        entries[col] = None
    block.append(tuple(entries))
    if len(block)>=blockSize:
      nDone += _insertBlock(cn,sqlStr,block)
      if not hasattr(cn,'autocommit') or not cn.autocommit:
        cn.commit()
      block = []
  if len(block):
    nDone += _insertBlock(cn,sqlStr,block)
  if not hasattr(cn,'autocommit') or not cn.autocommit:
    cn.commit()
开发者ID:Acpharis,项目名称:rdkit,代码行数:56,代码来源:DbUtils.py

示例8: GetCursor

# 需要导入模块: from rdkit.Dbase import DbModule [as 别名]
# 或者: from rdkit.Dbase.DbModule import connect [as 别名]
  def GetCursor(self):
    """ returns a cursor for direct manipulation of the DB
      only one cursor is available

    """
    if self.cursor is not None:
      return self.cursor
      
    self.cn = DbModule.connect(self.dbName,self.user,self.password)
    self.cursor = self.cn.cursor()
    return self.cursor
开发者ID:ASKCOS,项目名称:rdkit,代码行数:13,代码来源:DbConnection.py

示例9: GetData

# 需要导入模块: from rdkit.Dbase import DbModule [as 别名]
# 或者: from rdkit.Dbase.DbModule import connect [as 别名]
def GetData(dBase,table,fieldString='*',whereString='',user='sysdba',password='masterkey',
            removeDups=-1,join='',forceList=0,transform=None,randomAccess=1,extras=None,cn=None):
  """ a more flexible method to get a set of data from a table

    **Arguments**

     - fields: a string with the names of the fields to be extracted,
          this should be a comma delimited list

     - where: the SQL where clause to be used with the DB query

     - removeDups indicates the column which should be used to screen
        out duplicates.  Only the first appearance of a duplicate will
        be left in the dataset.

    **Returns**

      - a list of the data


    **Notes**

      - EFF: this isn't particularly efficient

  """
  if not cn:
    cn = DbModule.connect(dBase,user,password)
  c = cn.cursor()
  cmd = 'select %s from %s'%(fieldString,table)
  if join:
    if join.strip().find('join') != 0:
      join = 'join %s'%(join)
    cmd += ' ' + join
  if whereString:
    if whereString.strip().find('where')!=0:
      whereString = 'where %s'%(whereString)
    cmd += ' ' + whereString

  if forceList:
    try:
      if not extras:
        c.execute(cmd)
      else:
        c.execute(cmd,extras)
    except:
      sys.stderr.write('the command "%s" generated errors:\n'%(cmd))
      import traceback
      traceback.print_exc()
      return None
    if transform is not None:
      raise ValueError('forceList and transform arguments are not compatible')
    if not randomAccess:
      raise ValueError('when forceList is set, randomAccess must also be used')
    data = c.fetchall()
    if removeDups>0:
      seen = []
      for entry in data[:]:
        if entry[removeDups] in seen:
          data.remove(entry)
        else:
          seen.append(entry[removeDups])
  else:
    if randomAccess:
      klass = RandomAccessDbResultSet 
    else:
      klass = DbResultSet 

    data = klass(c,cn,cmd,removeDups=removeDups,transform=transform,extras=extras)

  return data
开发者ID:Acpharis,项目名称:rdkit,代码行数:72,代码来源:DbUtils.py


注:本文中的rdkit.Dbase.DbModule.connect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。