Skip to main content
Clarify its use
Source Link

SQLite3 sets PRAGMA FOREIGN_KEYS = OFF by default. This is, of course, an invitation to problems.

I know that I can turn on the PRAGMA permanently by placing the command in ~/.sqliterc, but the problem is that it would affect every invocation of SQLite3, not just my own. That could lead to unexpected results in other installed scripts. Thus, I want to avoid using ~/.sqliterc.

Note that I'm using SQLite3 in a Bash script (not typing from the terminal).

One way to solve this is to remember to add the PRAGMA in every invocation of SQLite3 where it's required, e.g.

sqlite3 -cmd 'PRAGMA FOREIGN_KEYS = ON' example.db <<-END_CMD ... END_CMD 

(I can find other ways that are significantly messier.)

The problem with this is that it's easy to forget, especially when maintaining older code.

So, is there a way to "temporarily-permanently" turn it on, by which I mean to turn it on for the entire duration of a script? I was wondering, for example, if it's possible to put it into an environment variable, e.g.

export PRAGMA_FOREIGN_KEYS=ON # This doesn't work. 

or something like that.

Is there a way to do this, or is using -cmd the simplest way?

SQLite3 sets PRAGMA FOREIGN_KEYS = OFF by default. This is, of course, an invitation to problems.

I know that I can turn on the PRAGMA permanently by placing the command in ~/.sqliterc, but the problem is that it would affect every invocation of SQLite3, not just my own. That could lead to unexpected results in other installed scripts. Thus, I want to avoid using ~/.sqliterc.

Note that I'm using SQLite3 in Bash.

One way to solve this is to remember to add the PRAGMA in every invocation of SQLite3 where it's required, e.g.

sqlite3 -cmd 'PRAGMA FOREIGN_KEYS = ON' example.db <<-END_CMD ... END_CMD 

(I can find other ways that are significantly messier.)

The problem with this is that it's easy to forget, especially when maintaining older code.

So, is there a way to "temporarily-permanently" turn it on, by which I mean to turn it on for the entire duration of a script? I was wondering, for example, if it's possible to put it into an environment variable, e.g.

export PRAGMA_FOREIGN_KEYS=ON # This doesn't work. 

or something like that.

Is there a way to do this, or is using -cmd the simplest way?

SQLite3 sets PRAGMA FOREIGN_KEYS = OFF by default. This is, of course, an invitation to problems.

I know that I can turn on the PRAGMA permanently by placing the command in ~/.sqliterc, but the problem is that it would affect every invocation of SQLite3, not just my own. That could lead to unexpected results in other installed scripts. Thus, I want to avoid using ~/.sqliterc.

Note that I'm using SQLite3 in a Bash script (not typing from the terminal).

One way to solve this is to remember to add the PRAGMA in every invocation of SQLite3 where it's required, e.g.

sqlite3 -cmd 'PRAGMA FOREIGN_KEYS = ON' example.db <<-END_CMD ... END_CMD 

(I can find other ways that are significantly messier.)

The problem with this is that it's easy to forget, especially when maintaining older code.

So, is there a way to "temporarily-permanently" turn it on, by which I mean to turn it on for the entire duration of a script? I was wondering, for example, if it's possible to put it into an environment variable, e.g.

export PRAGMA_FOREIGN_KEYS=ON # This doesn't work. 

or something like that.

Is there a way to do this, or is using -cmd the simplest way?

Source Link

Can I pass a PRAGMA to sqlite3 using an environment variable (Bash)?

SQLite3 sets PRAGMA FOREIGN_KEYS = OFF by default. This is, of course, an invitation to problems.

I know that I can turn on the PRAGMA permanently by placing the command in ~/.sqliterc, but the problem is that it would affect every invocation of SQLite3, not just my own. That could lead to unexpected results in other installed scripts. Thus, I want to avoid using ~/.sqliterc.

Note that I'm using SQLite3 in Bash.

One way to solve this is to remember to add the PRAGMA in every invocation of SQLite3 where it's required, e.g.

sqlite3 -cmd 'PRAGMA FOREIGN_KEYS = ON' example.db <<-END_CMD ... END_CMD 

(I can find other ways that are significantly messier.)

The problem with this is that it's easy to forget, especially when maintaining older code.

So, is there a way to "temporarily-permanently" turn it on, by which I mean to turn it on for the entire duration of a script? I was wondering, for example, if it's possible to put it into an environment variable, e.g.

export PRAGMA_FOREIGN_KEYS=ON # This doesn't work. 

or something like that.

Is there a way to do this, or is using -cmd the simplest way?