#! /usr/bin/python
import sys
import sqlite3
import apache_log_parser
from pprint import pprint

def print_usage():
	print("Error: Wrong arguments")
	print("Syntax:")
	print("    apache2sqlite.py <apache_log_file> <output_database>")
	sys.exit()

if len(sys.argv) != 3:
	print_usage()
try:	
	log_name = sys.argv[1]
	db_name  = sys.argv[2]	
except ValueError:
	print_usage()

APACHE_COMBINED="%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""  
APACHE_COMMON="%h %l %u %t \"%r\" %>s %b"

line_parser = apache_log_parser.make_parser(APACHE_COMBINED)

conn = sqlite3.connect(db_name)
c = conn.cursor()
try:
	c.execute("DROP TABLE logs")
except sqlite3.OperationalError:
	pass

try:
	c.execute("CREATE TABLE logs (id INTEGER PRIMARY KEY, source, request, status integer, size integer, method, referer, agent, version integer, date, user, label)")
except sqlite3.OperationalError:
	print("logs table already exists")

f = open(log_name, 'r')
line_num = 1
for line in f:
	line_data = line_parser(line)
	c.execute("   INSERT into logs "+
	          "               (id, source, request, status, size, method, referer, agent, version, date, user) "+
	          "        VALUES ("+str(line_num)+" ,"+
	                      "'"+line_data['remote_host']+"' ,"+
	                      "'"+line_data['request_url']+"' ,"+
	                      " "+line_data['status']+" ,"+
	                      " "+line_data['response_bytes_clf']+" ,"+
	                      "'"+line_data['request_method']+"' ,"+
	                      "'"+line_data['request_header_referer']+"' ,"+
	                      "'"+line_data['request_header_user_agent']+"' ,"+
	                      "'"+line_data['request_http_ver']+"' ,"+
	                      "'"+line_data['time_received']+"' ,"+
	                      "'"+line_data['remote_user']+"'"+
	                      ")")
	conn.commit()
	
	#pprint(line_data)
	#if line_num > 0:
	#	break
	line_num = line_num + 1

conn.close()

@@ -1034,17 +1034,16 @@
 o logs %22
-+
 %0A%09      
@@ -1144,17 +1144,16 @@
  user) %22
-+
 %0A%09      
@@ -1177,39 +1177,71 @@
 ES (
-%22+str(line_num)+%22 ,%22+%0A%09        
+?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)%22,%0A%09          (str(line_num),%0A%09
     
@@ -1239,39 +1239,32 @@
 m),%0A%09           
-   %22'%22+
 line_data%5B'remot
@@ -1275,43 +1275,22 @@
 st'%5D
-+%22' ,%22+%0A%09                      %22'%22+
+,%0A%09           
 line
@@ -1309,34 +1309,19 @@
 st_url'%5D
-+%22' ,%22+%0A%09         
+,%0A%09
         
@@ -1315,38 +1315,32 @@
 '%5D,%0A%09           
-  %22 %22+
 line_data%5B'statu
@@ -1342,31 +1342,19 @@
 status'%5D
-+%22 ,%22+%0A%09       
+,%0A%09
         
@@ -1360,16 +1360,8 @@
     
-    %22 %22+
 line
@@ -1391,42 +1391,22 @@
 lf'%5D
-+%22 ,%22+%0A%09                      %22'%22+
+,%0A%09           
 line
@@ -1428,23 +1428,17 @@
 method'%5D
-+%22' ,%22+
+,
 %0A%09      
@@ -1438,39 +1438,24 @@
 %09           
- 
-          %22'%22+
 line_data%5B'r
@@ -1473,39 +1473,27 @@
 er_referer'%5D
-+%22' ,%22+%0A%09      
+,%0A%09
             
@@ -1487,33 +1487,24 @@
 %09           
-     %22'%22+
 line_data%5B'r
@@ -1529,23 +1529,17 @@
 _agent'%5D
-+%22' ,%22+
+,
 %0A%09      
@@ -1539,39 +1539,24 @@
 %09           
-           %22'%22+
 line_data%5B'r
@@ -1572,33 +1572,19 @@
 tp_ver'%5D
-+%22' ,%22+%0A%09        
+,%0A%09
         
@@ -1578,39 +1578,32 @@
 '%5D,%0A%09           
-   %22'%22+
 line_data%5B'time_
@@ -1616,43 +1616,22 @@
 ed'%5D
-+%22' ,%22+%0A%09                      %22'%22+
+,%0A%09           
 line
@@ -1654,13 +1654,9 @@
 er'%5D
-+%22'%22+
+)
 %0A%09  
@@ -1667,23 +1667,8 @@
     
-            %22)%22
 )%0A%09c
@@ -1679,17 +1679,19 @@
 commit()
-%0A
+%09%0A%09
 %09%0A%09#ppri

Use bind variables for query