litedb-org/LiteDB · error · Exception

Database not connected

Error message

Database not connected

What it means

Thrown by the LiteDB shell 'run' command when no database is open. The Run command reads a script file and queues its lines for execution, but executing any queued SQL requires an active connection, so env.Database must be set first via an 'open' or 'connect' command.

Source

Thrown at LiteDB.Shell/Commands/Run.cs:23

{
    [Help(
        Name = "run",
        Syntax = "run <filename>",
        Description = "Queue shell commands inside filename to be run in order.",
        Examples = new string[] {
            "run scripts.txt"
        }
    )]
    internal class Run : IShellCommand
    {
        public bool IsCommand(StringScanner s)
        {
            return s.Scan(@"run\s+").Length > 0;
        }

        public void Execute(StringScanner s, Env env)
        {
            if (env.Database == null) throw new Exception("Database not connected");

            var filename = s.Scan(@".+").Trim();

            foreach (var line in File.ReadAllLines(filename))
            {
                env.Input.Queue.Enqueue(line);
            }
        }
    }
}

View on GitHub (pinned to f906a5f850)

Solutions

  1. Run the connect/open command first, e.g. 'open MyData.db', then 'run scripts.txt'.
  2. Verify the open command succeeded (no error written) before invoking run.
  3. Pass the database file as a shell startup argument so the connection is established before commands are read.

Example fix

// before
> run scripts.txt
// after
> open MyData.db
> run scripts.txt
Defensive patterns

Strategy: validation

Validate before calling

// Guard the run command when scripting the shell
if (env.Database == null)
{
    env.Display.WriteLine(ConsoleColor.Yellow, "No database open. Run: open <file>");
    return;
}

Type guard

static bool HasDatabase(Env env) => env?.Database != null;

Prevention

When it happens

Trigger: Typing 'run scripts.txt' in the LiteDB shell before opening a database with 'open <file>' (or equivalent connect command). The shell's env.Database is null until a connection command populates it.

Common situations: Starting a shell session and immediately running a script without connecting; the connection string from a prior 'open' failed silently so env.Database stayed null; scripting the shell in a non-interactive pipeline that omits the connect step.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/0cfcf13dd9b78365. Report an issue: GitHub.