0

I'm trying to use an SQLite insert operation in a python script, it works when I execute it manually on the command line but when I try to access it on the web it won't insert it in the database. Here is my function:

def insertdb(unique_id,number_of_days):
        conn = sqlite3.connect('database.db')
        print "Opened database successfully";
        conn.execute("INSERT INTO IDENT (ID_NUM,DAYS_LEFT) VALUES (?,?)",(unique_id,number_of_days));
        conn.commit()
        print "Records created successfully";
        conn.close()

When it is executed on the web, it only shows the output "Opened database successfully" but does not seem to insert the value into the database. What am I missing? Is this a server configuration issue? I have checked the database permissions on writing and they are correctly set.

6
  • sqlite writes temporary journal files. The directory where your datab ase is located needs to have write access by your web server. Does it? Commented Dec 10, 2013 at 1:20
  • Do you know where I would go about changing this? Commented Dec 10, 2013 at 1:23
  • 2
    Normally the containing directory needs to have read/write/execute permissions for the webserver user (or group). e.g: If you're webserver user/group is apache/apache the following should fix this: chmod 775 /path/to/db/dir and chown apache:apache /path/to/db/dir Commented Dec 10, 2013 at 1:26
  • Do you have any idea what the current working directory is when running in your unspecified web environment? If not, why are you using database.db without a full path? If so, what is it, and what are the owner and mode on it? Commented Dec 10, 2013 at 1:30
  • Also, why is this tagged mysql when you're trying to use sqlite? Commented Dec 10, 2013 at 1:33

1 Answer 1

2

The problem is almost certainly that you're trying to create or open a database named database.db in whatever happens to be the current working directory, and one of the following is true:

  • The database exists and you don't have permission to write to it. So, everything works until you try to do something that requires write access (like commiting an INSERT).
  • The database exists, and you have permission to write to it, but you don't have permission to create new files in the directory. So, everything works until sqlite needs to create a temporary file (which it almost always will for execute-ing an INSERT).

Meanwhile, you don't mention what web server/container/etc. you're using, but apparently you have it configured to just swallow all errors silently, which is a really, really bad idea for any debugging. Configure it to report the errors in some way. Otherwise, you will never figure out what's going on with anything that goes wrong.

If you don't have control over the server configuration, you can at least wrap all your code in a try/except and manually log exceptions to some file you have write access to (ideally via the logging module, or just open and write if worst comes to worst).

Or, you can just do that with dumb print statements, as you're already doing:

def insertdb(unique_id,number_of_days):
        conn = sqlite3.connect('database.db')
        print "Opened database successfully";
        try:
            conn.execute("INSERT INTO IDENT (ID_NUM,DAYS_LEFT) VALUES (?,?)",(unique_id,number_of_days));
            conn.commit()
            print "Records created successfully";
        except Exception as e:
            print e # or, better, traceback.print_exc()
        conn.close()
2
  • Awesome, you are correct it seems that I don't have the correct permissions to do an INSERT operation on the database via the web. Unsure how to go about doing that but at least I know what is wrong, and that's half the battle. Commented Dec 10, 2013 at 2:06
  • @RayY: I explained where to start looking in my answer and in my comments, along with James Mills's comments. What is the current working directory where database.db lives? What user does the web server run as? What's the ownership and permissions of database.db and of the directory it lives in? If the web-server user has a shell, it may help to login as that user and run your script directly in the command-line Python interpreter. And if you need more help on permissions stuff, ask at SuperUser. Commented Dec 10, 2013 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.