MISSION AND VISION 2012


On this Blog you can gain and collect some ideas, information about different kinds of topics about computer, tutorial and tips that can be applied. You can share your ideas too thru posting comment and feedback to said topic.

Thank You :)

Note : If the Link(s) doesn't work/broken please Comment.

(If this site helps you, please Comment and click the link to say Thanks)
Please Click here to Say Thank You :)



Showing posts with label MYSQL. Show all posts
Showing posts with label MYSQL. Show all posts

Friday, June 21, 2013

Get Values and Subtotals in One Shot

If you need to show values with totals and subtotals in a report, you could make two separate queries. But it is safer and quicker to do both in one go.
You might need to produce a report that shows values and calculates subtotals. In Figure, the subtotals are listed at the end of each group.

Table values and subtotals
itemserialNumberprice
Awl110
Awl310
Awlsubtotal20
Bowl210
Bowl510
Bowl610
Bowlsubtotal30
Cowl410
Cowlsubtotal10

For a simple query like this, the time cost of the round trip to the database server may dominate the cost of the query itself. So, making two trips to the database is a lot like going to the bar to buy a drink, walking away, and then going back to the bar for your change.
More than one trip to the database also introduces the possibility that someone else has added a row between your visits. If that happens, your total could be wrong.
A simple UNION will add the subtotals to your result:
mysql> SELECT item, serialNumber, price FROM source
    -> UNION
    -> SELECT item, NULL,         SUM(price)
    ->   FROM source
    ->  GROUP BY item
    -> ORDER BY item;
+------+--------------+-------+
| item | serialNumber | price |
+------+--------------+-------+
| Awl  |         NULL |    20 |
| Awl  |            1 |    10 |
| Awl  |            3 |    10 |
| Bowl |         NULL |    30 |
| Bowl |            2 |    10 |
| Bowl |            5 |    10 |
| Bowl |            6 |    10 |
| Cowl |         NULL |    10 |
| Cowl |            4 |    10 |
+------+--------------+-------+

The database server still has roughly the same amount of work to do as before, but the overall performance should be improved because you have saved the cost of sending a query and getting the results back.
The query works fine. The only slight problem is that subtotals show up as the first row for each item (depending on the SQL implementation you are using). Traditionally, the subtotals show up as the last item in each run.
You can change the ORDER BY expression to c, COALESCE(seq, 1E9). This will ensure that the NULL values in the serialNumber column show up last (1E9 is the number 1,000,000,000). You can have the UNION as a derived table to make it clear that the ORDER BY applies to the whole UNION:
mysql> SELECT item, serialNumber, price FROM(
    ->   SELECT item, serialNumber, price FROM source
    ->   UNION
    ->   SELECT item, NULL,         SUM(price)
    ->     FROM source
    ->    GROUP BY item
    -> ) t
    -> ORDER BY item, COALESCE(serialNumber,1E9);
+------+--------------+-------+
| item | serialNumber | price |
+------+--------------+-------+
| Awl  |            1 |    10 |
| Awl  |            3 |    10 |
| Awl  |         NULL |    20 |
| Bowl |            2 |    10 |
| Bowl |            5 |    10 |
| Bowl |            6 |    10 |
| Bowl |         NULL |    30 |
| Cowl |            4 |    10 |
| Cowl |         NULL |    10 |
+------+--------------+-------+


ROLLUP and GROUPING SETS

SQL Server, MySQL, and DB2 can do this using the WITH ROLLUP clause. Oracle has a GROUPING SETS clause that covers this (DB2 supports this as well).

SQL Server, MySQL, and DB2
In SQL Server, MySQL, and DB2:
mysql> SELECT item, serialNumber, SUM(price)
    ->   FROM source
    ->  GROUP BY item,serialNumber WITH ROLLUP;
+------+--------------+------------+
| item | serialNumber | SUM(price) |
+------+--------------+------------+
| Awl  |            1 |         10 |
| Awl  |            3 |         10 |
| Awl  |         NULL |         20 |
| Bowl |            2 |         10 |
| Bowl |            5 |         10 |
| Bowl |            6 |         10 |
| Bowl |         NULL |         30 |
| Cowl |            4 |         10 |
| Cowl |         NULL |         10 |
| NULL |         NULL |         60 |
+------+--------------+------------+


