{"record":{"id":"be74684d66e1b21c","repo":"litedb-org/LiteDB","slug":"database-not-connected-be7468","errorCode":null,"errorMessage":"Database not connected","messagePattern":"Database not connected","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LiteDB.Shell/Shell/ShellProgram.cs","lineNumber":39,"sourceCode":"            while (input.Running)\n            {\n                // read next command from user or queue\n                var cmd = input.ReadCommand();\n\n                if (string.IsNullOrEmpty(cmd)) continue;\n\n                try\n                {\n                    var scmd = GetCommand(cmd);\n\n                    if (scmd != null)\n                    {\n                        scmd(env);\n                        continue;\n                    }\n\n                    // if string is not a shell command, try execute as sql command\n                    if (env.Database == null) throw new Exception(\"Database not connected\");\n\n                    env.Running = true;\n\n                    display.WriteResult(env.Database.Execute(cmd), env);\n\n                }\n                catch (Exception ex)\n                {\n                    display.WriteError(ex);\n                }\n            }\n        }\n\n        #region Shell Commands\n\n        private static readonly List<IShellCommand> _commands = new List<IShellCommand>();\n\n        static ShellProgram()","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB.Shell/Shell/ShellProgram.cs#L21-L57","documentation":"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.","triggerScenarios":"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.","commonSituations":"Interactive shell session begun without connecting; a connect command failed; piping SQL into a shell instance that was started without a database argument.","solutions":["Connect first with 'open <file>' so env.Database is populated.","If you intended a shell command, check the spelling/syntax (the router did not match it).","Launch the shell with the database path argument to auto-connect on startup."],"exampleFix":"// before\n> SELECT * FROM customers\n// after\n> open MyData.db\n> SELECT * FROM customers","handlingStrategy":"validation","validationCode":"if (env.Database == null)\n{\n    env.Display.WriteLine(ConsoleColor.Yellow, $\"Not a shell command and no database open: '{cmd}'.\");\n    continue;\n}","typeGuard":"static bool HasDatabase(Env env) => env?.Database != null;","tryCatchPattern":null,"preventionTips":["Open a database before typing SQL in the shell.","Confirm the command name is spelled correctly if you intended a shell command.","Auto-connect by launching the shell with the database path."],"tags":["shell","connection","sql-execution"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}