0

I have a file containing sql commands and I want to execute the commands given in the file and if any command throws any error then ignore the error and execute the next command. Here is a sample file:

drop table department;
drop table classroom;

create table classroom
    (building       varchar(15),
     room_number        varchar(7),
     capacity       numeric(4,0),
     primary key (building, room_number)
    );

create table department
    (dept_name      varchar(20), 
     building       varchar(15), 
     budget             numeric(12,2) check (budget > 0),
     primary key (dept_name)
    );

For eg. if the classroom table doesn't exists then the drop table command will produce an error and the program will terminate. I want that the program keep running and execute all commands in the file.

The problem I'm facing is that the create table command is in multiple lines so I don't know how to execute that.

10
  • Read the entire file into a string, use.. I think it's the executescript method on the sqlite connection object? Commented Oct 22, 2020 at 18:32
  • Why are you using VARCHAR with specified length while it's ignored by sqlite? Commented Oct 22, 2020 at 18:33
  • @OlvinRoght I didn't create this file. I'm currently learning sql in my university and this is a practice problem but I couldn't figure out how to do it. Commented Oct 22, 2020 at 18:37
  • @Shawn What if there is an error while executing the sql? Commented Oct 22, 2020 at 18:38
  • Then you catch it and do something in response? Commented Oct 22, 2020 at 18:47

2 Answers 2

1

Take a look at the sqlite3 manual:

import sqlite3
con = sqlite3.connect(":memory:")
cur = con.cursor()
with open('PATH_TO_SQL_FILE', 'r') as fp:
    text = fp.read().split(';')
    for command in text:
       try:
           cur.execute(command)
       except sqlite3.Error:
           pass
Sign up to request clarification or add additional context in comments.

7 Comments

How will this handle errors? Please read the question carefully.
Oh my bet. Is this what you are looking for: stackoverflow.com/questions/36816640/…
No, this is not what I want to do.
I updated the answer. can you elaborate why this is not what you want to do?
Take a look at the comments under the question.
|
0
import sqlite3

#define connection and cursor
connection = sqlite3.connect ('test_db')
cursor =connection.cursor()


command1 ="""create table classroom
    (building       varchar(15),
    room_number        varchar(7),
    capacity       numeric(4,0),
    primary key (building, room_number)
    )"""

cursor.execute(command1)


command2 ="""create table department
    (dept_name      varchar(20), 
    building       varchar(15), 
    budget             numeric(12,2) check (budget > 0),
    primary key (dept_name)
    )"""
cursor.execute(command2)

5 Comments

if you want to add more sql statements then you need to add more commands (command3,command4 etc)
this will only work for the sample file. I want it to work for any file. Please read the question carefully.
I don't have any information about the file I want to take the file as input and then execute the sql commands given in it.
Also I think it will be difficult if you do not have any information about the file...For example if you want to drop a table that does not exist yet...I expect an error message...
Yes there will be an error message and I want to catch the error and then execute the next command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.