Faithfulman
(Getting the hang of it)
2005-02-22 11:48 PM
ODBC - DBcommand

$ConnDSN = 'ncivesConn'
$sql = "SELECT workst.wSid FROM workst WHERE workst.pcName = '@wksta'"


$recordset = DBCommand($dsn,$sql)
; ? 'Error = '+@ERROR+' - '+@SERROR
sleep 3
for $row=0 to ubound($recordset,1)
for $column=0 to ubound($recordset,2)
? 'Field(row='+$row+', column='+$column+') ='+$recordset[$row,$column]
next
next


This doesn't seem to work. I am just trying to have the Kix Script query for one single variable. There will never ever be more than one variable. What am I doing wrong.??
Help!!


Sealeopard
(KiX Master)
2005-02-23 12:37 AM
Re: ODBC - DBcommand

What's the error code? Is the DSN correct? Haven't seen one of these so far? What's the backend database?

Faithfulman
(Getting the hang of it)
2005-02-23 12:53 AM
Re: ODBC - DBcommand

I get an error 87 - The Parameter is incorrect.

I am wondering if it is not even attempting to connect?
I'm using MySQL ... and I have the ODBC settings already setup and I've been using it with Cyrstal Reports, so I know it works.

Here is my script:

Code:
   
Dim $pcn

SetConsole ("SHOW")

Color w+/n

;$pcn=@wksta

? "Your PC name is " + "@wksta"

sleep 2







function DBCommand($ConnDSN,$sql)
Dim $objConn, $adStateOpen
dim $Conntimeout, $CmdTimeout
dim $cmdCommand, $rsRecordset
dim $Records, $FinalRecords
dim $adCmdText, $adLockReadOnly, $adOpenStatic
dim $row, $rows, $column, $columns

$ConnDSN=trim($ConnDSN)
if not $ConnDSN
exit 87
endif
$sql=trim($sql)
if not $sql
exit 87
endif

; default database parameters
$adStateOpen=1
$ConnTimeout=15
$CmdTimeout=30
$adCmdText = 1
$adOpenStatic = 3
$adLockReadOnly = 1

; open the database connection
$objConn = CreateObject("ADODB.Connection")
if @ERROR
exit @ERROR
endif
$objConn.ConnectionTimeout = $ConnTimeout
if @ERROR
exit @ERROR
endif
$objConn.CommandTimeout = $CmdTimeout
if @ERROR
exit @ERROR
endif
$objConn.Open($ConnDSN)
if @ERROR
exit @ERROR
endif
if not $objConn.State=$adStateOpen
$objConn=''
$DBCommand=''
exit @ERROR
endif

; create the database command object
$cmdCommand = CreateObject('ADODB.Command')
if @ERROR
exit @ERROR
endif
$cmdCommand.ActiveConnection = $objConn
if @ERROR
exit @ERROR
endif
$cmdCommand.CommandType = $adCmdText
if @ERROR
exit @ERROR
endif
$cmdCommand.CommandText = $sql
if @ERROR
$DBCommand=@ERROR
exit @ERROR
endif

if instr($sql,'SELECT')=1
; create the recordset object
$rsRecordSet = CreateObject('ADODB.Recordset')
if @ERROR
exit @ERROR
endif
$rsRecordset.CursorType = $adOpenStatic
if @ERROR
exit @ERROR
endif
$rsRecordset.LockType = $adLockReadOnly
if @ERROR
exit @ERROR
endif
$rsRecordset.Open($cmdCommand)
if @ERROR
exit @ERROR
endif

if $rsRecordset.EOF and $rsRecordSet.BOF
; recordset is empty
$FinalRecords=''
else
if @ERROR
exit @ERROR
endif

; retrieve all records at once and transpose into tabular format
$Records = $rsRecordset.GetRows()
$columns=ubound($records,1)
$rows=ubound($records,2)
redim $FinalRecords[$rows,$columns]
for $row=0 to $rows
for $column=0 to $columns
$FinalRecords[$row,$column]=$records[$column,$row]
next
next
endif

; close recordset
if $rsRecordset.state=$adStateOpen
$rsRecordset.Close()
if @ERROR
exit @ERROR
endif
endif

$rsRecordset=''
$cmdCommand=''

$DBCommand=$FinalRecords
else
$rsRecordset=$cmdCommand.Execute()
$cmdCommand=''
$rsRecordset=''
if @ERROR
exit @ERROR
endif

$DBCommand=0
endif

; close the database connection
If $objConn.State = $adStateOpen
$objConn.Close()
if @ERROR
exit @ERROR
endif
EndIf
$objConn=''

