litedb-org/LiteDB · error · Exception

Database not connected

Error message

Database not connected

What it means

Thrown by the LiteDB shell main loop when a typed line is neither a recognized shell command nor can be executed as SQL because no database is connected. After the command router finds no matching shell command, it falls back to executing the line as SQL via env.Database.Execute, which requires env.Database to be set.

Source

Thrown at LiteDB.Shell/Shell/ShellProgram.cs:39

            while (input.Running)
            {
                // read next command from user or queue
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd)) continue;

                try
                {
                    var scmd = GetCommand(cmd);

                    if (scmd != null)
                    {
                        scmd(env);
                        continue;
                    }

                    // if string is not a shell command, try execute as sql command
                    if (env.Database == null) throw new Exception("Database not connected");

                    env.Running = true;

                    display.WriteResult(env.Database.Execute(cmd), env);

                }
                catch (Exception ex)
                {
                    display.WriteError(ex);
                }
            }
        }

        #region Shell Commands

        private static readonly List<IShellCommand> _commands = new List<IShellCommand>();

        static ShellProgram()

View on GitHub (pinned to f906a5f850)

Solutions

  1. Connect first with 'open <file>' so env.Database is populated.
  2. If you intended a shell command, check the spelling/syntax (the router did not match it).
  3. Launch the shell with the database path argument to auto-connect on startup.

Example fix

// before
> SELECT * FROM customers
// after
> open MyData.db
> SELECT * FROM customers
Defensive patterns

Strategy: validation

Validate before calling

if (env.Database == null)
{
    env.Display.WriteLine(ConsoleColor.Yellow, $"Not a shell command and no database open: '{cmd}'.");
    continue;
}

Type guard

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

Prevention

When it happens

Trigger: Entering any SQL statement (e.g. 'SELECT * FROM customers') or unrecognized text in the shell before a database is open. Since it is not a known shell command and env.Database is null, the guard fires.

Common situations: Interactive shell session begun without connecting; a connect command failed; piping SQL into a shell instance that was started without a database argument.

Related errors


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