Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of the defining characteristics of the SQLite database management system is that it keeps each database in a single file. If your database is going to run in a UNIX-based environment, which includes many, if not most, web servers, then you'll need to understand how to set file permissions so that your application can access the database. Here we are working on a Mac, running Mac OS X, which happens to be UNIX-based, and so we have this issue of file permissions. This is the directory where the data is being kept, and if I right-click on this and bring up Get Info - if you also on a Mac, follow along if not, just watch - you'll notice down here at the bottom we have the permissions for this folder.
It says Read & Write permissions for Me, and it says Read & Write permissions for everyone. That's what allows the web server, which runs as a different user, to be able to create files in that directory. If I change this to Read only, which is the default, when I created that directory this was the way that it came up, and then I come in here and tried to run my createdatabase database script, you'll notice that it fails. It says "bwSQLite could not create database," "unable to open database file." That means that the permissions on the directory were not allowing the web server to create a file in that directory.
So, if I come back over here, and I change this to Read & Write for everyone and then I run this program again, you'll see that it successfully created the database, and there it is in the folder. You'll also notice that when I Get Info on that files that it's owned by nobody. The web server is running as a user called nobody, and this is typical of shared web hosts. So, in order for that nobody user to be able to create files in this directory, the permissions on the directory itself which is owned by Me, must allow Read & Write permissions for others.
On the Mac, that's called everyone; on some operating systems it's called others, or world. Now, if you're not running on a Mac, or even if you are running on a Mac and your script is going to be running on a server some place that happens to be a UNIX-based server, like many of them are, then you'll need to know how to set these permissions on the remote server. So let's take a look at how we do that. Here I have a program called Transmit which is the file transfer program, the FTP program that I'm using. It doesn't matter what FTP program you're using or if you are using something else on a different operating system; the techniques are going to be the same. The actual keystrokes and button presses might be a little bit different, but here is what you are going to look for.
This is the folder where I am keeping the data on that server. When I right-click on it, and usually if I am in a program that I'm not familiar with, and I am looking for how to do this, that's going to be the first thing I'm going to try, so I am going to right-click on it, and I am going to select Get Info here. You'll look for something in this pop-up menu, or in one of the menus at the top of the program that says something about setting permissions or getting info or something like that on the file, where you might expect to find this dialog box. Here is the dialog box that we are looking for. This is the information on that file. It's got its file Size.
It's got the Modified date. It says who the owner of the file is. That's the User account that created it, and then it's got the permissions themselves. The permissions are often displayed in several different forms. Typically, if you're a UNIX person like I am, you'll recognize this; this is the octal bits of the file. Anybody who has worked with UNIX for a long time knows with that means. If you don't know what that means, don't worry about it. It's automatically updated. You are going to use these check boxes here, and you'll notice that there are permissions for User.
In this case, that's the same as Owner, billw - that's me. There are permissions for Group, and you are not going to worry about that and all. In this case, that's also me because this is owned by me without being set to a group. And then there are permissions for World, which is the same as everyone on the Mac or other on some other operating systems, and here it says world. So, that's the one that you are concerned with, because the web server is running as a different user than the owner of the file. So, here it's checked Read, Write and Execute. In the case of the directory, or a folder, the Execute bit serves a different purpose than it does with regular files.
The Execute bit is the bit that says that it's a directory, so you need to leave that checked. But if I were to uncheck this one here, which is Write permissions for World - you'll notice that these other displays get updated; you need to worry about that - and I press Apply, and now I go in, and I run the createdatabase script on the server which I have loaded up over here, you see, I get that same error; "could not create the database, unable to open database file." Now, if I come back here and bring this up again, that's still up there actually, and I checked that right and I press Apply, now I go back over to the web browser and run the script again, you see, it successfully created the file.
So, we'll close this, and we need to reload the directory because it's on a remote server so it doesn't do that automatically, and there is the file that we created from that script. So, understanding file permissions is going to save you a lot of trouble and headaches if your database application is going to run on a UNIX-based system. Keep in mind that the host application, that's the web server in this case, contains the SQLite driver, and it must have Read, Write and Create permissions in the folder where you create the database file.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 74767 Viewers
80 Video lessons · 129852 Viewers
52 Video lessons · 64057 Viewers
59 Video lessons · 49845 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.