Python equivalent to basic SDBC setTimeStamp

Good Morning,
I have been working on some code in basic and would like to convert to python. The specific stumbling block is the python equivalent for setTimeStamp.

insSql="INSERT INTO SAMPLE_HEADER (SAMPLE_ID,OPERATOR,SUBMITTER,FILE,LP_ANALYSIS_TIME,HP_ANALSYS_TIME,REPORT_TIME,REPORT_RANGE,ADV_CONTACT_ANGLE,REC_CONTACT_ANGLE,PENETROMETER_ID,PENETROMETER_MASS,SAMPLE_MASS,STEM_VOLUME_USED,SHOW_NEG_INT,CORRECTION_TYPE,HG_TEMP,ASSEMBLY_MASS,PENETROMETER_VOL) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
	
	stmt = dbConn.prepareStatement(insSql)
	
	stmt.setString(1,sampleId)
	stmt.setString(2,operator)
	stmt.setString(3,submitter)
	stmt.setString(4,file)
    stmt.setTimeStamp(5,lpAnalysisTime)
	stmt.setTimeStamp(6,hpAnalysisTime)
	stmt.setTimeStamp(7,reportTime)
	stmt.setString(8,reportRange)
	stmt.setInt(9,advContactAngle)
	stmt.setInt(10,recContactAngle)
	stmt.setString(11,penetroMeterId)
	stmt.setDouble(12,penetroMeterMass)
	stmt.setDouble(13,sampleMass)
	stmt.setInt(14,stemVolume)
	
	stmt.setBoolean(15,showNegInt)
	stmt.setString(16,correctionType)
	
	stmt.setDouble(17,HgTemp)
	
	stmt.setDouble(18,assemblyMass)
	

	stmt.setDouble(19,penetroMeterVol)

	stmt.executeUpdate()
	dbConn.commit()	
	stmt.clearparameters()
	stmt.Close()
	Set stmt=nothing

The API is the same in either language. Does this work with StarBasic but fails with Python? Did you double-check the actual data types of your columns and their order of appearance?

I will double check the code.

Check the database first, code second.

image

Yes, is the same.

from datetime import datetime
from com.sun.star.util import DateTime

def _date_to_struct(value):
    d = DateTime()
    d.Year = value.year
    d.Month = value.month
    d.Day = value.day
    d.Hours = value.hour
    d.Minutes = value.minute
    d.Seconds = value.second
    return d


def main():
    
    DB_NAME = 'db_test'
    now = datetime.now()
    sql = f'INSERT INTO "dates" VALUES(?, ?)'

    dbc = app.create_instance('com.sun.star.sdb.DatabaseContext')
    db = dbc.getByName(DB_NAME)
    con = db.getConnection('', '')

    cursor = con.prepareStatement(sql)
    cursor.setInt(1, 3)
    cursor.setTimestamp(2, _date_to_struct(now))
    cursor.executeUpdate()

    con.close()
    app.debug('Ok...')
    return

image

2 Likes

The never ending conflict between human minds and data types …

1 Like

I think I will mark this as the answer while still acknowledging the previous response as the one that helps my work!:+1::+1: