简体   繁体   中英

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.

The output I get after executing of the code:

执行代码后输出

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?"

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.

The manual shows a -init file argument that should be more appropriate for what you want to achieve.

Try to add your meta-commands ( .mode csv , .import x ...) in a separate myFile , and execute sqlite3 -init myFile and it should work.


Aside from this main question, I see potential problems with your code:

You are opening a connection to a database with 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.

if you are using Process.Start() you don't need a bat file, you can directly call sqlite3 with the required arguments. (Though you will probably need the full path of sqlite3 for this to work)

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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