Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - MySQL query

Reply
 
Old 08-28-2009   #1 (permalink)
James


 
 

MySQL query

This simple script connects to a MySQL database and displays the results
of a query. So far it will only display the first record from the
query. If the query returns more than one record only the first record
is displayed. I'd like to save the entire results of the query to a
file as well as diplay them on screen. Is this possible? I quoted out
the If.. Else block because the script would hang.

DIM objConn, strConnection, objRS, strQuery
Set objConn = CreateObject("ADODB.Connection")

strConnection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=192.168.0.1;
PORT=3306;" &_
"DATABASE=mydb; USER=user; PASSWORD=password; OPTION=3;"
objConn.open strConnection
set objRS = CreateObject("ADODB.RecordSet")
set objRS.ActiveConnection = objConn

strQuery = "select login,password from users where companyid=105"

objRS.Open strQuery
'If objRS.EOF Then
'Wscript.Echo "Record cannot be found."
'Else
'Do until objRS.EOF
wscript.echo objRS("login") & " " & objRS("password")
'loop
'End if

objRS.close
Set objRS = Nothing
objConn.close
Set objConn = Nothing

My System SpecsSystem Spec
Old 08-28-2009   #2 (permalink)
ekkehard.horner


 
 

Re: MySQL query

James schrieb:
Quote:

> This simple script connects to a MySQL database and displays the results
> of a query. So far it will only display the first record from the
> query. If the query returns more than one record only the first record
> is displayed. I'd like to save the entire results of the query to a
> file as well as diplay them on screen. Is this possible? I quoted out
> the If.. Else block because the script would hang.
>
> DIM objConn, strConnection, objRS, strQuery
> Set objConn = CreateObject("ADODB.Connection")
>
> strConnection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=192.168.0.1;
> PORT=3306;" &_
> "DATABASE=mydb; USER=user; PASSWORD=password; OPTION=3;"
> objConn.open strConnection
> set objRS = CreateObject("ADODB.RecordSet")
> set objRS.ActiveConnection = objConn
>
> strQuery = "select login,password from users where companyid=105"
>
> objRS.Open strQuery
> 'If objRS.EOF Then
> 'Wscript.Echo "Record cannot be found."
> 'Else
> 'Do until objRS.EOF
> wscript.echo objRS("login") & " " & objRS("password")
you forgot:
objRS.MoveNext
Quote:

> 'loop
> 'End if
>
> objRS.close
> Set objRS = Nothing
> objConn.close
> Set objConn = Nothing
My System SpecsSystem Spec
Old 08-28-2009   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: MySQL query


"James" <jwanders@xxxxxx> wrote in message
news:53Klm.185171$vp.35604@xxxxxx
Quote:

> This simple script connects to a MySQL database and displays the results
> of a query. So far it will only display the first record from the query.
> If the query returns more than one record only the first record is
> displayed. I'd like to save the entire results of the query to a file as
> well as diplay them on screen. Is this possible? I quoted out the If..
> Else block because the script would hang.
>
> DIM objConn, strConnection, objRS, strQuery
> Set objConn = CreateObject("ADODB.Connection")
>
> strConnection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=192.168.0.1;
> PORT=3306;" &_
> "DATABASE=mydb; USER=user; PASSWORD=password; OPTION=3;"
> objConn.open strConnection
> set objRS = CreateObject("ADODB.RecordSet")
> set objRS.ActiveConnection = objConn
>
> strQuery = "select login,password from users where companyid=105"
>
> objRS.Open strQuery
> 'If objRS.EOF Then
> 'Wscript.Echo "Record cannot be found."
> 'Else
> 'Do until objRS.EOF
> wscript.echo objRS("login") & " " & objRS("password")
> 'loop
> 'End if
>
> objRS.close
> Set objRS = Nothing
> objConn.close
> Set objConn = Nothing
Your script hung because the Do Until loop never advances, and so goes
forever. You need to add a MoveNext statement in the loop. For example:
=======
If objRS.EOF Then
Wscript.Echo "Record cannot be found."
Else
Do until objRS.EOF
wscript.echo objRS("login") & " " & objRS("password")
objRS.MoveNext
loop
End if
=========
Even if you expect only one record in the recordset, the EOF condition will
never result if you don't move to the next record.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
execute mysql query using vbscript VB Script
mySQL on Vista x64 Software
i cannot connect to my odcb driver, mysql .. need query string PowerShell
Vista doesnt like MySQL Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46