def test_InsertData(self): newTblName = 'NEW_TABLE' conn = DbConnect(self.tempDbName) try: conn.GetCursor().execute('drop table %s' % (newTblName)) except Exception: pass conn.Commit() conn.AddTable(newTblName, 'id int,val1 int, val2 int') for i in range(10): conn.InsertData(newTblName, (i, i + 1, 2 * i)) conn.Commit() d = conn.GetData(table=newTblName) assert len(d) == 10 self.assertEqual(len(conn.GetColumnNames(table=newTblName)), 3) conn.AddColumn(newTblName, 'val3', 'int') conn.Commit() self.assertEqual(len(conn.GetColumnNames(table=newTblName)), 4) d = conn.GetColumns('id,val3', table=newTblName) self.assertEqual(len(d), 10) self.assertTrue(all(r[1] is None for r in d)) for r in d: conn.InsertColumnData(newTblName, 'val3', r[0], 'id={0}'.format(r[0])) conn.Commit() d = conn.GetColumns('id,val3', table=newTblName) self.assertTrue(all(r[0] == r[1] for r in d)) d = None try: conn.GetCursor().execute('drop table %s' % (newTblName)) except Exception: assert 0, 'drop table failed'
def testGetData1(self): """ basic functionality """ conn = DbConnect(self.dbName, 'ten_elements') d = conn.GetData(randomAccess=1) assert len(d) == 10 assert tuple(d[0]) == (0, 11) assert tuple(d[2]) == (4, 31) self.assertRaises(IndexError, lambda: d[11]) d = conn.GetColumns(fields='id,val') self.assertEqual(len(d), 10) assert tuple(d[0]) == (0, 11) assert tuple(d[2]) == (4, 31) self.assertEqual(conn.GetDataCount(), 10)
def GetAllDescriptorNames(db, tbl1, tbl2, user='******', password='******'): """ gets possible descriptor names from a database **Arguments** - db: the name of the database to use - tbl1: the name of the table to be used for reading descriptor values - tbl2: the name of the table to be used for reading notes about the descriptors (*descriptions of the descriptors if you like*) - user: the user name for DB access - password: the password for DB access **Returns** a 2-tuple containing: 1) a list of column names 2) a list of column descriptors **Notes** - this uses _Dbase.DbInfo_ and Dfunctionality for querying the database - it is assumed that tbl2 includes 'property' and 'notes' columns """ from rdkit.Dbase.DbConnection import DbConnect conn = DbConnect(db, user=user, password=password) colNames = conn.GetColumnNames(table=tbl1) colDesc = map(lambda x: (x[0].upper(), x[1]), conn.GetColumns('property,notes', table=tbl2)) for name, desc in countOptions: colNames.append(name) colDesc.append((name, desc)) return colNames, colDesc