Hello,
Here is the structure of the embedded FireBird table:
CREATE TABLE TBL_TSTDATE
(
ID SMALLINT generated by default as identity primary key,
DA_DATE DATE
)
Here is the code i have used to test:
from __future__ import unicode_literals
import cx_Oracle
import uno
from datetime import datetime
from scriptforge import CreateScriptService
def getData():
__openDB()
bas = CreateScriptService("Basic")
stmt = fConnection.createStatement()
stmt.execute('delete from TBL_TSTDATE')
fConnection.commit()
stmt.close()
stmt=None
selectSysDateEpat ="select trunc(sysdate) DA_DATE from dual"
epatcursor.execute(selectSysDateEpat)
result= epatcursor.fetchone()
insSQL = "INSERT INTO TBL_TSTDATE (DA_DATE) VALUES (?)"
stmt = fConnection.prepareStatement(insSQL)
for DA_DATE in result:
print(DA_DATE)
print(type(DA_DATE))
stmt.setString(1, DA_DATE)
stmt.executeUpdate()
stmt.clearParameters()
def __openDB():
global fConnection
global epat
global epatcursor
XSCRIPTCONTEXT.getDocument().CurrentController.connect()
oDoc = XSCRIPTCONTEXT.getDocument()
db = oDoc.DataSource
fConnection = db.getConnection("","")
epat = cx_Oracle.connect(user='????',password='????',dsn='????')
epatcursor = epat.cursor()
def __closeDb():
epatcursor.close()
epat.close()
fConnection.close()
epatcursor= None
epat=None
fConnection=None
The result of this code is the following printout:
>>> 2022-04-30 00:00:00
<class 'datetime.datetime'>
With the following popup error messge:
Couldn't convert 2022-04-30 00:00:00 to a UNO type; caught exception: <class 'AttributeError'>: 'datetime.datetime' object has no attribute 'getTypes', traceback follows
no traceback available
(Error during invoking function getData in module vnd.sun.star.tdoc:/725167500/Scripts/python/LibraryOpwpVEppr/LibraryTblTstDate/ModuleTblTstDate.py (<class 'uno.com.sun.star.uno.RuntimeException'>: Couldn't convert 2022-04-30 00:00:00 to a UNO type; caught exception: <class 'AttributeError'>: 'datetime.datetime' object has no attribute 'getTypes', traceback follows
no traceback available
I then made modification to the code. The code in full with modifications is:
from __future__ import unicode_literals
import cx_Oracle
import uno
from datetime import datetime
from scriptforge import CreateScriptService
def getData():
__openDB()
bas = CreateScriptService("Basic")
stmt = fConnection.createStatement()
stmt.execute('delete from TBL_TSTDATE')
fConnection.commit()
stmt.close()
stmt=None
selectSysDateEpat ="select trunc(sysdate) DA_DATE from dual"
epatcursor.execute(selectSysDateEpat)
result= epatcursor.fetchone()
insSQL = "INSERT INTO TBL_TSTDATE (DA_DATE) VALUES (?)"
stmt = fConnection.prepareStatement(insSQL)
for DA_DATE in result:
sDateTable =DA_DATE.strftime("%Y-%m-%d")
print(sDateTable)
print(type(sDateTable))
stmt.setDate(1, sDateTable)
stmt.executeUpdate()
stmt.clearParameters()
def __openDB():
global fConnection
global epat
global epatcursor
XSCRIPTCONTEXT.getDocument().CurrentController.connect()
oDoc = XSCRIPTCONTEXT.getDocument()
db = oDoc.DataSource
fConnection = db.getConnection("","")
epat = cx_Oracle.connect(user='????',password='????',dsn='????')
epatcursor = epat.cursor()
def __closeDb():
epatcursor.close()
epat.close()
fConnection.close()
epatcursor= None
epat=None
fConnection=None
The result of this code is the following printout:
>>> 2022-04-30
<class 'str'>
With the following popup error message:
value is not of same or derived type! (Error during invoking function getData in module vnd.sun.star.tdoc:/725167500/Scripts/python/LibraryOpwpVEppr/LibraryTblTstDate/ModuleTblTstDate.py (<class 'ooo_script_framework.com.sun.star.script.CannotConvertException'>: value is not of same or derived type!
File "D:\Program Files\LibreOffice\App\libreoffice\program\pythonscript.py", line 915, in invoke
ret = self.func( *args )
File "vnd.sun.star.tdoc:/725167500/Scripts/python/LibraryOpwpVEppr/LibraryTblTstDate/ModuleTblTstDate.py", line 25, in getData
))
I modified the insert SQL to be insSQL = "INSERT INTO TBL_TSTDATE (DA_DATE) VALUES (cast (? as date))"
with the set value to be both stmt.setDate(1, sDateTable)
as well as stmt.setString(1, sDateTable)
and nothing worked.
Thank you for your suggestions so far.