简体   繁体   English

我正在使用 System.Diagnostics.Process 运行 sqlite 特殊命令,但只有一半的命令被执行

[英]I am using System.Diagnostics.Process to run the sqlite special command but only half of the commands get executed

string connectionString = @"Data Source=C:\sqlite\test.db; Version=3; FailIfMissing=True; Foreign Keys=True;";

SQLiteConnection conn = new SQLiteConnection(connectionString);          
conn.Open();

string batFilePath = @"D:\mockforbat.bat";

if (!File.Exists(batFilePath))
{
    using (FileStream fs = File.Create(batFilePath))
    {
        fs.Close();
    }
}

using (StreamWriter sw = new StreamWriter(batFilePath))
{
    sw.WriteLine(@"C:");
    sw.WriteLine(@"cd\");
    sw.WriteLine(@"cd sqlite");
    sw.WriteLine(@"sqlite3 test.db");
    sw.WriteLine(@".mode csv");
    sw.WriteLine(@".import D:/Ashif/SQLITE/Bulk.csv excelUpload"); 
}

Process process = Process.Start(batFilePath);
process.WaitForExit();  

When I execute this command, it will execute only up to the "sqlite3 test.db" line, and the other commands are not executed.当我执行这个命令时,它只会执行到“sqlite3 test.db”行,其他命令不执行。

The output I get after executing of the code:执行代码后得到的 output:

执行代码后输出

You didn't explicitly ask a question, so I assume the question is "how can I call the sqlite command line from my program with the relevant arguments?"您没有明确提出问题,所以我认为问题是“我如何使用相关的 arguments 从我的程序中调用 sqlite 命令行?”

The sqlite3 command line interface is interactive by default, but you probably don't want that when using it as part of a script/automated execution. sqlite3 命令行界面默认是交互式的,但在将它用作脚本/自动执行的一部分时,您可能不希望这样做。

The manual shows a -init file argument that should be more appropriate for what you want to achieve.手册显示了一个-init file参数,该参数应该更适合您想要实现的目标。

Try to add your meta-commands ( .mode csv , .import x ...) in a separate myFile , and execute sqlite3 -init myFile and it should work.尝试在单独的myFile中添加您的元命令( .mode csv.import x ...),然后执行sqlite3 -init myFile它应该可以工作。


Aside from this main question, I see potential problems with your code:除了这个主要问题,我发现您的代码存在潜在问题:

You are opening a connection to a database with new SQLiteConnection(connectionString);您正在使用new SQLiteConnection(connectionString); but you are never using this connection.但你从来没有使用过这个连接。 Instead, you are connecting again to your database by executing the sqlite3 test.db in your bat file.相反,您通过执行 bat 文件中的sqlite3 test.db再次连接到数据库。

if you are using Process.Start() you don't need a bat file, you can directly call sqlite3 with the required arguments.如果你使用Process.Start()你不需要一个 bat 文件,你可以直接用所需的 arguments 调用sqlite3 (Though you will probably need the full path of sqlite3 for this to work) (虽然你可能需要sqlite3的完整路径才能工作)

To sum up, you could use something like that (untested, adapt as needed):总而言之,您可以使用类似的东西(未经测试,根据需要进行调整):

            var sqliteScriptPath = @"D:\myScript.txt";
            var scriptContent = @"
.mode csv
.open test.db
.import D:/Ashif/SQLITE/Bulk.csv excelUpload";

            File.WriteAllText(sqliteScriptPath, scriptContent);

            Environment.CurrentDirectory = @"C:\sqlite"; // If you use relative paths somewhere in your command you will need something like that
            // You can also create a ProcessStartInfo instance, and set its WorkingDirectory property before giving it to Process.Start()

            Process process = Process.Start("sqlite3", $"-init {sqliteScriptPath}");
            process.WaitForExit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM