55See the file 'doc/COPYING' for copying permission
66"""
77
8+ import sqlite3
9+
810from extra .safe2bin .safe2bin import safechardecode
911from lib .core .common import unsafeSQLIdentificatorNaming
12+ from lib .core .exception import sqlmapGenericException
1013from lib .core .exception import sqlmapMissingDependence
1114from lib .core .exception import sqlmapValueException
1215
@@ -17,12 +20,6 @@ class Replication:
1720 """
1821
1922 def __init__ (self , dbpath ):
20- try :
21- import sqlite3
22- except ImportError :
23- errMsg = "missing module 'sqlite3' needed by switch '--replicate'"
24- raise sqlmapMissingDependence , errMsg
25-
2623 self .dbpath = dbpath
2724 self .connection = sqlite3 .connect (dbpath )
2825 self .connection .isolation_level = None
@@ -53,54 +50,57 @@ def __init__(self, parent, name, columns=None, create=True, typeless=False):
5350 self .name = unsafeSQLIdentificatorNaming (name )
5451 self .columns = columns
5552 if create :
56- self .parent . cursor . execute ('DROP TABLE IF EXISTS "%s"' % self .name )
53+ self .execute ('DROP TABLE IF EXISTS "%s"' % self .name )
5754 if not typeless :
58- self .parent . cursor . execute ('CREATE TABLE "%s" (%s)' % (self .name , ',' .join ('"%s" %s' % (unsafeSQLIdentificatorNaming (colname ), coltype ) for colname , coltype in self .columns )))
55+ self .execute ('CREATE TABLE "%s" (%s)' % (self .name , ',' .join ('"%s" %s' % (unsafeSQLIdentificatorNaming (colname ), coltype ) for colname , coltype in self .columns )))
5956 else :
60- self .parent . cursor . execute ('CREATE TABLE "%s" (%s)' % (self .name , ',' .join ('"%s"' % unsafeSQLIdentificatorNaming (colname ) for colname in self .columns )))
57+ self .execute ('CREATE TABLE "%s" (%s)' % (self .name , ',' .join ('"%s"' % unsafeSQLIdentificatorNaming (colname ) for colname in self .columns )))
6158
6259 def insert (self , values ):
6360 """
6461 This function is used for inserting row(s) into current table.
6562 """
6663
6764 if len (values ) == len (self .columns ):
68- self .parent . cursor . execute ('INSERT INTO "%s" VALUES (%s)' % (self .name , ',' .join (['?' ]* len (values ))), safechardecode (values ))
65+ self .execute ('INSERT INTO "%s" VALUES (%s)' % (self .name , ',' .join (['?' ]* len (values ))), safechardecode (values ))
6966 else :
7067 errMsg = "wrong number of columns used in replicating insert"
7168 raise sqlmapValueException , errMsg
7269
70+ def execute (self , sql , parameters = []):
71+ try :
72+ self .parent .cursor .execute (sql , parameters )
73+ except sqlite3 .OperationalError , ex :
74+ errMsg = "problem occurred ('%s') while accessing sqlite database " % ex
75+ errMsg += "located at '%s'. Please make sure that " % self .parent .dbpath
76+ errMsg += "it's not used by some other program"
77+ raise sqlmapGenericException , errMsg
78+
7379 def beginTransaction (self ):
7480 """
7581 Great speed improvement can be gained by using explicit transactions around multiple inserts.
7682 Reference: http://stackoverflow.com/questions/4719836/python-and-sqlite3-adding-thousands-of-rows
7783 """
78- self .parent . cursor . execute ('BEGIN TRANSACTION' )
84+ self .execute ('BEGIN TRANSACTION' )
7985
8086 def endTransaction (self ):
81- self .parent . cursor . execute ('END TRANSACTION' )
87+ self .execute ('END TRANSACTION' )
8288
8389 def select (self , condition = None ):
8490 """
8591 This function is used for selecting row(s) from current table.
8692 """
87- stmt = 'SELECT * FROM %s' % self .name
93+ _ = 'SELECT * FROM %s' % self .name
8894 if condition :
89- stmt += 'WHERE %s' % condition
90- return self .parent . cursor . execute (stmt )
95+ _ += 'WHERE %s' % condition
96+ return self .execute (_ )
9197
9298 def createTable (self , tblname , columns = None , typeless = False ):
9399 """
94100 This function creates Table instance with current connection settings.
95101 """
96102 return Replication .Table (parent = self , name = tblname , columns = columns , typeless = typeless )
97103
98- def dropTable (self , tblname ):
99- """
100- This function drops table with given name using current connection.
101- """
102- self .cursor .execute ('DROP TABLE IF EXISTS %s' % tblname )
103-
104104 def __del__ (self ):
105105 self .cursor .close ()
106106 self .connection .close ()
0 commit comments