2

I'm trying to run the sqlite.exe tool as a process in my c# code in order to read a sql from a file.

If I run the sqLite3 tool in powershell then it works fine (sqlite3.exe "mydatabase.db" ".read mySql.sql")

But when I run the sqlite3 tool from my c# code as a process, then nothing happens to mydatabase.db. It's still 0b when sqlite3 terminates. I get no error message, the output from the sqlite3.exe is an empty string and the exit code is 1 (verified in the exit event). Does anyone have a clue why the database.db why the records in the .sql file is not added to the .db file?.

using (Process pProcess = new Process())
        {
            pProcess.StartInfo.FileName = sqlLite3ExePath;
            pProcess.StartInfo.Arguments = $"\"{sqLitePath2}\" \".read {sqlPath}\"";;
            pProcess.StartInfo.UseShellExecute = false;
            pProcess.StartInfo.RedirectStandardOutput = true;
            pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;//System.Diagnostics.ProcessWindowStyle.Hidden;
            pProcess.StartInfo.CreateNoWindow = false;//true; //not diplay a windows
            pProcess.EnableRaisingEvents = true;
            pProcess.Exited += PProcess_Exited;
            pProcess.Start();
            string output = pProcess.StandardOutput.ReadToEnd(); //The output result
            pProcess.WaitForExit();

            Debug.WriteLine(output);
        }
7
  • Please share a minimal reproducible example. Commented Jul 18, 2020 at 0:07
  • You have what looks to be a very important line commented out. Commented Jul 18, 2020 at 0:46
  • 1
    Try reading the StandardError to end instead - if the tool has encountered errors and pumped messages they may not be in standard output, but in the error stream. Note of course you'll need to redirectstandarderror= true Commented Jul 18, 2020 at 4:44
  • Why don't you just load the script into your app and execute it using the c# SQLite driver, like you're gonna be doing for everything else? Commented Jul 18, 2020 at 4:45
  • @Shawn, your right. I commented that line to check if I the standard output stream worked as it should, and I forgot to remove the // before posting. Commented Jul 18, 2020 at 12:41

1 Answer 1

1

As CaiusJard said in the comments, an error was passed in the error stream. Adding the following lines told me that my path was wrong.

pProcess.StartInfo.RedirectStandardError = true;
string error = pProcess.StandardError.ReadToEnd(); //The output result

The path divider "\" was removed since the path was parsed twice. Once setting the argument, and once when it was read by the tool. Replacing "\" with "/" in my paths did the trick

Sign up to request clarification or add additional context in comments.

Comments

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.