Oracle and DB2
In Oracle and DB2:
SQL> SELECT item, serialNumber, SUM(price)
  2    FROM source
  3    GROUP BY GROUPING SETS ((item,serialNumber),(item),(  ));

ITEM                           SERIALNUMBER SUM(PRICE)
------------------------------ ------------ ----------
Awl                                       1         10
Awl                                       3         10
Awl                                                 20
Bowl                                      2         10
Bowl                                      5         10
Bowl                                      6         10
Bowl                                                30
Cowl                                      4         10
Cowl                                                10
                                                    60


Hacking the Hack

Of course, there is no reason why you shouldn't include the GRAND TOTAL in another UNION:
mysql> SELECT item, serialNumber, price FROM(
    ->   SELECT item, serialNumber, price FROM source
    ->   UNION
    ->   SELECT item, NULL,         SUM(price)
    ->     FROM source
    ->    GROUP BY item
    ->   UNION
    ->   SELECT NULL, NULL,         SUM(price)
    ->     FROM source
    -> ) t
    -> ORDER BY COALESCE(item,'zzz'), COALESCE(serialNumber,1E9);
+------+--------------+-------+
| item | serialNumber | price |
+------+--------------+-------+
| Awl  |            1 |    10 |
| Awl  |            3 |    10 |
| Awl  |         NULL |    20 |
| Bowl |            2 |    10 |
| Bowl |            5 |    10 |
| Bowl |            6 |    10 |
| Bowl |         NULL |    30 |
| Cowl |            4 |    10 |
| Cowl |         NULL |    10 |
| NULL |         NULL |    60 |
+------+--------------+-------+

But having totals showing in the same column as the values themselves can make for a confusing report. If you can format the subtotals and totals so that they stand out, it can help. But it improves readability if you can put these in different columns.
You can add two more columns to the UNION. It takes a little care to put the NULL values in the right place:
mysql> SELECT item,serialNumber,price,sub,grand FROM (
    ->    SELECT item,serialNumber,price,NULL AS sub,NULL AS grand
    ->      FROM source
    ->    UNION
    ->    SELECT item,NULL,        NULL, SUM(price), NULL
    ->      FROM source
    ->     GROUP BY item
    ->    UNION
    ->    SELECT NULL,NULL,        NULL, NULL,       SUM(price)
    ->      FROM source
    ->  ) t
    ->  ORDER BY COALESCE(item,'zzz'), COALESCE(serialNumber,1E9);
+------+--------------+-------+------+-------+
| item | serialNumber | price | sub  | grand |
+------+--------------+-------+------+-------+
| Awl  |            1 |    10 | NULL |  NULL |
| Awl  |            3 |    10 | NULL |  NULL |
| Awl  |         NULL |  NULL |   20 |  NULL |
| Bowl |            2 |    10 | NULL |  NULL |
| Bowl |            5 |    10 | NULL |  NULL |
| Bowl |            6 |    10 | NULL |  NULL |
| Bowl |         NULL |  NULL |   30 |  NULL |
| Cowl |            4 |    10 | NULL |  NULL |
| Cowl |         NULL |  NULL |   10 |  NULL |
| NULL |         NULL |  NULL | NULL |    60 |
+------+--------------+-------+------+-------+
 
Source : Direct Link 
 
 
*If this site helps you, please click the link below to say Thanks*

Please Register here to Say Thank You :)
 


Wednesday, June 19, 2013

MYSQL 3.51.27 and 5.1.5 Database

MySQL open source software is provided under the GPL License.
OEMs, ISVs and VARs can purchase commercial licenses.


Source : http://www.mysql.com/

Download Here : Link


How to Download :
1. Click "Direct Link"
2. Click "Skip Ad"
3. Download the File  



*If this site helps you, please click the link below to say Thanks*

THANK YOU! :)

Friday, May 4, 2012

Store and Retrieve Image in MYSQL Database using Binary Data

Follow the instruction and you may not get an error.

