- NSIS Discussion
- Please help me with FileWrite and FileClose
Archive: Please help me with FileWrite and FileClose
sunlight112
1st December 2006 06:32 UTC
Please help me with FileWrite and FileClose
With many supporters in forum, I can run database well. However, I have one problem which I can explain why it happens. I had worked well last week. But in this week, my code run is error although I do not repair any code line in it. I do not know why!
My code here:
Function DropSchema
Detailprint "Delete database if it exists"
GetTempFileName $8
Detailprint "Get Temp File Name: $8" ##C:\Document.....
StrCpy $8 "drop.bat"
FileOpen $9 $8 "w"
FileWrite $9 "@echo off$\r$\n"
StrCpy $7 $TEMP 2 ##C:
FileWrite $9 "$7$\r$\n"
FileWrite $9 "mysql.exe --user=$root --password=$passroot < c:\drop_db.sql$\r$\n"
FileWrite $9 "pause$\r$\n"
FileClose $9
nsExec::exec "$8"
Pop $8
StrCmp $8 "0" 0 Error
Delete $8
Detailprint "Delete database successful"
Goto End
Error:
Detailprint "Delete database fail"
Detailprint "Error: $8"
MessageBox MB_OK|MB_ICONSTOP "Delete database is error.$\r$\nPlease try again latter"
Abort
End:
FunctionEnd
Section SetupSchema_RunDB
Call DropSchema
Detailprint "Create database"
GetTempFileName $5
Detailprint "Get Temp File Name: $5" ##C:\Document.....
StrCpy $5 "db.bat"
FileOpen $6 $5 "w"
FileWrite $6 "@echo off$\r$\n"
StrCpy $7 $TEMP 2 ##C:
FileWrite $6 "$7$\r$\n"
FileWrite $6 "mysql.exe --user=$root --password=$passroot < c:\schema.sql$\r$\n"
FileWrite $6 "pause $\r$\n"
FileClose $6
nsExec::exec "$5"
Pop $5
StrCmp $5 "0" 0 Error
Delete $5
Detailprint "Create database successful"
Goto Next
Error:
Detailprint "Create database fail"
Detailprint "Error: $5"
MessageBox MB_OK|MB_ICONSTOP "Create database is error.Do you type correctly password?$\r$\nRun server will be error.Please try again latter"
Goto End
Next:
Detailprint "Run script"
GetTempFileName $0
Detailprint "Get Temp File Name: $0" ##C:\Document.....
StrCpy $0 "sql.bat"
FileOpen $1 $0 "w"
FileWrite $1 "@echo off$\r$\n"
StrCpy $2 $TEMP 2 ##C:
FileWrite $1 "$2$\r$\n"
FileWrite $1 "mysql.exe OpenIM --user=$root --password=$passroot < c:\openim.sql$\r$\n"
FileWrite $1 "pause$\r$\n"
FileClose $1
nsExec::exec "$0"
Pop $0
StrCmp $0 "0" 0 lblError
Delete $0
Detailprint "Run database successful"
Goto End
lblError:
Detailprint "Run database fail"
Detailprint "$0"
MessageBox MB_OK|MB_ICONSTOP "Run database is error.Do you type correctly password?$\r$\nRun server will be error.Please try again latter"
Goto End
End:
SectionEnd
Now I do not explain why I run it error.
I aslo compile it many times and run it again, again too much but sometime, It runs well. Sometimes, it does not run :(
Somebody know please tell me why?
Thank a lot,
bholliger
1st December 2006 09:52 UTC
Hi sunlight112!
If there's a problem with the database, redirect the output to a file for further diagnose.
mysql.exe --user=$root --password=$passroot < c:\drop_db.sql > c:\error.log
Cheers
Bruno
Red Wine
1st December 2006 10:03 UTC
Just a quick look on your script, try this, it may help,
remove the line StrCpy $8 "drop.bat"
after FileClose use rename e.g
Rename '$8' '$TEMP\drop.bat'
nsExec::exec '$TEMP\drop.bat'
sunlight112
4th December 2006 02:42 UTC
Thank bholliger and Red Wine a lot,
I has tryed with your ideas but It still can not work well.
With file error.log, after I run installer, nothing has in this file.
At that time, my installer run well. It means no error but database can not be created.
With this same installer, I run the same way, the first, It runs well.However,the second, it is still not to create database again( i has delete database from mysql before I install my installer again to check why it is wrong)
I can not explain why sometimes it works well and sometimes it can not work.
Please help me!
Does my code has some problems?
Please tell me if you do not clearly what I say :)
Thank in advance,
bholliger
4th December 2006 03:58 UTC
Hi sunlight112!
I forgot something. Maybe the STDERR stream is not included in the file. Try using 2>&1 as well.
mysql.exe --user=$root --password=$passroot < c:\drop_db.sql 2>&1 > c:\error.log
As Red Wine wrote before, I wouldn't use GetTempFileName. I'd use $TEMP\myfile.bat.
Is mysql.exe in the path variable? What happens if you execute the string in the prompt?
Cheers
Bruno
sunlight112
4th December 2006 06:22 UTC
Thank bholliger,
I am sure the path variable has set. I also check my command with command line and it works well.
I has changed my path to become $TEMP like this:
Function DropSchema
ClearErrors
FileOpen $9 $8 "w"
FileWrite $9 "@echo off$\r$\n"
StrCpy $7 $TEMP 2 ##C:
FileWrite $9 "$7$\r$\n"
FileWrite $9 "mysql.exe --user=$root --password=$passroot < c:\drop_db.sql > c:\error.log$\r$\n"
FileWrite $9 "pause$\r$\n"
FileClose $9
Rename '$8' '$TEMP\drop.bat'
StrCpy $Variable '$TEMP\drop.bat'
nsExec::exec '$Variable'
Pop $Variable
StrCmp $Variable "0" 0 Error
Delete $Variable
Detailprint "Delete database successful"
Goto End
Error:
Detailprint "Delete database fail"
Detailprint "Error: $Variable"
MessageBox MB_OK|MB_ICONSTOP "Delete database is error.$\r$\nPlease try again latter"
Abort
End:
FunctionEnd
It still does not works although I try with your idea " 2>&1 "
I try with "2>&1" logfile does not appear when I run my installer.
I think my installer run unstable. But I can not repair it and make it better.
sunlight112
4th December 2006 08:31 UTC
Now I can know why my installer run is wrong
It may be my error.
When I create installer, I set environment after that I run database. I think it can be this problem because sometimes it does not understand path variable of mysql. So, I can be error. However, I close my installer, I run my batch file. At that time, my batch file run well.
I know when I set environment variable I should restart computer. But it do it, how can I run my database for user?
Somebody used to pass this problem, please help me!
Your help is very useful for me!
Thank a lot,
bholliger
4th December 2006 11:36 UTC
Hi sunlight112!
This link [1] might help you.
[1] http://dev.mysql.com/doc/refman/5.0/...mand-line.html
Cheers
Bruno
sunlight112
5th December 2006 02:40 UTC
Thank bholliger a lot,
Now I can run my installer to create database but I am worry about root and password to run it.
I allow user to type it in my installer. But I run with the wrong password, my installer still announce "RUN DATABASE SUCCESSFUL"
Detailprint "Run script"
GetTempFileName $0 $TEMP
; StrCpy $0 "sql.bat"
FileOpen $1 $0 "w"
FileWrite $1 "@echo off$\r$\n"
StrCpy $2 $TEMP 2 ##C:
FileWrite $1 "$2$\r$\n"
FileWrite $1 '"$Mysql\mysql.exe" --user=$root --password=$passroot -D openim< "$path_openim\home\database\openim.sql"$\r$\n'
FileWrite $1 "pause$\r$\n"
FileClose $1
Rename $0 '$TEMP\run.bat'
StrCpy $9 '$TEMP\run.bat'
nsExec::exec "$9"
Pop $9
Detailprint "$$9 : $9"
StrCmp $9 "0" 0 lblError
Delete '$TEMP\run.bat'
Detailprint "Run database successful"
Goto End
lblError:
Detailprint "Run database fail"
Detailprint "$9"
MessageBox MB_OK|MB_ICONSTOP "Run database is error.Do you type correctly password?$\r$\nRun server will be error.Please try again latter"
Goto End
End:
Almost POP $9 always give "0" although run database successful or fail.
How can I know to run database is error if user typed wrong password and root.
Please help me!
Thank a lot,
bholliger
5th December 2006 03:39 UTC
Hi sunlight112!
I'd use ExecWait to run mysql.exe. It allows you to get the return code of mysql.exe in a variable. I believe that you have to change the command. Instead of "<" use -e "sourcefile". Have a look at [1] for more information.
Altough I can't check it, the return codes mitght be those described in [2].
Due to this change you don't have to worry about writing sql files and deleting them afterwards.
[1] http://dev.mysql.com/doc/refman/5.0/en/batch-mode.html
[2] http://dev.mysql.com/doc/refman/5.0/...es-client.html
Have fun!
Cheers
Bruno
sunlight112
5th December 2006 06:25 UTC
Thank bholliger a million,
Now my installer run well
:D