View Poll Results: Would this be a beneficial script?

Voters
4. You may not vote on this poll
  • Yes

    2 50.00%
  • No

    0 0%
  • Maybe

    2 50.00%
Results 1 to 4 of 4

Thread: Updating Your Exsisting Applications Database

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    Lightbulb Updating Your Exsisting Applications Database

    Hey there everyone,

    For my first ever post on vbforums I thought I would write about something that has been a right pain in the CPU.

    I have been looking for several weeks now for a low cost (or preferably free) module or other kind of add in that will allow me to update or create a database system. which will be used for one of my VB6 Applications, so i can easily update the structure of the database when I make a new version of my application. Thus far I have had no success (Except for the ones that cost money of course).

    However I have decided that the best way to accoplish this would be just to create an empty set of tables in a database to form a model database. This way I can just compare the model to the live database and alter or add as appropriate.

    I have written a module that will accomplish the update and am now getting ready to compile it in some form.

    I am however wondering if this might be a useful tool for anyone here?

    I also want to ask what and how is the best way is to compile my script to a dll or similar form?

    At present My script supports MySQL only, and I have created the script so if like me your database needs more than 1 set of the same tables you can add a suffix to the table name. If there is a need for a script like this then give me a shout as I am also looking into making the script compatible with a whole host of Databases, Including: -

    MSSQL, Oracle, PostgreSQL, Access, MySQL, DB2

    Also if things go well maybe even to others not yet stumbled upon.

    Also if there are any features that may be useful to the script I would be willing to add any possable requests.

    ------ Life is a waste of time ------
    ------ Time is a waste of life ------
    ------ Get wasted all the time ------
    - and you'll have the time of your life -

    Izzy Out.

  2. #2
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Updating Your Exsisting Applications Database

    Are you going to be posting your code in our CodeBank?
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    Thumbs up Re: Updating Your Exsisting Applications Database

    Hi again,

    Once this code is near finnished I will indeed post the code on here for everyone to use, also if required i can upload the module file for vb6.

    Save everyone from having to spend money.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    Re: Updating Your Exsisting Applications Database

    Right, After a long time of umming and arring I have finaly got my first test script for database updating ready. As I described before I uses 2 databases, a live one which is to be updated, and a model which is the new database structure to be used. At presant this has only got MySQL writen into the script as this is my development database type. Here is my code so far:

    VB Code:
    1. Public Function UDB(DBC1 As ADODB.Connection, _
    2. DBC2 As ADODB.Connection, dbname1 As String, dbname2 As String, _
    3. DBT, DoEdits As Integer, suffix, prefix As String, Progress As ProgressBar, _
    4. lbl1 As label) As Boolean
    5.     DoEvents
    6.     Dim DBrs As ADODB.Recordset, DBrs2 As ADODB.Recordset
    7.     Dim DBrs1 As ADODB.Recordset, DBrs3 As ADODB.Recordset
    8.     Dim strSQL, IsFound As Boolean, extracheck As Boolean
    9.     On Error GoTo UDerr
    10.     Set DBrs = New ADODB.Recordset
    11.     DBrs.ActiveConnection = DBC1
    12.     Set DBrs2 = New ADODB.Recordset
    13.     DBrs2.ActiveConnection = DBC2
    14.     Select Case DBT
    15.         Case "MySQL"
    16.             DBrs2.Open "SHOW TABLES WHERE Tables_in_" & dbname2 & _
    17. " LIKE '" & prefix & "%'"
    18.             Do While Not DBrs2.EOF
    19.                 DBrs.Open "SHOW TABLES WHERE Tables_in_" & dbname1 & _
    20. "='" & DBrs2("Tables_in_" & dbname2) & suffix & "'"
    21.                 If Not DBrs.EOF Then
    22.                     DBrs.Close
    23.                     Set DBrs1 = New ADODB.Recordset
    24.                     DBrs1.ActiveConnection = DBC2
    25.                     DBrs1.Open "DESCRIBE " & DBrs2("Tables_in_" & dbname2)
    26.                     Do While Not DBrs1.EOF
    27.                         Set DBrs3 = New ADODB.Recordset
    28.                         DBrs3.ActiveConnection = DBC1
    29.                         DBrs.Open "DESCRIBE " & DBrs2("Tables_in_" & dbname2) _
    30. & suffix
    31.                         IsFound = False
    32.                         extracheck = False
    33.                         Do While Not DBrs.EOF
    34.                             If DBrs("Field") = DBrs1("Field") Then
    35.                                 IsFound = True
    36.                                 If DBrs("Type") = DBrs1("Type") And DBrs("Null") _
    37. = DBrs1("Null") And DBrs("Default") = DBrs1("Default") And DBrs("Key") = _
    38. DBrs1("Key") And DBrs("Extra") = DBrs1("Extra") Then extracheck = True
    39.                             End If
    40.                             DBrs.MoveNext
    41.                         Loop
    42.                         DBrs.Close
    43.                         If IsFound Then
    44.                             If DoEdits = 0 Then
    45.                                 If DBrs1("Key") <> "PRI" Then
    46.                                 If Not extracheck Then
    47.                                 lbl1.Caption = "Updating Current Column: " & _
    48. DBrs2("Tables_in_" & dbname2) & suffix & "." & DBrs1("Field")
    49.                                 Progress.value = Progress.value + 1
    50.                                 strSQL = "ALTER TABLE " & DBrs2("Tables_in_" & _
    51. dbname2) & suffix & " MODIFY " & DBrs1("Field") & " " & " " & _
    52. DBrs1("Type") & " "
    53.                                 If DBrs1("Null") = "NO" Then strSQL = strSQL & _
    54. "NOT NULL "
    55.                                 If DBrs1("Default") <> "" Then strSQL = _
    56. strSQL & "DEFAULT '" & DBrs1("Default") & "'"
    57.                                 DBrs3.Open strSQL
    58.                                 End If
    59.                                 End If
    60.                             End If
    61.                         Else
    62.                             lbl1.Caption = "Adding Column to Current Table: " & _
    63. DBrs2("Tables_in_" & dbname2) & suffix & "." & DBrs1("Field")
    64.                             Progress.value = Progress.value + 1
    65.                             strSQL = "ALTER TABLE " & DBrs2("Tables_in_" & _
    66. dbname2) & suffix & " ADD COLUMN " & DBrs1("Field") & " " & " " & _
    67. DBrs1("Type") & " "
    68.                             If DBrs1("Null") = "NO" Then strSQL = strSQL & _
    69. "NOT NULL "
    70.                             If DBrs1("Default") <> "" Then strSQL = strSQL _
    71. & "DEFAULT '" & DBrs1("Default") & "' "
    72.                             If DBrs1("Key") = "PRI" Then strSQL = strSQL _
    73. & "PRIMARY KEY "
    74.                             If DBrs1("Extra") = "auto_increment" Then strSQL = _
    75. strSQL & "AUTO_INCREMENT"
    76.                             DBrs3.Open strSQL
    77.                         End If
    78.                         DBrs1.MoveNext
    79.                     Loop
    80.                 Else
    81.                     DBrs.Close
    82.                     lbl1.Caption = "Adding Table: " & DBrs2("Tables_in_" & _
    83. dbname2) & suffix
    84.                     Progress.value = Progress.value + 1
    85.                     strSQL = "CREATE TABLE " & DBrs2("Tables_in_" & dbname2) _
    86. & suffix & " ("
    87.                     Set DBrs1 = New ADODB.Recordset
    88.                     DBrs1.ActiveConnection = DBC2
    89.                     DBrs1.Open "DESCRIBE " & DBrs2("Tables_in_" & dbname2)
    90.                     Do While Not DBrs1.EOF
    91.                         strSQL = strSQL & DBrs1("Field") & " " & DBrs1("Type") & " "
    92.                         If DBrs1("Null") = "NO" Then strSQL = strSQL & "NOT NULL "
    93.                         If DBrs1("Default") <> "" Then strSQL = strSQL _
    94. & "DEFAULT '" & DBrs1("Default") & "' "
    95.                         If DBrs1("Key") = "PRI" Then strSQL = strSQL & _
    96. "PRIMARY KEY "
    97.                         If DBrs1("Extra") = "auto_increment" Then strSQL = _
    98. strSQL & "AUTO_INCREMENT"
    99.                         strSQL = strSQL & ","
    100.                         DBrs1.MoveNext
    101.                     Loop
    102.                     strSQL = Mid(strSQL, 1, Len(strSQL) - 1) & ")"
    103.                     DBrs.Open strSQL
    104.                 End If
    105.                 DBrs2.MoveNext
    106.             Loop
    107.     End Select
    108.     UDB = True
    109.     Progress.value = Progress.value + 1
    110.     lbl1.Caption = "Database Section Updated: - " & prefix
    111.     Exit Function
    112. UDerr:
    113.     UDB = False
    114. End Function

    to call this script use the following syntax

    VB Code:
    1. success = UDB(Live DB Connection, Model DB Connection, Name of Live Dabatase, Name of Model Database, Database Type, Do all changes (1=yes,0=no), table name suffix (used if there are more that 1 tables with same structure), table name prefix (used if you only want to updte 1 section of your database i.e. Users), ProgressBar name (used for returning progressbar status), label name (used to return current update status))

    If the function returns true the update was successful, other wise there was an error.

    If anybody has any further sugesstions to add to this i would be happy to incorporate them in

    Hope this helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width