# db_open

Definition: | integer i = db_open(string st, integer locktype)
Description: | Open an existing Euphoria database. The file containing the database is given by s. i is a return code indicating success or failure. locktype indicates the type of lock that you want to place on the database file while you have it open. This database becomes the current database to which all other database operations will apply.
pwa/p2js: | Not supported
Comments: | The types of lock that you can use are: DB_LOCK_NO (no lock), DB_LOCK_SHARED (shared lock for read-only access), DB_LOCK_EXCLUSIVE (for read/write access), and DB_LOCK_READ_ONLY (read-only no lock). DB_LOCK_SHARED is only supported on Linux. It allows you to read the database, but not write anything to it. If you request DB_LOCK_SHARED on Windows it will get the same lock as DB_LOCK_EXCLUSIVE, but still be read-only. The return codes (see also db_create) are: DB_OK(0) - success, DB_OPEN_FAIL(-1) - could not open the file, DB_LOCK_FAIL(-3) - could not lock the file in the manner requested, and DB_FATAL_FAIL(-404) - database is corrupt (does not start with DB_MAGIC). If the lock fails, your program should wait a few seconds and try again. Another process might be currently accessing the database.
Example: |
    tries = 0
    while 1 do
        err = db_open("mydata", DB_LOCK_SHARED)
        if err = DB_OK then
            exit
        elsif err = DB_LOCK_FAIL then
              tries += 1
              if tries > 10 then
                puts(2, "too many tries, giving up\n")
                abort(1)
              else
                  sleep(5)
              end if
        else
              puts(2, "Couldn't open the database!\n")
              abort(1)
        end if
    end while
See Also: | db_create, db_close