Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

การใช้ MySQL ใน Batch Mode

MySQL รันใน batch mode ได้ โดยใส่ statements ในไฟล์ แล้วสั่ง 'mysql' อ่าน input จากไฟล์:

shell> mysql < batch-file

บน Windows หากมี special characters ใช้:

C:\> mysql -e "source batch-file"

ระบุ connection parameters:

shell> mysql -h host -u user -p < batch-file
Enter password: ********

ทำไมต้องใช้ Script?

  • Query ที่รันบ่อย: สร้าง script หลีกเลี่ยง retype ทุกครั้ง
  • Note: หาก script ต้องทำงานต่อแม้มี errors ใช้ '--force' command line option
  • Generate new queries จาก existing queries ที่คล้ายกันโดย copy/edit script files
  • Batch mode useful สำหรับ developing query (multi-line/multi-statement) ผิดพลาดแค่ edit script รันใหม่

Output ใหญ่: pipe through pager แทน scroll off screen:

shell> mysql < batch-file | more

เก็บ output ไว้ process ต่อ:

shell> mysql < batch-file > mysql.out

Share/distribute script ให้ others execute statements ได้