简体   繁体   English

如何使用PowerShell导入CSV文件?

[英]How to import CSV file using PowerShell?

I want to import a CSV File (comma delimited with double quote) into SQLite using PowerShell script.我想使用 PowerShell 脚本将 CSV 文件(用双引号分隔的逗号)导入到 SQLite 中。 I tried:我试过了:

echo ".import export.csv mytable" | sqlite3.exe

I get this error:我收到此错误:

export.csv:327: unescaped " character export.csv:327: 未转义的 " 字符

I get this error for all lines.我对所有行都收到此错误。 On SQLite's command line shell same command works:在 SQLite 的命令行 shell 上,相同的命令有效:

sqlite > .import export.csv mytable

How can I make this command work using a PowerShell script?如何使用 PowerShell 脚本使此命令起作用?

what about sqlite3.exe ".import export.csv mytable". sqlite3.exe“.import export.csv mytable”怎么样。

You can check the documentation of sqlite3.exe to use it not being in its shell, how to pass parameter to sqlite3.exe.您可以查看 sqlite3.exe 的文档以使用它不在其外壳中,如何将参数传递给 sqlite3.exe。

You can try below line你可以试试下面的线

Invoke-Expression 'sqlite3 yourdatabase.db < ".import export.csv mytable"'

None of the solutions listed on this page work.此页面上列出的所有解决方案均无效。 However, I managed to figure it out after a bit of trial-and-error.但是,经过一些反复试验,我设法弄清楚了。 I haven't seen this solution posted anywhere, so posting it here so that it hopefully helps someone else who stumbled upon this page via Google like I did.我还没有在任何地方看到这个解决方案,所以把它张贴在这里,希望它可以帮助像我一样通过谷歌偶然发现这个页面的其他人。

$params = @"
.mode csv
.separator ,
.import export.csv mytable
"@

$params | .\sqlite3.exe mydatabase.db

This works for me in both PowerShell 5.1 and v7.0.这在 PowerShell 5.1 和 v7.0 中都适用于我。

The following single command line works in both以下单个命令行适用于两者

  • cmd.exe (version 10.0.xx via ver ) and cmd.exe (版本 10.0.xx 通过ver )和
  • powershell.exe (version 5.1.xx via $PSVersionTable ) powershell.exe (版本 5.1.xx 通过$PSVersionTable
.\sqlite3.exe my.db ".import file1.csv table1 --csv"

This loads the contents of csv file file1.csv into table table1 within the sqlite database file my.db .这会将 csv 文件file1.csv的内容加载到 sqlite 数据库文件my.db中的表table1中。 The --csv flag will import the file and create the table structure based on the header column names in that file, and will approximately infer the data types. --csv标志将导入文件并根据该文件中的 header 列名创建表结构,并将大致推断数据类型。

You can import multiple files this way.您可以通过这种方式导入多个文件。 ie IE

.\sqlite3.exe my.db ".import file1.csv table1 --csv" ".import file2.csv table2 --csv"

You can chain commands together to immediately open the database for querying by appending ; .\sqlite3.exe my.db您可以将命令链接在一起,通过附加立即打开数据库进行查询; .\sqlite3.exe my.db ; .\sqlite3.exe my.db . ; .\sqlite3.exe my.db

ie IE

.\sqlite3.exe my.db ".import file1.csv table1 --csv" ".import file2.csv table2 --csv; .\sqlite3.exe my.db"

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

相关问题 如何使用PowerShell导入CSV并从文本文件中读取一行? - How to Import a CSV and read a line from text file using PowerShell? Powershell | 导入.csv文件 - Powershell | Import .csv file 如何使用 PowerShell 导入 CSV,它不是 CSV 文件,文件到 SQL 数据库 - How to import a CSV, which is not a CSV file, file into SQL database using PowerShell 如何使用powershell import-csv将csv数据导入sql - How to import csv data into sql using powershell import-csv 如何使用 PowerShell 中的 Import-csv 处理 CSV 文件的动态列 - How to handle dynamic columns for CSV file using Import-csv in PowerShell Powershell 使用 TransferText 导入 CSV 文件 - Powershell import CSV file with TransferText 在 powershell 中导入和导出 csv 文件 - import and export csv file in powershell 有谁知道如何使用Powershell将csv文件导入到Azure应用设置(网络登台)中? - Does anyone know how to import a csv file into Azure app settings (web staging) using powershell? 使用 PowerShell 从 CSV 文件导入和解析特定数据 - Import and parse specific data from CSV-file by using PowerShell 如何使用Powershell CSV或文本文件将多个USERS访问权限导入多个邮箱 - How to import multiple USERS access-rights to multiple Mailboxes using Powershell CSV or Text File
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM