Load data into an existing database table via a DB-API 2.0
    connection or cursor. As :func:`petl.io.db.todb` except that the database
    table will be appended, i.e., the new data will be inserted into the
    table, and any existing rows will remain.
541120
def appenddb(table, dbo, tablename, schema=None, commit=True):
    

    needs_closing = False

    # convenience for working with sqlite3
    if isinstance(dbo, string_types):
        import sqlite3
        dbo = sqlite3.connect(dbo)
        needs_closing = True

    try:
        _todb(table, dbo, tablename, schema=schema, commit=commit,
              truncate=False)

    finally:
        if needs_closing:
            dbo.close()
841311
def insert_object_into_db_pk_known(self,
                                       obj: Any,
                                       table: str,
                                       fieldlist: Sequence[str]) -> None:
        """"""
        pkvalue = getattr(obj, fieldlist[0])
        if pkvalue is None:
            raise AssertionError("insert_object_intoto_db_pk_known called "
                                 "without PK")
        valuelist = []
        for f in fieldlist:
            valuelist.append(getattr(obj, f))
        self.db_exec(
            get_sql_insert(table, fieldlist, self.get_delims()),
            *valuelist
        )
549063
def insert(self, payload):
        
        response = self.session.post(self._get_table_url(), data=json.dumps(payload))
        return self._get_content(response)
860801
def insert_row(dbconn, tablename, *args):
    
    cur = dbconn.cursor()
    cur.execute("INSERT INTO '{name}' VALUES{args}".format(name=tablename, args=args))
    dbconn.commit()
129905
def append_table(self, name, **kwargs):
        """"""
        self.stack.append(Table(name, **kwargs))
451484
def createTable(dbconn, pd):
    """"""
    cols = ('%s %s' % (defn.name, getTypename(defn)) for defn in pd.fields)
    sql  = 'CREATE TABLE IF NOT EXISTS %s (%s)' % (pd.name, ', '.join(cols))

    dbconn.execute(sql)
    dbconn.commit()
1373669
function addData (db, mime, source) {
  Object.keys(mime).forEach(function (key) {
    var data = mime[key]
    var type = key.toLowerCase()
    var obj = db[type] = db[type] || createTypeEntry(source)

    // add missing data
    setValue(obj, 'charset', data.charset)
    setValue(obj, 'compressible', data.compressible)

    // append new extensions
    appendExtensions(obj, data.extensions)
  })
}
446043
def db_chainstate_append(cls, cur, **fields):
        
        missing = []
        extra = []
        for reqfield in CHAINSTATE_FIELDS:
            if reqfield not in fields:
                missing.append(reqfield)

        for fieldname in fields:
            if fieldname not in CHAINSTATE_FIELDS:
                extra.append(fieldname)

        if len(missing) > 0 or len(extra) > 0:
            raise ValueError("Invalid fields: missing: {}, extra: {}".format(','.join(missing), ','.join(extra)))

        query = 'INSERT INTO chainstate ({}) VALUES ({});'.format(
                ','.join( CHAINSTATE_FIELDS ),
                ','.join( ['?'] * len(CHAINSTATE_FIELDS)))

        args = tuple([fields[fieldname] for fieldname in CHAINSTATE_FIELDS])
        cls.db_query_execute(cur, query, args)
        return True
315309
def insertDict(self, tblname, d, fields = None):
		''''''
		
		if fields == None:
			fields = sorted(d.keys())
		values = None
		try:
			SQL = 'INSERT INTO %s (%s) VALUES (%s)' % (tblname, join(fields, ", "), join(['%s' for x in range(len(fields))], ','))
			values = tuple([d[k] for k in fields])
	 		self.locked_execute(SQL, parameters = values)
		except Exception, e:
			if SQL and values:
				sys.stderr.write("\nSQL execution error in query '%s' %% %s at %s:" % (SQL, values, datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
			sys.stderr.write("\nError: '%s'.\n" % (str(e)))
			sys.stderr.flush()
			raise Exception("Error occurred during database insertion: '%s'." % str(e))
1299027
def insert(dbconn, packet):
    """"""
    values = [ ]
    pd     = packet._defn

    for defn in pd.fields:
        if defn.enum:
            val = getattr(packet.raw, defn.name)
        else:
            val = getattr(packet, defn.name)

        if val is None and defn.name in pd.history:
            val = getattr(packet.history, defn.name)

        values.append(val)

    qmark = ['?'] * len(values)
    sql   = 'INSERT INTO %s VALUES (%s)' % (pd.name, ', '.join(qmark))

    dbconn.execute(sql, values)
1225743
def _add_matched_objects_to_database(
            self,
            matchedObjects):
        
        self.log.info(
            'starting the ``_add_matched_objects_to_database`` method')

        print "Adding the matched sources to the `pyephem_positions` database table"

        allMatches = []
        for m in matchedObjects:
            allMatches += m

        dbSettings = self.settings["database settings"]["atlasMovers"]

        insert_list_of_dictionaries_into_database_tables(
            dbConn=self.atlasMoversDBConn,
            log=self.log,
            dictList=allMatches,
            dbTableName="pyephem_positions",
            uniqueKeyList=["expname", "object_name"],
            dateModified=True,
            batchSize=10000,
            replace=True,
            dbSettings=dbSettings
        )

        self.log.info(
            'completed the ``_add_matched_objects_to_database`` method')
        return None
408642
public void addNew() throws DBException
    {
        this.getRecord().setEditMode(Constants.EDIT_NONE);
        m_objectID = null;
        m_dataSource = null;
        try   {
            this.doAddNew();
            m_iRecordStatus = DBConstants.RECORD_NEW;
        } catch (DBException ex)   {
            throw ex;
        }
        this.getRecord().setEditMode(Constants.EDIT_ADD);   // I do this before the handleNewRecord behaviors do anything
        this.getRecord().handleNewRecord();   // Display Fields
    }
68044
def _load_data(self, data, from_db=False):
        
        self._data = data[:]
        self.setattrs(
            values=[],
            node_stack=[],
            node_dict={},
        )
        self._from_db = from_db
391230
def insertDataset(self, blockcontent, otptIdList, migration=False):
        
        dataset = blockcontent['dataset']
        conn = self.dbi.connection()

        # First, check and see if the dataset exists.
        try:
            datasetID = self.datasetid.execute(conn, dataset['dataset'])
            dataset['dataset_id'] = datasetID
        except KeyError as ex:
            if conn:conn.close()
            dbsExceptionHandler("dbsException-invalid-input2", "DBSBlockInsert/InsertDataset: Dataset is required.\
                Exception: %s.  troubled dataset are: %s" %(ex.args[0], dataset),
	        self.logger.exception, "DBSBlockInsert/InsertDataset: Dataset is required.\
                Exception: %s.  troubled dataset are: %s" %(ex.args[0], dataset	))
        except Exception as ex1:
            if conn:conn.close()
            raise ex1
        if datasetID > 0:
            # Then we already have a valid dataset. We only need to fill the map (dataset & output module config)
            # Skip to the END
            try:
                self.insertDatasetWOannex(dataset = dataset,
                                          blockcontent = blockcontent,
                                          otptIdList = otptIdList, conn = conn,
                                          insertDataset = False, migration=migration)
            finally:
                if conn:conn.close()
            return datasetID

        # Else, we need to do the work
        #Start a new transaction
        tran = conn.begin()
        primary_ds_name = ''
        try:
            #1. Deal with primary dataset. Most primary datasets are
            #pre-installed in db
            primds = blockcontent["primds"]
            primary_ds_name = primds["primary_ds_name"]
            primds["primary_ds_id"] = self.primdsid.execute(conn,
                                      primds["primary_ds_name"], transaction=tran)
            if primds["primary_ds_id"] <= 0:
                #primary dataset is not in db yet.
                try:
                    primds["primary_ds_id"] = self.sm.increment(conn, "SEQ_PDS")
                    primds["creation_date"] = primds.get("creation_date", dbsUtils().getTime())
                    if not migration:
                        primds["create_by"] = dbsUtils().getCreateBy()
                    self.primdsin.execute(conn, primds, tran)
                except exceptions.IntegrityError as ex:
                    if (str(ex).find("ORA-00001") != -1 and str(ex).find("TUC_PDS_PRIMARY_DS_NAME") != -1)\
                        or str(ex).lower().find("duplicate") !=-1:
                        primds["primary_ds_id"] = self.primdsid.execute(conn,
                                                primds["primary_ds_name"],
                                                transaction=tran)
                        if primds["primary_ds_id"] <= 0:
                            if tran:tran.rollback()
                            if conn:conn.close()
                            dbsExceptionHandler('dbsException-conflict-data',
                                'Primary dataset not yet inserted by concurrent insert. ',
                                self.logger.exception,
                                'Primary dataset not yet inserted by concurrent insert. '+ str(ex))

                    elif str(ex).find("ORA-01400") > -1:
                        if tran:tran.rollback()
                        if conn:conn.close()
                        dbsExceptionHandler('dbsException-missing-data',
                            'Missing data when insert primary_datasets. ',
                            self.logger.exception,
                            'Missing data when insert primary_datasets. '+ str(ex))
                    else:
                        if tran:tran.rollback()
                        if conn:conn.close()
                        dbsExceptionHandler('dbsException-invalid-input2',
                            'Invalid data when insert primary_datasets. ',
                            self.logger.exception,
                            'Invalid data when insert primary_datasets. '+ str(ex))
                except Exception as ex:
                    if tran:tran.rollback()
                    if conn:conn.close()
                    raise
            dataset['primary_ds_id'] = primds["primary_ds_id"]
            #2 Deal with processed ds
            #processed ds is handled inside of dataset insertion, However we need to make sure it is formatted correctly.
            #processed_ds_name is not required pre-exist in the db. will insert with the dataset if not in yet
            #
            #         processed_ds_name=acquisition_era_name[-filter_name][-processing_str]-vprocessing_version
            #         Note [-filterName] is new as 4/30/2012. See ticket #3655. YG
            #
            #althrough acquisition era and processing version is not required for a dataset
            #in the schema(the schema is build this way because
            #we need to accommodate the DBS2 data), but we impose the requirement on the API.
            #So both acquisition and processing eras are required.
            #We do the format checking after we deal with acquisition era and processing era.
            #
            #YG 12/07/2011  TK-362

            #3 Deal with Acquisition era
            aq = blockcontent.get('acquisition_era', {})
            has_acquisition_era_name = 'acquisition_era_name' in aq
            has_start_date = 'start_date' in aq

            def insert_acquisition_era():
                try:
                    #insert acquisition era into db
                    aq['acquisition_era_id'] = self.sm.increment(conn, "SEQ_AQE")
                    self.acqin.execute(conn, aq, tran)
                    dataset['acquisition_era_id'] = aq['acquisition_era_id']
                except exceptions.IntegrityError as ei:
                    #ORA-01400: cannot insert NULL into required columns, usually it is the NULL on start_date
                    if "ORA-01400" in str(ei) :
                        if tran:tran.rollback()
                        if conn:conn.close()
                        dbsExceptionHandler("dbsException-invalid-input2",
                                            "BlockInsert: acquisition_era_name and start_date are required. \
                                            NULL was received from user input. Please correct your data.")
                    #ok, already in db?
                    if (str(ei).find("ORA-00001") != -1 and str(ei).find("TUC_AQE_ACQUISITION_ERA_NAME") != -1)\
                        or str(ei).lower().find("duplicate") !=-1:
                        dataset['acquisition_era_id'] = self.acqid.execute(conn, aq['acquisition_era_name'])
                        if dataset['acquisition_era_id'] <= 0:
                            if tran:tran.rollback()
                            if conn:conn.close()
                            dbsExceptionHandler("dbsException-invalid-input2", "BlockInsert: \
Check the spelling of acquisition Era name. The db may already have the same \
acquisition era, but with different cases.", self.logger.exception, "BlockInsert: \
Check the spelling of acquisition Era name. The db may already have the same \
acquisition era, but with different cases.")
                    else:
                        if tran:tran.rollback()
                        if conn:conn.close()
                        dbsExceptionHandler('dbsException-invalid-input2',
                            'Invalid data when insert acquisition_eras . ',
                            self.logger.exception,
                            'Invalid data when insert acquisition_eras. '+ str(ei))
 
                except Exception:
                    if tran:tran.rollback()
                    if conn:conn.close()
                    raise

            if has_acquisition_era_name and has_start_date:
                insert_acquisition_era()

            elif migration and not has_acquisition_era_name:
                #if no processing era is available, for example for old DBS 2 data, skip insertion
                aq['acquisition_era_id'] = None
                dataset['acquisition_era_id'] = None

            elif migration and not aq['start_date']:
                aq['start_date'] = 0
                insert_acquisition_era()

            else:
                if tran:tran.rollback()
                if conn:conn.close()
                dbsExceptionHandler("dbsException-invalid-input2", "BlockInsert: Acquisition Era is required", 
			self.logger.exception, "BlockInsert: Acquisition Era is required")

            #4 Deal with Processing era
            pera = blockcontent.get('processing_era', {})

            if 'processing_version' in pera:
                try:
                    #insert processing era into db
                    pera['processing_era_id'] = self.sm.increment(conn, "SEQ_PE")
                    #pera['processing_version'] = pera['processing_version'].upper()
                    self.procsingin.execute(conn, pera, tran)
                    dataset['processing_era_id'] = pera['processing_era_id']
                except exceptions.IntegrityError as ex:
                    if (str(ex).find("ORA-00001: unique constraint") != -1 and \
                                str(ex).find("TUC_PE_PROCESSING_VERSION") != -1) or \
                                str(ex).lower().find("duplicate") !=-1:
                        #ok, already in db
                        dataset['processing_era_id'] = self.procsingid.execute(conn, pera['processing_version'])
                    elif str(ex).find("ORA-01400") > -1:
                        if tran:tran.rollback()
                        if conn:conn.close()
                        dbsExceptionHandler('dbsException-missing-data',
                            'Missing data when insert processing_eras. ',
                            self.logger.exception,
                            'Missing data when insert Processing_eras. '+ str(ex))
                    else:
                        if tran: tran.rollback()
                        if conn: conn.close()
                        dbsExceptionHandler('dbsException-invalid-input2',
                            'Invalid data when insert Processing_ears. ',
                            self.logger.exception,
                            'Invalid data when insert Processing_eras. '+ str(ex))
                except Exception as ex:
                    if tran: tran.rollback()
                    if conn: conn.close()
                    raise
            elif migration:
                #if no processing era is available, for example for old DBS 2 data, skip insertion
                pera['processing_era_id'] = None
                dataset['processing_era_id'] = None
            else:
                if tran: tran.rollback()
                if conn: conn.close()
                dbsExceptionHandler('dbsException-invalid-input2', 'BlockInsert:processing version is required')

            #Make sure processed_ds_name is right format.
            #processed_ds_name=acquisition_era_name[-filter_name][-processing_str]-vprocessing_version
            #In order to accommodate DBS2 data for migration, we turn off this check in migration.
            #These will not cause any problem to none DBS2 data because when we migration, the none DBS2 data is
            #already checked when they were inserted into the source dbs.  YG 7/12/2012
            if not migration and aq["acquisition_era_name"] != "CRAB" and aq["acquisition_era_name"] != "LHE":
                erals=dataset["processed_ds_name"].rsplit('-')
                if erals[0] != aq["acquisition_era_name"] or erals[len(erals)-1] != "%s%s"%("v", pera["processing_version"]):
                    if tran:tran.rollback()
                    if conn:conn.close()
                    dbsExceptionHandler('dbsException-invalid-input2', "BlockInsert:\
                   processed_ds_name=acquisition_era_name[-filter_name][-processing_str]-vprocessing_version must be satisified.",
		   self.logger.exception, 
		   "BlockInsert: processed_ds_name=acquisition_era_name[-filter_name][-processing_str]-vprocessing_version must be satisified."	)

            #So far so good, let's commit first 4 db activities before going on.
            tran.commit()
        except KeyError as ex:
            if tran:tran.rollback()
            if conn:conn.close()
            dbsExceptionHandler("dbsException-invalid-input2", "DBSBlockInsert/insertOutputModuleConfig: \
                                         KeyError exception: %s. " %ex.args[0], self.logger.exception,
				         "DBSBlockInsert/insertOutputModuleConfig: KeyError exception: %s." %ex.args[0]	)            
        except:
        
            if tran:tran.rollback()
            if conn:conn.close()
            raise

        #Continue for the rest.
        tran = conn.begin()
        try:
            #5 Deal with physics gruop
            phg = dataset['physics_group_name']
            if phg:
                #Yes, the dataset has physica group.
                phgId = self.phygrpid.execute(conn, phg, transaction=tran)
                if phgId <= 0 :
                    #not in db yet, insert it
                    phgId = self.sm.increment(conn, "SEQ_PG")
                    phygrp = {'physics_group_id':phgId, 'physics_group_name':phg}
                    try:
                        self.phygrpin.execute(conn, phygrp, tran)
                    except exceptions.IntegrityError as ex:
                        if str(ex).find("ORA-00001") != -1 and str(ex).find("PK_PG") != -1:
                            if tran:tran.rollback()
                            if conn:conn.close()
                            dbsExceptionHandler(message='InsertPhysicsGroup Error', logger=self.logger.exception, serverError="InsertPhysicsGroup: "+ str(ex))
                        if (str(ex).find("ORA-00001") != -1 and str(ex).find("TUC_PG_PHYSICS_GROUP_NAME") != -1) or\
                            str(ex).lower().find("duplicate") != -1:
                            phgId = self.phygrpid.execute(conn, phg, transaction=tran)
                            if phgId <= 0:
                                if tran:tran.rollback()
                                if conn:conn.close()
                                dbsExceptionHandler(message='InsertPhysicsGroup Error ', logger=self.logger.exception, serverError="InsertPhysicsGroup: "+str(ex))
                        elif str(ex).find("ORA-01400") > -1:
                            if tran:tran.rollback()
                            if conn:conn.close()
                            dbsExceptionHandler('dbsException-missing-data',
                                'Missing data when insert Physics_groups. ',
                                self.logger.exception,
                                'Missing data when insert Physics_groups. '+ str(ex))
                        else:
                            if tran: tran.rollback()
                            if conn: conn.close()
                            dbsExceptionHandler('dbsException-invalid-input2',
                            'Invalid data when insert Physics_groups. ',
                            self.logger.exception,
                            'Invalid data when insert Physics_groups. '+ str(ex))                        
                    except Exception as ex:
                        if tran:tran.rollback()
                        if conn:conn.close()
                        raise
                dataset['physics_group_id'] = phgId
                #self.logger.debug("***PHYSICS_GROUP_ID=%s***" %phgId)
            else:
                #no physics gruop for the dataset.
                dataset['physics_group_id'] = None
            del dataset['physics_group_name']
            #6 Deal with Data tier. A dataset must has a data tier
            dataset['data_tier_name'] = dataset['data_tier_name'].upper()
            #We no longer handle Tier inside dataset insert. If a data tier is no in DBS before the dataset 
            # is inserted. We will report error back to the user as missing data. See github issue #466.
            # This is to prevent users to insert random data tiers into phys* DB. YG May-15-2015
            dtId = 0
            dtId = self.tierid.execute(conn, dataset['data_tier_name'])   
            #When no data tier found, it return tier id -1
            if dtId <= 0:
                dbsExceptionHandler('dbsException-missing-data', 'Required data tier %s not found in DBS when insert dataset. Ask your admin adding the tier before insert/migrate the block/dataset.' %dataset['data_tier_name'],
                         self.logger.exception, 'Required data tier not found in DBS when insert dataset. ')
            #7 Deal with dataset access type. A dataset must have a data type
            dataset['dataset_access_type'] = dataset['dataset_access_type'].upper()
            #handle dataset access type inside dataset insertion with Inser2.
            tran.commit()
        except Exception as ex:
            if tran:tran.rollback()
            if conn:conn.close()
            raise
        #Before we insert the dataset, we need to make sure dataset=/primary_dataset_name/processed_dataset_name/data_tier 
        d2 = dataset['dataset'].rsplit('/')
        if (d2[1] != primary_ds_name or d2[2] != dataset["processed_ds_name"] or d2[3] != dataset['data_tier_name']):
            if tran:tran.rollback()
            if conn:conn.close()
            dbsExceptionHandler('dbsException-invalid-input2', 
                                'dataset=/primary_dataset_name/processed_dataset_name/data_tier is not matched.',
                                self.logger.exception, 'dataset=/primary_dataset_name/processed_dataset_name/data_tier is not matched.')      
        try:
            #self.logger.debug("*** Trying to insert the dataset***")
            dataset['dataset_id'] = self.insertDatasetWOannex(dataset = dataset,
                                           blockcontent = blockcontent,
                                           otptIdList = otptIdList,
                                           conn = conn, insertDataset = True, migration=migration)
        finally:
            if tran:tran.rollback()
            if conn:conn.close()

        return dataset['dataset_id']
1194309
def _saveDB(self):
        
        # We're a "fresh" copy now
        self._updated = time.time()
        if self._new:
            operation = 'INSERT'
            if not self._validID():
                self._setID(self._nextSequence())
            # Note that we assign this ID to our self
            # BEFORE possibly saving any of our attribute
            # objects that might be new as well. This means
            # that they might have references to us, as long
            # as the database does not require our existence
            # yet.
            #
            # Since mysql does not have Sequences, this will
            # not work as smoothly there. See class
            # MysqlForgetter below.
        else:
            operation = 'UPDATE'
        (sql, fields) = self._prepareSQL(operation)
        values = []
        for field in fields:
            value = getattr(self, field)
            # First some dirty datatype hacks
            if DateTime and type(value) == DateTime.DateTimeType:
                # stupid psycopg does not support it's own return type..
                # lovely..
                value = str(value)
            if DateTime and type(value) == DateTime.DateTimeDeltaType:
                # Format delta as days, hours, minutes seconds
                # NOTE: includes value.second directly to get the
                # whole floating number
                value = value.strftime("%d %H:%M:") + str(value.second)
            if value is True or value is False:
                # We must store booleans as 't' and 'f' ...
                value = value and 't' or 'f'
            if isinstance(value, Forgetter):
                # It's another object, we store only the ID
                if value._new:
                    # It's a new object too, it must be saved!
                    value.save()
                try:
                    (value,) = value._getID()
                except:
                    raise "Unsupported: Can't reference multiple-primary-key: %s" % value
            values.append(value)
        cursor = self.cursor()
        cursor.execute(sql, values)
        # cursor.commit()
        cursor.close()
        self._new = False
        self._changed = None
900353
def save_object_to_db(self,
                          obj: Any,
                          table: str,
                          fieldlist: Sequence[str],
                          is_new_record: bool) -> None:
        """"""
        if is_new_record:
            pkvalue = getattr(obj, fieldlist[0])
            if pkvalue is None:
                self.insert_object_into_db_pk_unknown(obj, table, fieldlist)
            else:
                self.insert_object_into_db_pk_known(obj, table, fieldlist)
        else:
            self.update_object_in_db(obj, table, fieldlist)
759296
def populateFromRow(self, row):
        
        self._dbFilePath = row[b'dataUrl']
        self._db = SqliteRnaBackend(self._dbFilePath)
        self.getRnaQuantMetadata()