Sqlite from actions

Hello,

I need to read data from a sqlite database from inside the actiony.py file and return it as a message. Unfortunately, I always get an error message, saying the databse cannot be accessed. The sqlite database file is inside my actions/ folder.

Are there any recommendations on how to solve this?

Seems the path is wrong? Can you share a minimal example of your code?

Of course I can :slight_smile:

barcode = tracker.get_slot("barcode")

        try:
            BASE_DIR = os.path.dirname(os.path.abspath(__file__))
            db_path = os.path.join(BASE_DIR, "database.sqlite")
            sqliteConnection = sqlite3.connect(db_path)
            cursor = sqliteConnection.cursor()

            # Sqlite query ausfuehren
            query = "SELECT product_name FROM selections WHERE code={bc}".format(
                bc=barcode
            )
            cursor.execute(query)
            records = cursor.fetchall()

            print(records)
         ...

I tried sqlite3.connect( "database.sqlite") as well with the same result. My code always enters the catch block. The error message is no such table but the table does definitely exist.

Thanks for your help!

Hi @andileni, Here is how I performed a simple query in MySQL. Try this format. Be sure to put the DB connection code outside of the try-catch. Only the query operations should be in try-catch. I’m not sure if this will work for sqlite but do try it.

Here “varunsapre” is the username of the DB, “password” is the password for the user and “rasadb” is the database name.

import MySQLdb

Can you share the stack trace? And also the logs from your database?

Also, the title of this topic says sqlite and you’re using mysql?

@Tobias_Wochinger I am using sqite, verunsapre10 gave an example how he is doing it with mysql :slight_smile:

The problem is solved now. Unfortunately I cannot give a recommandation on what was wrong, because we don’t know either :frowning:

Probably a problem with a permission or a stopped and cached docker container.