exit 0
endfunction




$ConnDSN = "ncivesConn"
$sql = "SELECT workst.wSid FROM workst WHERE workst.pcName = '@wksta'"


$recordset = DBCommand($dsn,$sql)

? 'Error = '+@ERROR+' - '+@SERROR

? $recordset
; sleep 3

for $row=0 to ubound($recordset,1)
for $column=0 to ubound($recordset,2)
'Field(row='+$row+', column='+$column+') ='+$recordset[$row,$column]
next
next



? "Your workstation ID number is " + $Records

sleep 3

SetConsole ("Hide")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:



Faithfulman
(Getting the hang of it)
2005-02-23 05:40 PM
Re: ODBC - DBcommand

Does anyone have any advice on this? Maybe someone can help me make a basic working script? Thanks,

Faithful


ShawnAdministrator
(KiX Supporter)
2005-02-23 06:04 PM
Re: ODBC - DBcommand

Right off the bat, i see a disconnect between your DSN here:

$ConnDSN = "ncivesConn"

and they way you use it, here:

$recordset = DBCommand($dsn,$sql)

should (at least) be trying $ConnDSN instead of $dsn ?


Faithfulman
(Getting the hang of it)
2005-02-23 09:20 PM
Re: ODBC - DBcommand

Wow ... I cant believe I forgot that.

But I still get an error. It says the following:

"his context.) [-2147352567/80020009]eration. It is either closed or invalid in t"


What does this mean??

Thanks,

Faithful


Stanley
(Starting to like KiXtart)
2005-02-23 09:48 PM
Re: ODBC - DBcommand

I know very little about your database connection issue but,
Including
Code:
  
$ = SetOption("WrapAtEOL","ON")


At the beginning of your script may give you a more readable error message.



ShawnAdministrator
(KiX Supporter)
2005-02-23 10:24 PM
Re: ODBC - DBcommand

ja for sure - put in Stanley's suggestion and lets see the full text of that error message.

-Shawn


Faithfulman
(Getting the hang of it)
2005-02-23 10:31 PM
Re: ODBC - DBcommand

Sorry about that ... here is the full error message:

Error = 2147352567 COM exception error "Open" (ADODB.Recordset - The connection cannot be used to perform this operation. It is either closed or invalid in this context.) [-2147352567/80020009]


Stanley
(Starting to like KiXtart)
2005-02-23 10:45 PM
Re: ODBC - DBcommand

Shouldn't this
Code:
 

$sql = "SELECT workst.wSid FROM workst WHERE workst.pcName = '@wksta'"


be this ?
Code:
 

$sql = "SELECT workst.wSid FROM workst WHERE workst.pcName = "+@wksta



Or does it need to be quoted ?

Code:
 

$sql = "SELECT workst.wSid FROM workst WHERE workst.pcName = "+CHR(34)+@wksta+CHR(34)





**DONOTDELETE**
(Lurker)
2005-02-23 11:02 PM
Re: ODBC - DBcommand

No, the SQL statement is correct. I tried both of your solutions and it gave me the same result.

I am printing out the SQL statement and testing it in PHPMYADMIN and it works fine in there. It has gotta to be something else.

HELP?


Faithfulman
(Getting the hang of it)
2005-02-23 11:05 PM
Re: ODBC - DBcommand

Sorry, ... that was me, I just forgot to sign back in.

ShawnAdministrator
(KiX Supporter)
2005-02-23 11:14 PM
Re: ODBC - DBcommand

Can you try hand-jobbing the DSN as a test, format should be something like:

$dsn = "Provider=MSDASQL;Driver={SQL Server};Server=myServerName; Database=myDatabaseName;Uid=myUsername;Pwd=myPassword"

Something like that - your mileage will vary.

-Shawn



Faithfulman
(Getting the hang of it)
2005-02-23 11:20 PM
Re: ODBC - DBcommand

Is there any reason it would be different because I am using a MYSQL server?



ShawnAdministrator
(KiX Supporter)
2005-02-23 11:33 PM
Re: ODBC - DBcommand

woops, ja - should be :

$dsn = "Provider=MySQLProv;Data Source=mySQLDB;User Id=myUsername;Password=myPassword"

-Shawn


Faithfulman
(Getting the hang of it)
2005-02-23 11:36 PM
Re: ODBC - DBcommand

Well, at least I know it's trying to work .... If I put in a bad password it comes up with "Access denied for user..."

But when I put the right password in. .... I still get the same error as before:

