0

I'm trying to create my tables with my Python code. I feel like I'm doing it right but when i execute my code, nothing is happening.

I tried the following code

commands.sql

DROP TABLE IF EXISTS text;

CREATE TABLE text(
    id_text SERIAL PRIMARY KEY,
    line_number VARCHAR(3),
    line_content VARCHAR(255)
);

CreateDB.py

from Utils import ConnectDB as ConnectDBFile


class CreateDB:

    conn = None
    cursor = None
    sql_path = "./commands.sql"
    array_sql_commands = []

    def __init__(self):
        self.conn = ConnectDBFile.ConnectDB.getInstance()
        self.cursor = self.conn.cursor()
        self.setSqlCommands()

    def setSqlCommands(self):
        self.array_sql_commands = open(self.sql_path).read().split(";")

    def createDatabase(self):
        for command in self.array_sql_commands:
            try:
                print("Running : " + command)
                self.cursor.execute(command)
            except :
                print("Command skipped")

RunCreation.py

from Utils import CreateDB

obj_create_db = CreateDB.CreateDB()
obj_create_db.createDatabase()

Someone knows what is wrong in my code ? Thanks!

EDIT : I'm trying to add manualy the ; because the split() delete it. Now, i've my sql commands ending with ; but nothing changes in the result.

BastienB
  • 180
  • 2
  • 14

0 Answers0