MySQL local script installer

From NSIS Wiki

Description

This is a small utility I made to import sql files to local MySQL 4.1 servers, as I have to do this on many machines. It's a stripped down version as others stuffs were too specifics to my needs but this version works fine too.

Basically, the script checks if no error is returned (like wrong password) and continues if it's ok.


The Script

Name "MySQL script Installation"
 
Var folder
var source
Var login
Var password
Var createdb
Var errorsrc
 
!define DBNAME mydatabase
 
 
# Included files
!include MUI2.nsh
!include InstallOptions.nsh
 
 
OutFile InstallSQLScript.exe
CRCCheck on
XPStyle on 
 
ShowInstDetails show
 
BrandingText "MYSQL import - alx5962"
 
 
Page custom GetInfos "" ": Information page"
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
 
 
Function .onInit 
    InitPluginsDir
    !insertmacro INSTALLOPTIONS_EXTRACT "custom.ini"    
    !insertmacro INSTALLOPTIONS_WRITE "custom.ini" "Field 10" "State" "${DBNAME}"
FunctionEnd
 
Section -Main
    ReadINIStr $folder "$PLUGINSDIR\custom.ini" "Field 2" "State" 
    ReadINIStr $source "$PLUGINSDIR\custom.ini" "Field 4" "State"
    ReadINIStr $login "$PLUGINSDIR\custom.ini" "Field 6" "State"
    ReadINIStr $password "$PLUGINSDIR\custom.ini" "Field 8" "State"
    ReadINIStr $createdb "$PLUGINSDIR\custom.ini" "Field 9" "State"
 
    ExecWait '"$folder\bin\mysql" --user=$login --password=$password --execute="SHOW DATABASES;"' $0
    StrCmp $0 1 0 +4
    StrCpy $errorsrc "Wrong login or password"
    Goto abortinst
 
    DetailPrint "Running import"
 
    StrCmp $createdb 1 createdbs importdbs
 
    createdbs:
    DetailPrint "Creating database"
    ExecWait '"$folder\bin\mysql" --user=$login --password=$password --execute="CREATE DATABASE ${DBNAME}"' $1
    StrCmp $1 1 0 +4
    StrCpy $errorsrc "Unable to create database"
    Goto abortinst    
 
    importdbs:
    DetailPrint "SQL file import"
    ExecWait '"$folder\bin\mysql" --user=$login --password=$password --execute="source $source" ${DBNAME}' $2
    StrCmp $2 1 0 endinst
    StrCpy $errorsrc "File import error"
    Goto abortinst    
 
 
    abortinst:
    DetailPrint "                         "  
    DetailPrint "$\n An error occured ! $\n"
    DetailPrint "  $errorsrc              "
    DetailPrint "                         "
 
    endinst:
 
 
SectionEnd
 
Function GetInfos 
    !insertmacro MUI_HEADER_TEXT "Database informations" "Database informations - please fill all elements"
    !insertmacro INSTALLOPTIONS_DISPLAY "custom.ini"
FunctionEnd


  • And now the InstallOption file called 'custom.ini' :


[Settings]
NumFields=10
CancelEnabled=1
Title=Page information
CancelShow=1
BackEnabled=1
 
[Field 1]
Type=Label
Text=MySQL folder
Left=16
Right=120
Top=7
Bottom=15
 
[Field 2]
Type=DirRequest
Left=16
Top=19
Right=138
Bottom=32
State=C:\
MinLen=3
Root=C:\
 
[Field 3]
Type=Label
Left=16
Top=73
Right=112
Bottom=82
Text=MySQL login
 
[Field 4]
Type=FileRequest
Left=16
Top=52
Right=283
Bottom=65
MinLen=3
Flags=FILE_MUST_EXIST|PATH_MUST_EXIST
 
[Field 5]
Type=Label
Left=15
Top=39
Right=138
Bottom=48
Text=SQL file
 
[Field 6]
Type=Text
Left=16
Top=87
Right=138
Bottom=100
State=root
 
[Field 7]
Type=Label
Left=161
Top=73
Right=273
Bottom=82
Text=MySQL password
 
[Field 8]
Type=Password
Left=161
Top=87
Right=283
Bottom=100
 
 
[Field 9]
Type=CheckBox
Left=16
Top=116
Right=97
Bottom=126
Text=" Create database :"
State=1
 
[Field 10]
Type=Text
Left=105
Top=115
Right=193
Bottom=128
Flags=READONLY
State=
Personal tools
donate
ads