COM exception error "OPEN" .......

Any other ideas??


ShawnAdministrator
(KiX Supporter)
2005-02-23 11:38 PM
Re: ODBC - DBcommand

Lets see all your code again (no need to show DBCommand again)

Faithfulman
(Getting the hang of it)
2005-02-23 11:45 PM
Re: ODBC - DBcommand

Here you go

Code:
 

$ = SetOption("WrapAtEOL","ON")
Dim $pcn

SetConsole ("SHOW")

Color w+/n

;$pcn=@wksta

? "Your PC name is " + "@wksta"

sleep 2


;$ConnDSN = "ncivesConn"


$ConnDSN = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=ncives;USER=Ref;PASSWORD=Ref;OPTION=3;"




$sql = "SELECT * FROM workst;"
;$sql = "SELECT workst.wSid FROM workst WHERE workst.pcName = '@wksta'"

? $sql
sleep 3

$recordset = DBCommand($ConnDSN,$sql)

? 'Error = '+@ERROR+' - '+@SERROR


sleep 1

for $row=0 to ubound($recordset,1)
for $column=0 to ubound($recordset,2)
? 'Field(row='+$row+', column='+$column+') ='+$recordset[$row,$column]
next
next



? "Your workstation ID number is " + $Records



<<>>




sleep 5

SetConsole ("Hide")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:




ShawnAdministrator
(KiX Supporter)
2005-02-23 11:58 PM
Re: ODBC - DBcommand

ok, I just got this to return success on my machine, what yours say ?

Code:

break on

$= SetOption("WrapAtEol", "On")

$connection = createobject("adodb.connection")

$ConnDSN = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=phone;USER=root;PASSWORD=********;";OPTION=3;"

$= $connection.open($connDSN)

? @SERROR



hmmm - i took the option=3 off though

[edit]

option=3 works for me too




ShawnAdministrator
(KiX Supporter)
2005-02-24 12:01 AM
Re: ODBC - DBcommand

I didn't post full code, see above

Faithfulman
(Getting the hang of it)
2005-02-24 12:10 AM
Re: ODBC - DBcommand

Just your code by itself get the result of:

"Operation completed succesfully"

But when I put my code back in I still get the error message from before.??


ShawnAdministrator
(KiX Supporter)
2005-02-24 12:18 AM
Re: ODBC - DBcommand

Now add this:

$rs = $connection.execute("SELECT workst.wSid FROM workst WHERE workst.pcName = '" + @WKSTA + "'")

? @SERROR


watch for those double/single quotes !


Faithfulman
(Getting the hang of it)
2005-02-24 12:19 AM
Re: ODBC - DBcommand

I have put the following:


Code:
 

$ConnDSN = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=ncives;USER=test;PASSWORD=test;OPTION=3;"


$sql = 'SELECT * FROM workst'
? $sql
sleep 3


$recordset = DBCommand($ConnDSN,$sql)

$connection = createobject("adodb.connection")
?$recordset

$= $connection.open($ConnDSN)

? @SERROR




? 'Error = '+@ERROR+' - '+@SERROR
sleep 1


for $row=0 to ubound($recordset,1)
for $column=0 to ubound($recordset,2)
? 'Field(row='+$row+', column='+$column+') ='+$recordset[$row,$column]
next
next


? "Your workstation ID number is " + $Recordset




And it appears to NOT be erroring out. It says completed sucessfully in two places. ... but never shows any data.
I'm confused!


ShawnAdministrator
(KiX Supporter)
2005-02-24 12:19 AM
Re: ODBC - DBcommand

see above

Faithfulman
(Getting the hang of it)
2005-02-24 12:26 AM
Re: ODBC - DBcommand

Now I get this error:

Script Error: IDispatch pointers not allowed in exrpession.


ShawnAdministrator
(KiX Supporter)
2005-02-24 12:31 AM
Re: ODBC - DBcommand

Sounds like a typo somewhere, I would just put these debugging statements in a separate script and run it by itself - we're just trying to narrow down where the problem is, getting close - you shouldn't be getting a syntax error.

Sealeopard
(KiX Master)
2005-02-24 12:46 AM
Re: ODBC - DBcommand

You cannot just print out an array as in Code:

? $Recordset


Alos, why do you create a separate recordset?
Try this:
Code:

$ConnDSN = 'DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=ncives;USER=test;PASSWORD=test;OPTION=3;'

$sql = 'SELECT * FROM workst'
? 'SQL = '+$sql

