Skip to content Skip to sidebar Skip to footer

How To Improve Sqlite Database Request And Speed

I found put that I open the database at every request.Is there a way to simplify and improve this code to increase the sqlite speed? name3 = ' '.join(name2) import

Solution 1:

  1. Do not use import in the middle of your program.
  2. Open the database once at the start of your program.
  3. Select only the records you actually need.
  4. Select only the columns you actually need.
  5. Do not use fetchall; read only the records you actually need.
  6. Do not fetch into a temporary variable if you can use the cursor directly.
import sqlite3

# at startup
conn = sqlite3.connect("keywords.db")

def search_location(name2):
    name3 = ' '.join(name2)
    c = conn.cursor()
    c.execute('SELECT location FROM kmedicals WHERE id = ?', (name3,))
    for (location,) in c:
        print name3.capitalize(),':' '\n',location
        break
    else:
        pass  # not found

Post a Comment for "How To Improve Sqlite Database Request And Speed"