예제 #1
0
파일: db.py 프로젝트: holycrepe/anknotes
def execute_sqlite_query(sql, data, in_query=False, **kwargs):
    queries = generate_sqlite_in_predicate(data, **kwargs) if in_query else generate_sqlite_predicate(data, **kwargs)
    results = []
    db = ankDB()
    for query, data in queries:
        sql = fmt(sql, pred=query)
        result = db.list(sql, *data)
        log_sql('FROM execute_sqlite_query ' + sql,
                ['Data [%d]: ' % len(data), data,result[:3]])
        results += result
    return results
예제 #2
0
파일: db.py 프로젝트: holycrepe/anknotes
 def executemany(self, sql, data, **kw):
     sql = self._create_query_(sql, **kw)
     log_sql(sql, data, self=self)
     self.mod = True
     t = time.time()
     try:
         self._db.executemany(sql, data)
     except (sqlite.OperationalError, sqlite.ProgrammingError, sqlite.Error, Exception) as e:
         log_sql(sql, data, self=self, filter_disabled=False)
         import traceback
         log_error('Error with ankDB().executemany(): %s\n Query: %s\n Trace: %s' % (str(e), sql, traceback.format_exc()))
         raise
     if self.echo:
         print sql, "%0.3fms" % ((time.time() - t) * 1000)
         if self.echo == "2":
             print data
예제 #3
0
파일: db.py 프로젝트: holycrepe/anknotes
 def execute(self, sql, a=None, kw=None, auto=None, **kwargs):
     if is_dict_type(a):
         kw, a = a, kw
     if not is_seq_type(a):
         a = item_to_list(a)
     if is_dict_type(sql):
         auto = sql
         sql = ' AND '.join(["`{0}` = :{0}".format(key) for key in auto.keys()])
     if kw is None:
         kw = {}
     kwargs.update(kw)
     sql = self._create_query_(sql, **kwargs)
     if auto:
         kw = auto
     log_sql(sql, a, kw, self=self)
     self.ankdb_lastquery = sql
     if self._is_stmt_(sql):
         self.mod = True
     t = time.time()
     try:
         if a:
             # execute("...where id = ?", 5)
             res = self._db.execute(sql, a)
         elif kw:
             # execute("...where id = :id", id=5)
             res = self._db.execute(sql, kw)
         else:
             res = self._db.execute(sql)
     except (sqlite.OperationalError, sqlite.ProgrammingError, sqlite.Error, Exception) as e:
         log_sql(sql, a, kw, self=self, filter_disabled=False)
         import traceback
         log_error('Error with ankDB().execute(): %s\n Query: %s\n Trace: %s' %
                   (str(e), sql, traceback.format_exc()))
         raise
     if self.echo:
         # print a, ka
         print sql, "%0.3fms" % ((time.time() - t) * 1000)
         if self.echo == "2":
             print a, kw
     return res