$recordset = DBCommand($ConnDSN,$sql)
? 'Error '+@ERROR+' - '+@SERROR

for $row=0 to ubound($recordset,1)
for $column=0 to ubound($recordset,2)
? 'Field(row='+$row+', column='+$column+') ='+$recordset[$row,$column]
next
next


Finally, sprinkle the UDF itself with error checks and step through it via DEBUG ON to see why it fails.


Faithfulman
(Getting the hang of it)
2005-02-24 06:58 PM
Re: ODBC - DBcommand

Sealepoard ....

Running Through DEBUG ON .... the error shows up when it gets to this:

? 'Error '+@ERROR+' - '+@SERROR


And It is still the same error:

Error = 2147352567 COM exception error "Open" (ADODB.Recordset - The connection cannot be used to perform this operation. It is either closed or invalid in this context.) [-2147352567/80020009]


Faithfulman
(Getting the hang of it)
2005-02-24 07:11 PM
Re: ODBC - DBcommand

Shawn,

When I remove all of my code out and replace it with just the code you have shown ... it says Operation complete twice ....
and then ... Press any key to conitue.

But still doesn't show any data.

Any other ideas??


ShawnAdministrator
(KiX Supporter)
2005-02-24 07:37 PM
Re: ODBC - DBcommand

ok, then lets just see if we can get the results of your query ... heres some new code, change the details as required ... then in the rs.EOF loop, try to display-out one of your field names ... this line here:

?"Name=" $rs.Fields("Name").Value


Change it to display one of your fields. Its good to dig down to this level because it gives an appreciation for how this stuff works under-the-covers (imho) ...

Code:

break on

$= SetOption("WrapAtEol", "On")

$connection = createobject("adodb.connection")

$ConnDSN = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=phone;USER=root;PASSWORD=******;";OPTION=3;"

$= $connection.open($connDSN)

$rs = $connection.execute("SELECT Name FROM Business WHERE Name = 'Shawn'")

While Not $rs.EOF

?"Name=" $rs.Fields("Name").Value

$rs.MoveNext

Loop

exit 1



If you dont see any values, try putting in an @ERROR to catch the error. If it does, we can move on ...


Faithfulman
(Getting the hang of it)
2005-02-24 07:52 PM
Re: ODBC - DBcommand

Boo ya! I got results!!

Cool .... I just added back in the rest of my code and it works fine ... So why did the UDF not work?? Is it buggy?

Thanks so much for your help!!!

Faithful


ShawnAdministrator
(KiX Supporter)
2005-02-24 08:06 PM
Re: ODBC - DBcommand

tbh idk - maybe Jens can fill in the blanks, the only other thing you can try and verify is the GetRows, that returns all the data into a really funky (COM) variant array, and then you parse it yourself (DBCommand parses it into a 2dim array) ... or you can use this $rs.EOF/MOVENEXT thingy to step through the data yourself - but the GetRows() is faster (imho)

The benefit of using EOF/MOVENEXT is that you can change the data model, shift stuff around - and maybe be not so code impacted by that (because your refering to fields through the Fields collection, by name) and not by a hardcoded array index ... your call ...

You can try GetRows like this (after your Execute):

$rows = $rs.getrows()

?"vartype= " + vartypename($rows) + " error=" @SERROR

-Shawn


Faithfulman
(Getting the hang of it)
2005-02-24 08:10 PM
Re: ODBC - DBcommand

I think that the code you showed will be fine as is. Thanks again so much.

Faithful


Sealeopard
(KiX Master)
2005-02-25 04:23 AM
Re: ODBC - DBcommand

Try to comment out sectionCode:

$rsRecordset.LockType = $adLockReadOnly
if @ERROR
exit @ERROR
endif

and try again. Also, for the error checks we'll need to know the line executed before the error lone triggered.

the function has been confirmed to work with MS Access and MS SQL Server, I don't have MySQL to test with.


ShawnAdministrator
(KiX Supporter)
2005-02-25 04:32 AM
Re: ODBC - DBcommand

Jens, thought I could help-out by doing a bit of debugging with my mysql, but this syntax works perfectly for me:

Code:

break on

$dsn = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=phone;USER=root;PASSWORD=******;option=3"
$sql = "SELECT Name FROM Business WHERE Name = 'Shawn'"

$rows = DBCommand($dsn, $sql)

? @SERROR

?"VarTypeName=" VarTypeName($rows)

exit 0



Get this:

Quote:


E:\>kix32 t

The operation completed successfully.
VarTypeName=Variant[]





So, this might be some kind of weird issue with his instance.

-Shawn