diff --git a/django/core/cache/backends/db.py b/django/core/cache/backends/db.py
index acbe702255..ec616d7e31 100644
a
|
b
|
class DatabaseCache(BaseDatabaseCache):
|
243 | 243 | |
244 | 244 | with connection.cursor() as cursor: |
245 | 245 | cursor.execute( |
246 | | 'SELECT %s FROM %s WHERE %s = %%s and expires > %%s' % ( |
| 246 | 'SELECT %s FROM %s WHERE %s = %%s and %s > %%s' % ( |
247 | 247 | quote_name('cache_key'), |
248 | 248 | quote_name(self._table), |
249 | 249 | quote_name('cache_key'), |
| 250 | quote_name('expires'), |
250 | 251 | ), |
251 | 252 | [key, connection.ops.adapt_datetimefield_value(now)] |
252 | 253 | ) |
… |
… |
class DatabaseCache(BaseDatabaseCache):
|
257 | 258 | self.clear() |
258 | 259 | else: |
259 | 260 | connection = connections[db] |
260 | | table = connection.ops.quote_name(self._table) |
261 | | cursor.execute("DELETE FROM %s WHERE expires < %%s" % table, |
262 | | [connection.ops.adapt_datetimefield_value(now)]) |
| 261 | quote_name = connection.ops.quote_name |
| 262 | table = quote_name(self._table) |
| 263 | cursor.execute( |
| 264 | 'DELETE FROM %s WHERE %s < %%s' % ( |
| 265 | table, |
| 266 | quote_name('expires'), |
| 267 | ), |
| 268 | [connection.ops.adapt_datetimefield_value(now)] |
| 269 | ) |
263 | 270 | cursor.execute("SELECT COUNT(*) FROM %s" % table) |
264 | 271 | num = cursor.fetchone()[0] |
265 | 272 | if num > self._max_entries: |
… |
… |
class DatabaseCache(BaseDatabaseCache):
|
270 | 277 | last_cache_key = cursor.fetchone() |
271 | 278 | if last_cache_key: |
272 | 279 | cursor.execute( |
273 | | 'DELETE FROM %s WHERE cache_key < %%s' % table, |
| 280 | 'DELETE FROM %s WHERE %s < %%s' % ( |
| 281 | table, |
| 282 | quote_name('cache_key'), |
| 283 | ), |
274 | 284 | [last_cache_key[0]], |
275 | 285 | ) |
276 | 286 | |
diff --git a/django/db/backends/base/operations.py b/django/db/backends/base/operations.py
index 0fcc607bcf..d7a4472483 100644
a
|
b
|
class BaseDatabaseOperations:
|
82 | 82 | This is used by the 'db' cache backend to determine where to start |
83 | 83 | culling. |
84 | 84 | """ |
85 | | return "SELECT cache_key FROM %s ORDER BY cache_key LIMIT 1 OFFSET %%s" |
| 85 | cache_key = self.ops.quote_name('cache_key') |
| 86 | return f"SELECT {cache_key} FROM %s ORDER BY {cache_key} LIMIT 1 OFFSET %%s" |
86 | 87 | |
87 | 88 | def unification_cast_sql(self, output_field): |
88 | 89 | """ |