def query_assoc(self, SQL, data=None, force_double_array=False, silent=False): try: self.lock.acquire(True) self.DBH.row_factory = self.dict_factory cur = self.DBH.cursor() if data: cur.execute(SQL, data) else: cur.execute(SQL) rows = cur.fetchall() cur.close() if (len(rows) == 1 and not force_double_array): return rows[0] else: return rows except Exception, e: if 'no such table: version' not in str(e).lower(): plugin.raise_error("Database error", e) plugin.log("Database query error: %s" % e) plugin.log("Database query error: %s" % SQL) return []
def query_assoc(self, SQL, data=None, force_double_array=False, silent=False): self.check_connection() try: self.lock.acquire(True) if data: SQL = SQL.replace('?', '%s') self.DBC.execute(SQL, data) else: self.DBC.execute(SQL) rows = self.DBC.fetchall() if (len(rows) == 1 and not force_double_array): d = {} for idx, col in enumerate(self.DBC.column_names): d[col] = rows[0][idx] return d else: set = [] for row in rows: d = {} for idx, col in enumerate(self.DBC.column_names): d[col] = row[idx] set.append(d) return set except Exception, e: error_msg = "Database query error: %s" % e if silent is False: plugin.raise_error("Database error", e) plugin.log(error_msg) plugin.log("Database query error: %s" % SQL) plugin.log(data) return []
def execute_many(self, SQL, data, silent=False): if SQL.startswith('REPLACE INTO'): SQL = 'INSERT OR ' + SQL try: self.lock.acquire(True) self.DBC.executemany(SQL, data) except Exception, e: if IGNORE_UNIQUE_ERRORS and re.match(self._unique_str, str(e)): if silent is False: plugin.raise_error("Database error", e) plugin.log("Database execute error: %s" % e) plugin.log("Database execute error: %s" % SQL)
def execute_many(self, SQL, data, silent=False): self.check_connection() try: self.lock.acquire(True) SQL = SQL.replace('?', '%s') self.DBC.executemany(SQL, data) except Exception, e: if IGNORE_UNIQUE_ERRORS and re.match(self._unique_str, str(e)): if silent is False: plugin.raise_error("Database error", e) plugin.log("Database execute error: %s" % e) plugin.log("Database execute error: %s" % SQL)
def query(self, SQL, data=None, force_double_array=False, silent=False): try: self.lock.acquire(True) if data: self.DBC.execute(SQL, data) else: self.DBC.execute(SQL) rows = self.DBC.fetchall() if (len(rows) == 1 and not force_double_array): return rows[0] else: return rows except Exception, e: if 'no such table: version' not in str(e).lower(): plugin.raise_error("Database error", e) plugin.log("Database query error: %s" % e) plugin.log("Database query error: %s" % SQL) return []
def _connect(self, quiet=False): try: import mysql.connector as database if not self.quiet and not quiet: plugin.log("%s loading mysql.connector as DB engine" % ADDON_NAME) dsn = { "database": self.dbname, "host": self.host, "port": int(self.port), "user": str(self.username), "password": str(self.password), "buffered": True } self.DBH = database.connect(**dsn) except Exception, e: plugin.log('****** %s SQL ERROR: %s' % (ADDON_NAME, e)) plugin.raise_error("MySQL Error", e) sys.exit()
def query(self, SQL, data=None, force_double_array=False, silent=False): self.check_connection() try: self.lock.acquire(True) if data: SQL = SQL.replace('?', '%s') self.DBC.execute(SQL, data) else: self.DBC.execute(SQL) rows = self.DBC.fetchall() if (len(rows) == 1 and not force_double_array): return rows[0] else: return rows except Exception, e: error_msg = "Database query error: %s" % e if silent is False: plugin.raise_error("Database error", e) plugin.log(error_msg) plugin.log("Database query error: %s" % SQL) plugin.log(data) return []
def _connect(self, quiet=False): vfs = VFSClass() if not self.quiet and not quiet: plugin.log("Connecting to " + self.db_file, LOG_LEVEL.VERBOSE) try: from sqlite3 import dbapi2 as database if not self.quiet and not quiet: plugin.log("%s loading sqlite3 as DB engine" % ADDON_NAME) except: from pysqlite2 import dbapi2 as database if not self.quiet and not quiet: plugin.log("%s loading pysqlite2 as DB engine" % ADDON_NAME) if not self.quiet and not quiet: plugin.log("Connecting to SQLite on: " + self.db_file) directory = os.path.dirname(self.db_file) if not vfs.exists(directory): vfs.mkdir(directory) self.DBH = database.connect(self.db_file, check_same_thread=False) try: self.DBC = self.DBH.cursor() except Exception, e: plugin.raise_error("SqlLite Error", e) sys.exit()