database locked occasionally with shell script sqlite3

Any guidance you can give is much appreciated. I did search the existing posts.

I’ve been scripting using do shell script sqlite3. The script I’m asking about keeps track of students signing into the library at my school. The script keeps a dialog box open so that students can scan the barcode from their ID into the dialog box. The script writes the barcode with date and time stamp etc. to the database. Then the script pops back up and re-opens the dialog box for the next student to scan.

I’m using do shell script sqlite3:


repeat while nothing is equal to ""

	tell application "System Events"
		tell process "Library Scan Master"
			set frontmost to true
		end tell
	end tell
	
	--GET BARCODE
	
	set barcode to the text returned of (display dialog "Enter your student number:" default answer "")

	--[SCRIPT OMITTED THAT PROCESSES BARCODE TO GET STUDENT NAME, TIME AND DATE STAMP, PERIOD, ETC.]

	--=====WRITE NAME, DATE, TIME TO DATABASE========		
		
	set db_path to quoted form of ("/Library/WebServer/Documents/[database name")
		
		
	--GET SET UP FOR SQLITE SHELL COMMANDS
	set db_path to space & db_path & space
		
	set sql_start to "sqlite3" & db_path & quote
		
	set sql_end to quote
		
		
		
	set sql_command to "insert into patrons (stid, pd, stname, timein, date) values ('" & barcode & "','" & pd & "','" & student_name & "',' " & time_stamp & "','" & date_stamp & "')"
		
		
		
		
	do shell script sql_start & sql_command & sql_end
		
end repeat		
		

The script will occasionally throw a “database locked” error - typically when there are many kids scanning in quickly one after the other.

My questions:

  1. When using shell script sqlite3, do you have to close the database? Is there a command for closing it that needs to be run at the end? I’m concerned that the database is locked because the script hasn’t closed it properly.

  2. If that’s not the issue, does this mean that the script does not wait for the database to finish writing before it proceeds with the next line of code? Is that the issue?