How to return value from database in Spotfire? - spotfire

Do you have any ideas how value which was writeback from SF to database could be returned from data base again to Spotfire input field.
(No just any value, what is mechanism stay behind it?)
If I need to make changes in this record ?
We writeback to DB with IronPython scripting assigning document properties.
Piece of code....
sqlIns = "INSERT INTO SCHEMA.TABLE_NAME (FIELD, WELL, WELLBORE, WELLTYPE) \n\
VALUES \n\
('" + Document.Properties["IFIELD"] + "', \n\
'" + Document.Properties["IWell"] + "', \n\
'" + Document.Properties["IWellBore"] + "', \n\
'" + Document.Properties["IWellType"] + "')"
print sqlIns
dbsettings = DatabaseDataSourceSettings( "System.Data.OracleClient","Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=server)(PORT=****))(CONNECT_DATA=(SERVICE_NAME=****)));User Id=;Password=",sqlIns)
ds = DatabaseDataSource(dbsettings)
newDataTable = Document.Data.Tables.Add("temp",ds)
Document.Data.Tables.Remove(newDataTable)
The problem is that before updating into the database the record, this record needs somehow modify. I insert the data through input or drop-down field in the text area, then press the Submit button to insert it into the database. It recorded there and appeared in the table which I add on the same page in SF.
My thoughts that it should be workflow like: highlight record you want to change (it happened in a table connected to SF)
-> once you choose the record, it appears in the input screen (input screen - text area where you enter all these values for the first time or another input screen only for data modification)
-> make required changes
-> re-write/update data to DB (with replacement initial record.)

The python script just sends an sql command. That command can be changed to do an update instead of an insert.
Post an example of code that you are using for the insert and people can assist on how to change to an update statement.
You'll need to have a primary key or other identifier for the update statement.
If you "wrote back to a database" to read the data into a input Field or table you'd have to query the database (a normal query to pull data into spotfire)

Related

CopyFromRecordset from Access to Excel is out of Order