Do this:
>> Go to MySQL Administrator  >> Startup Variable >> Advanced Networking  >>
Change Max. Pocket Size to 4M or 16M  >> Apply Changes >> Exit.

1. Create a table in your MYSQL Database

Database name: "photo"
Table name: "tblpicture"

Fields name and type:

>> "Picture"  as  LONG BLOB    'where you can save the Image using Binary Data

>> "Picturesize" as Integer    'where you can save the File Size of the Image

>> "ID" as Number , check Autoincrement


2. Then Go to Your Visual Basic 6.0


>>You Need:

Two Modules , One Form and One Picture in your Folder name "No Picture.jpg"

>> In your Form you need two "Image"  , Stretch = True

Name: "Image2" and "Image3"

>> three "Command Buttons"

Name: "cmdUpload" and "cmdSave" and "cmdRetrieve"

>>One "CommonDialog"

Name: "CommonDialog1"



3. Code  (Form) >> Copy/Paste

 'Declaration

Option Explicit
Dim picdialog As Integer
Dim PictureFileName As String

Private Sub cmdUpload_Click()
    'For Browsing Pictures

    On Error GoTo errhandler
    CommonDialog1.CancelError = True

    CommonDialog1.Filter = "All Picture Files|*.jpg;*.gif;*.bmp;*.wmf;*.ico|JPEG Images(*.jpg)|*.jpg|Bitmap Images (*.bmp)|*.bmp|Word Meta Files (*.wmf)|*.wmf|GIF Images (*.gif)|*.gif"
    CommonDialog1.DialogTitle = "Insert Picture"
   
   
    CommonDialog1.ShowOpen

            picdialog = 2
            Image2.Visible = True
            Image2.Picture = LoadPicture(CommonDialog1.filename)
            PictureFileName = CommonDialog1.filename
            If Image2.Picture = Picture Then
                Image3.Visible = True
            Else
                Image3.Visible = False
            End If

Exit Sub
errhandler:
    Select Case Err
    Case 32755 '  Dialog Cancelled
        MsgBox "you cancelled the dialog box"
    Case Else
        MsgBox "Unexpected error. Err " & Err & " : " & Error
    End Select
End Sub

Private Sub cmdSave_Click()
       
   
    With rsPhoto
    .AddNew

      If MsgBox("Are you sure you want to save picture?", vbQuestion + vbYesNo, "Save") = vbYes Then
                            If picdialog = 1 Then
                                CommonDialog1.filename = App.Path & "\No Picture.jpg"
                                GetPhoto CommonDialog1.filename, rsPhoto, "Picture", "Picturesize"
                                .Update
                            ElseIf picdialog = 2 Then
                              
                                If Not IsNull(CommonDialog1.filename) Then GetPhoto CommonDialog1.filename, rsPhoto, "Picture", "Picturesize"
                                .Update
                            End If
                          
                  
                  
                    MsgBox "Record Save", vbInformation, "Save"
                    Image3.Picture = LoadPicture(App.Path & "\No Picture.jpg")
                    Image2.Picture = LoadPicture(App.Path & "\No Picture.jpg")
                    Image2.Visible = False
                    Image3.Visible = True
       Else
                        .CancelUpdate
       End If
    End With
End Sub

Private Sub cmdRetrieve_Click()
       

        Image3.Visible = False
        Image2.Visible = True
        If Not IsNull(rsPhoto!Picture) Then FillPhoto rsPhoto, "Picture", "Picturesize", Image2 Else: Image3.Visible = True

End Sub

Private Sub Form_Load()
    picdialog = 1
    Image2.Visible = False
   
    Closerecordset
    Openrecordset ("Select * from tblpicture")
End Sub

Private Sub Form_Unload(Cancel As Integer)
    rsPhoto.Close
    Set rsPhoto = Nothing
    CloseConnection
End Sub



4. Module (Name: "ADOConnection") >> Copy/Paste

Global con As New ADODB.Connection
Global conn As New ADODB.Connection
Global rsPhoto As New ADODB.Recordset
Global imsConnectionString As String

