0

This is my code example.

import sqlite3

def application(environ, start_response):

 output = "<p> LOG</p>"

 db = sqlite3.connect('/root/example.db')
 db.row_factory = sqlite3.Row
 cursor = db.cursor()
 cursor.execute('''SELECT id, message,date FROM table''')
 for row in cursor:
  print('{0} : {1}, {2}'.format(row['id'], row['message'], row['date']))
 db.close()

start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
return output

I get Internal Server Error.

How would solve the problem?

4
  • Have you checked apache logs? Commented Apr 7, 2015 at 10:55
  • it would be in /var/log/apache2/error.log Commented Apr 7, 2015 at 10:55
  • [Thu Apr 02 20:36:16 2015] [error] [client 172.16.1.1] File "/var/www/html/controller.py", line 7, in application [Thu Apr 02 20:36:16 2015] [error] [client 172.16.1.1] db = sqlite3.connect('/root/example.db') [Thu Apr 02 20:36:16 2015] [error] [client 172.16.1.1] OperationalError: unable to open database file [Thu Apr 02 20:36:48 2015] [error] [client 172.16.1.1] mod_wsgi (pid=15627, process='', application='example.com|'): Failed to parse WSGI script file '/var/www/html/controller.py'. Commented Apr 7, 2015 at 11:01
  • I guess (or better hope) your webserver is not running as root? You have to put the database in a directory where your webserver can read and write, not /root. Commented Apr 7, 2015 at 11:23

1 Answer 1

1

Since you webserver (hopefully) doesn't run as the root-User, placing your database in the /root directory will not work.

You habe to put the database in a directory that is writable by your webserver (as it looks to me, that would be /var/www for you).

And just to prevent further privilege problems, the database file needs to be readable and writable by the webserver as well. On Ubuntu/Debian systems, this can be done with

chown www-data:www-data /var/www/example.db
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It works! But only shows message LOG. Checking /var/log/html/error_log appear the values. But I would like that return row values on the web. Any idea?
Of course you have to append the rows to the output variable that you are returning and not printing them.

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.