Very frustrating results from:
ws.Range(myExcelTable).CopyFromRecordset rs
' myExcelTable is a named table with assigned range
The pull for rs (the recordset, is a select all, doesn't matter if it is done this way or with field names, it is the same result)
cmdSQL2 = "SELECT * FROM " & myAccessTable
rs.Open cmdSQL2, conn
For an unknown reason it copies records into excel in this order (using pID to show ordering):
3-8
0-2
9-number of records
I have tried several range methods and the result is the same.
I will "hand bomb" them here to cut out the extra work I performed in building range strings.
ws.Range("S4").CopyFromRecordset rs
ws.Range("S4:BP258").CopyFromRecordset rs
This behavior does not appear if the table you are copying from Access has only one Field (column) and the Table in Excel has a single header (column), but it is appearing for me now in this multi-field table.
I feel this is happening when the recordset is actually created somehow and not on the "paste" or assignment side (Excel).
'*************************************************************************
Problem Solved by this SO Contributor (will leave here until it is placed as an answer):
https://stackoverflow.com/users/7296893/erik-a
Using ORDER BY in the sql sommand:
cmdSQL2 = "SELECT * FROM " & myAccessTable & " ORDER BY " & primaryKey
From immediate window (removes the variables so you can see the command better for those new to such things:
SELECT * FROM County_Directory ORDER BY [pID]
'*************************************************************************

Passing string result to query then export as csv

Good Afternoon,
I have an access query that contains a list of all my customers lets call that CUS
I have another query that has a list of ORDERS
I would like to write some VBS that cycles through the customer list and exports a csv file containing all orders that belong to that customer.
The vba would then move on to the next customer on the list and perform the same action.
Any help would be great.
Snippet of code below
almost there cant get the WHERE condition working it keeps displaying a popup for me to populate however the same string is feeding the msgbox fine here is a snippet below tht is within the loop
strcustcode = rs!OCUSTCODE
ordercount = rs!orders
TIMEFILE = Format$(Time, "HHMM")
MsgBox ([strcustcode] & " has " & [ordercount] & " orders")
StrSQL = "Select * From [24-ND_Cus] where [24-ND_Cus].[OCUSTCODE] = strcustcode "
Set qd = db.CreateQueryDef("tmpExport", StrSQL)
DoCmd.TransferText acExportDelim, , "tmpExport", "c:file.csv" db.QueryDefs.Delete "tmpExport" –
Don't use [ ] around VBA variables. Don't use parens for the MsgBox when you just want to give user a message. The parens make it a function that requires a response by user to set a variable.
MsgBox strcustcode & " has " & ordercount & " orders"
Concatenate the variable into the SQL statement. If OCUSTCODE is a text type field, use apostrophe delimiters for the parameter.
StrSQL = "Select * From [24-ND_Cus] Where [OCUSTCODE] = '" & strcustcode & "'"
I don't advise code that routinely modifies design and changing a query SQL statement is changing design. If the only change is filter criteria and a dynamic parameterized query won't work, I suggest a 'temp' table - table is permanent, data is temporary. Delete and write records to the table and export the table.

How do I construct a SELECT statement incorporating Excel cell values?

I am querying an external database with power query through Excel. What I am wanting to do is populate my where clause based off of cell values so for example my sql statement would read something like
Select *
From employees
where hiredate between Sheet1!A1 and Sheet2!A2
I tried the above syntax and it does not work, but I think it illustrates what I am wanting to achieve.
How can I pass parameters from my Excel sheet into my power query syntax?
EDIT
I tried following the blog.oraylis.de (not actual link, link is in comments), but I get the below error when I try to execute my statement
DataSource.Error ODBC:Error[4203] ERROR: column "start_p" does not exist;
I created a table named it parameters added headers & Values, and the full SQL I try to run is this:
let
startp_Param = Excel.CurrentWorkbook(){[Name="Parameter"]}[Content],
startp_Value = startp_Param {0}[Value],
endp_Param = Excel.CurrentWorkbook(){[Name="Parameter"]}[Content],
endp_Value = endp_Param {1}[Value],
Source = Odbc.Query("dsn=postgresql", "Select * FROM employees where hiredate BETWEEN startp_Value AND endp_Value")
in
Source
EDIT # 2
I altered my query to read like this
"Select *
FROM employees
where hiredate BETWEEN " & startp_Value & " AND " & endp_Value
However this now presents an error of:
"We cannot apply operator & to types Text and Number"
startp_Value and endp_Value are steps in Power Query, so you have to use & to add it to the string, like this:
Source = Odbc.Query("dsn=postgresql", "Select * FROM employees where hiredate BETWEEN " & startp_Value & " AND " & endp_Value
This doesn't handle sanitizating your SQL query, so you may want to do it to startp_Value and endp_Value (for example, by converting to a Date and then back to a Text).

Excel VBA query table properties

I have an odbc query table which is currently working, but I need to define the field selected form the database by a value in cell D18.
The following code should replace the command text in the query table properties but it doesn't work.
debug says "Subscript out of range". if i debug and run ?text in the immediate window then run this resulting text line in SQL it returns the correct value, so the sql statement is right. what have i got wrong in the vba syntax
Sub Accrual()
Text = "SELECT "
Text = Text & "BALANCE.CDTLED_" & Range("D11").Value - 1 & " "
Text = Text & "FROM x3sov.SOVEX.BALANCE BALANCE "
Text = Text & "WHERE (BALANCE.ACC_0='2109') AND (BALANCE.FCY_0='S01')"
Sheets("Control").QueryTables(1).CommandText = Text
End Sub
any help much appreciated
Since Excel introduced ListObjects (called Tables in the UI), you can no longer rely on QueryTables.Count to get to all the external data connections. If you don't have any QueryTables, then the external data connection goes through a ListObject.
Sheet1.ListObjects(1).QueryTable.CommandText = Text
Note that ListObject doesn't have a QueryTables property, but a QueryTable property because there's always only one.

Populate Drop Down List Box (DDLB) with two values in PowerBuilder

I've created a Drop Down List Box (DDLB) in my window (I'm using PowerBuilder 10.5). Once I would call my function, the DDLB would fill with all the different cities from my table. This is the code I've used:
FOR li_i=1 TO ii_br_red
ls_city = dw_city.GetItemString(li_i, 'city')
IF ddlb_city.FindItem(ls_city, 1) = -1 THEN
ddlb_city.AddItem(ls_city) END IF; NEXT
Next part of the code is in the ddlb "selectionchanged" event...
dw_city.SetFilter("city = '" + this.text + "'")
dw_city.Filter()
This works great, and after calling my function (via click on a command button) I'd get a list of all different cities in my table, ex.
Paris
London
New York
Washington
No town would be listed twice.
What I need to do now is add a country next to every city in my DDLB. So that after clicking my command button I would get this in my DDLB:
Paris (France)
London (GB)
New York (USA)
Washington (USA)
Any advice? Thanks in advance...
SECOND QUESTION, similar to this subject: I have an SQL code:
SELECT distinct name FROM table1;
This gives me 8 different names. What I want to do is fill another DDLB, ddlb_1 with these names, but this must occur on the open event of my program. This is what I've written in the open event of my program:
string ls_name
SELECT distinct name INTO :ls_name FROM tabel1;
ddlb_1.AddItem(ls_name)
But this only gives me the first name. I'm guessing I need some kind of count, but I just can't pull it off.
If you do not want to change the design of the program, and as you states that the country is in the same DW, you could hack the code a little to add the country to the ddlb (I suppose that the country is available on the same row of the dw):
String ls_country
FOR li_i=1 TO ii_br_red
ls_city = dw_city.GetItemString(li_i, 'city')
IF ddlb_city.FindItem(ls_city, 1) = -1 THEN
ls_country = dw_city.GetItemString(li_i, 'country')
ddlb_city.AddItem(ls_city + ' (' + ls_country + ')')
END IF
NEXT
A quick and dirty hack to get back the value in the event to filter the DW would be
int p
string ls_city
ls_city = this.text
p = pos(ls_city, '(')
if p > 0 then ls_city = left(ls_city, p - 2) //skip the "space + (country)" part
dw_city.SetFilter("city = '" + ls_city + "'")
dw_city.Filter()
But this kind of code is difficult to maintain and should be replaced by something else, as the processing of the city value is strongly coupled to its representation in the list.
A better solution would be a dropdowndatawindow, or (worse) an array of the cities names where the index of a city + country in the ddlb would correspond to the index of the bare city name suitable for filtering the DW
I think you should modify the "source" datawindow' select, and you should get the final result which you want, and you would only need to copy the datas from the datawindow to the ddlb. You should use distinct in the select something like this:
select distinct city + ' (' + country_code + ')' from cities_and_countries_table
of course you should replace the "city", "country_code" to the actual column name in your table as well the table-name. With this you will get every city only once and they will be already concatenated with the country code.
Br. Gábor
Does it really have to be DDLB? I would give the user a Single Line Edit for the city name and filter the DW as the user types.
To answer my own second question, this is how I've done it finally...
String ls_name
DECLARE xy CURSOR FOR
SELECT distinct name FROM table1;
OPEN xy;
FETCH xy INTO :ls_name;
do until sqlca.sqlcode <> 0
ddlb_1.AddItem(ls_name);
FETCH xyINTO :ls_name;
loop
CLOSE xy;
I'm new to PowerBuilder, But I just used that kind of scenario, however I used a DDW (Drop Down Data Window) Instead of a List Box On this case, you can display more than one column as soon as the DW gets the focus and you'd be able to dynamically populate the data. Give it a try. It worked for me, DW's are a pain in the neck when you're just starting (as in my case) but you can do a lot with it

Resources