Jump to content

Pull data from MySQL database and generate report


Recommended Posts

Not sure how to do this:

I'm going to pull out data from MySQL database sorted on username and
save it to a variable, like this:

Local $data = ""
$data &= "user1 :: 1" & @CRLF
$data &= "user1 :: 0" & @CRLF
$data &= "user1 :: 1" & @CRLF
$data &= "user1 :: 0" & @CRLF
$data &= "user1 :: 1" & @CRLF
$data &= "user1 :: 1" & @CRLF
$data &= "user1 :: 1" & @CRLF
$data &= "user2 :: 1" & @CRLF
$data &= "user2 :: 0" & @CRLF
$data &= "user2 :: 1" & @CRLF
$data &= "user2 :: 0" & @CRLF
$data &= "user2 :: 1" & @CRLF
$data &= "user2 :: 0" & @CRLF
$data &= "user2 :: 1" & @CRLF
$data &= "user2 :: 1" & @CRLF
$data &= "user2 :: 1" & @CRLF
$data &= "user2 :: 1" & @CRLF
$data &= "user2 :: 0" & @CRLF
$data &= "user3 :: 1" & @CRLF
$data &= "user3 :: 1" & @CRLF
$data &= "user3 :: 1" & @CRLF
$data &= "user3 :: 1" & @CRLF
$data &= "user3 :: 0" & @CRLF

From above data, I would like my script to generate a report like this:

User1
Total record = 7
Percentage 1 versus 0 = 5 / 7 x 100% = 71.42%

User2
Total record = 11
Percentage 1 versus 0 = 7 / 11 x 100% = 63.63%

User3
Total record = 5
Percentage 1 versus 0 = 4 / 5 x 100% = 80.00%

Of course "user1", "user2", "user3" could be anything like "Bill", "Susan" and
not limited to 3 users.

I am even not sure should I pull the MySQL data and save it to a variable as a long string (like above)
or as an array, or should I use MySQL command to count total record for each user.

Totally lost on how to do it.

Link to comment
Share on other sites

Use SQL to grab information in arrays and AutoIt code to format it the way you want.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Use SQL to grab information in arrays and AutoIt code to format it the way you want.

 

I solved it this way. What are another ways that is faster, more efficient and shorter code?

#include <array.au3>

Local $sData = ""

$sData  = "user1 :: 1" & @CRLF
$sData &= "user1 :: 0" & @CRLF
$sData &= "user1 :: 1" & @CRLF
$sData &= "user1 :: 0" & @CRLF
$sData &= "user1 :: 1" & @CRLF
$sData &= "user1 :: 1" & @CRLF
$sData &= "user1 :: 1" & @CRLF
$sData &= "user1 :: 1" & @CRLF
$sData &= "user1 :: 1" & @CRLF
$sData &= "user1 :: 1" & @CRLF
$sData &= "user1 :: 1" & @CRLF
$sData &= "user2 :: 1" & @CRLF
$sData &= "user2 :: 0" & @CRLF
$sData &= "user2 :: 1" & @CRLF
$sData &= "user2 :: 0" & @CRLF
$sData &= "user2 :: 1" & @CRLF
$sData &= "user2 :: 0" & @CRLF
$sData &= "user2 :: 1" & @CRLF
$sData &= "user2 :: 1" & @CRLF
$sData &= "user2 :: 1" & @CRLF
$sData &= "user2 :: 1" & @CRLF
$sData &= "user2 :: 0" & @CRLF
$sData &= "user3 :: 1" & @CRLF
$sData &= "user3 :: 1" & @CRLF
$sData &= "user3 :: 1" & @CRLF
$sData &= "user3 :: 1" & @CRLF
$sData &= "user3 :: 0" & @CRLF

Local $aData = StringSplit($sData, @CRLF, 3)
Local $sSaveUser = "", $sOut = "", $i0, $i1 = 0

For $a = 0 To UBound($aData) - 1
    If $sSaveUser = StringLeft($aData[$a], StringInStr($aData[$a], ' ::') - 1) Then
        If StringMid($aData[$a], StringInStr($aData[$a], ':: ') + 3) = "1" Then
            $i1 += 1
        Else
            $i0 += 1
        EndIf
    Else
        If $a <> 0 Then $sOut &= $sSaveUser & @CRLF & "sum of 1 = " & $i1 & @CRLF & "sum of 0 = " & $i0 & @CRLF & @CRLF
        $sSaveUser = StringLeft($aData[$a], StringInStr($aData[$a], ' ::') - 1)
        $i0 = 0
        $i1 = 0
        If StringMid($aData[$a], StringInStr($aData[$a], ':: ') + 3) = "1" Then
            $i1 += 1
        Else
            $i0 += 1
        EndIf
    EndIf
    If StringLen($aData[$a]) = 0 Then ExitLoop
Next

ConsoleWrite($sOut)

The result is:

user1

sum of 1 = 9

sum of 0 = 2

user2

sum of 1 = 7

sum of 0 = 4

user3

sum of 1 = 4

sum of 0 = 1

 

Edited by michaelslamet
Link to comment
Share on other sites

What I said, but I'm not familiar enough with MySQL to provide code.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

What I said, but I'm not familiar enough with MySQL to provide code.

 

In fact, I solved it using your advice :)

Nevermind about the MySQL part. I wonder about the "AutoIT part". Although my code run just fine, I'm not confident it's the best way to do it.

That is why I'm asking for advices, suggestions, codes to tweak it.

So, please lead me to the light :)

Link to comment
Share on other sites

Post your code please

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

ms,

Your code runs 100,000 entries in 1.2 seconds.  Seems pretty fast to me :thumbsup:

 

Good to hear that :idiot:

As you might see, I learn from you from >this post and borrow the logic (and the vars!) :muttley:

Anyway, I always wonder, cant we shorten this code:

If StringMid($aData[$a], StringInStr($aData[$a], ':: ') + 3) = "1" Then
    $i1 += 1
Else
    $i0 += 1
EndIf

 

To only 1 line, something like:

If StringMid($aData[$a], StringInStr($aData[$a], ':: ') + 3) = "1" Then $i1 += 1 Else $i0 += 1 EndIf

 

It doesn't work with 3.8.8.1, but why? Why dont we make it run at 3.3.10 ? Because writing it that way is a bad practice?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...