Public Sub main()
    On Error Resume Next
    DoEvents

        imsConnectionString = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=Photo;User='root';Password=''"
        con.ConnectionString = imsConnectionString

    Debug.Print "Connection Object Created"

    con.Open

        If con.State = adStateClosed Then
            MsgBox "Unable to connect to database", vbCritical
            Form1.Show
            Exit Sub
        End If
   
    con.CursorLocation = adUseClient
    Form1.Show
End Sub

Public Sub CloseConnection()
    con.Close
    Set con = Nothing
End Sub

Public Function Openrecordset(YourQuery As String)
    If rsPhoto.State = 0 Then rsPhoto.Open YourQuery, con, adOpenDynamic, adLockOptimistic
End Function

Public Function Closerecordset()
    If rsPhoto.State = 1 Then rsPhoto.Close
End Function


5. Module (Name : "modGenDec") >> Copy/Paste

'Declaration for Save/Load Picture

Public StudentIDselect As String
Public costumerpic As IPictureDisp

Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Public Const MAX_PATH = 260
Public Const BLOCK_SIZE = 10000
Public frm2Show As String

Public Sub FillPhoto(rstMain As Recordset, PFName As String, SizeField As String, picEmp As Image)

On Error Resume Next
Dim bytes() As Byte
Dim file_name As String
Dim file_num As Integer
Dim file_length As Long
Dim num_blocks As Long
Dim left_over As Long
Dim block_num As Long
Dim hgt As Single

    'me.imgPhoto.Visible = False
    Screen.MousePointer = vbHourglass
    DoEvents

    ' Get a temporary file name.
    file_name = TemporaryFileName()

    ' Open the file.
    file_num = FreeFile
    Open file_name For Binary As #file_num

    ' Copy the data into the file.
    file_length = rstMain(SizeField)
    num_blocks = file_length / BLOCK_SIZE
    left_over = file_length Mod BLOCK_SIZE

    For block_num = 1 To num_blocks
        bytes() = rstMain(PFName).GetChunk(BLOCK_SIZE)
        Put #file_num, , bytes()
    Next block_num

    If left_over > 0 Then
        bytes() = rstMain(PFName).GetChunk(left_over)
        Put #file_num, , bytes()
    End If

    Close #file_num

    picEmp.Picture = LoadPicture(file_name)

    Screen.MousePointer = vbDefault

End Sub


Public Sub GetPhoto(filename As String, rstMain As Recordset, FieldName As String, SizeField As String)
On Error Resume Next
Dim file_num As String
Dim file_length As Long
Dim bytes() As Byte
Dim num_blocks As Long
Dim left_over As Long
Dim block_num As Long

    file_num = FreeFile
    Open filename For Binary Access Read As #file_num

    file_length = LOF(file_num)
    If file_length > 0 Then
        num_blocks = file_length / BLOCK_SIZE
        left_over = file_length Mod BLOCK_SIZE

        rstMain(SizeField) = file_length

        ReDim bytes(BLOCK_SIZE)
        For block_num = 1 To num_blocks
            Get #file_num, , bytes()
            rstMain(FieldName).AppendChunk bytes()
        Next block_num

        If left_over > 0 Then
            ReDim bytes(left_over)
            Get #file_num, , bytes()
            rstMain(FieldName).AppendChunk bytes()
        End If

        Close #file_num
    End If
Exit Sub
End Sub

Public Function TemporaryFileName() As String
Dim temp_path As String
Dim temp_file As String
Dim Length As Long

    ' Get the temporary file path.
    temp_path = VBA.Space$(MAX_PATH)
    Length = GetTempPath(MAX_PATH, temp_path)
    temp_path = Left$(temp_path, Length)

    ' Get the file name.
    temp_file = VBA.Space$(MAX_PATH)
    GetTempFileName temp_path, "per", 0, temp_file
    TemporaryFileName = Left$(temp_file, InStr(temp_file, VBA.Chr$(0)) - 1)
End Function




******Remember**********
>> In your "Project Explorer" right side of your window screen,then "Project Property" Right click
 set the "Startup Object" to "Sub Main" then Run your Program


*****Oooopsss!!!********
>> In the "cmdRetrieve_Click()" you can now use your "LOGIC" to retrieve the picture you want :)

ENJOY :)