Fahd Jerbi
Fahd Jerbi

Reputation: 1

How to create a schema with tables in a single query with PyQT (using Postgres database)?

I'm building a GUI containing a button that creates a database schema with tables, I read the docs in PyQt about QSqlDatabase and QSqlQuery but it looks like it just runs the first line creating the schema without tables:

        db = QSqlDatabase().addDatabase("QPSQL")
        db.setHostName("localhost")
        db.setDatabaseName("my_db")
        db.setUserName("postgres")
        db.setPassword("0000")
        db.open()

        my_schema = "schema1"
        query = QSqlQuery()
        query.exec_(
            """
            CREATE SCHEMA IF NOT EXISTS {my_schema};
            SET search_path TO {my_schema};
            CREATE TABLE pt (
                    id SERIAL PRIMARY KEY NOT NULL,
                    name VARCHAR(5),
                    geom geometry(Point, 2532)
            );
            """.format(
                my_schema=my_schema
            )
        )

        err = query.lastError().databaseText()

        print("a schema has been created successfully !")
        print(err)

I tried to split the query and to put the query in separate SQL file but it didn't work.

Upvotes: 0

Views: 46

Answers (0)

Related Questions