$sqliteCommand = ".open '$fullPathToSqliteDatabase' # there can't be any spaces before the new line and the dot (.) or else sqlite throws an exception # Name of the database table to insert into. # Full path to the sqlite database you want to use or create. Add them to a script you've created or save them as modules. It uses the same code that we used above. Here are two Powershell functions that handle importing and exporting CSV data with a SQLite database. Select count(*), substring(emailaddress, instr(emailaddress, from email1 group by substring(emailaddress, instr(emailaddress, Powershell Select x.emailaddress from email1 x where x.emailaddress in (select y.emailaddress from email2 y) Then execute your sql statement and it will be written to disk.headers on (".output" without any parameters switches back to standard output.) ".output duplicates.csv" writes the output to a file. ".headers on" adds the column name to the output. While we're here let's also find out how many different domains are represented in our list of email addresses: select count(*), substring(emailaddress, instr(emailaddress, from email1 group by substring(emailaddress, instr(emailaddress, Exporting as a CSVĮxporting to a CSV file is as simple as importing a CSV file. Now we can find duplicate email addresses with an easy sql query: select x.emailaddress from email1 x where x.emailaddress in (select y.emailaddress from email2 y) sqlite3Īnd here's what it looks like in my command line: Run SQL Queries ".schema" tells us the structure of the tables that were created, which used the first row of the csv file as the column name. Called "dot commands", statements that start with a period are executed by sqlite3.exe and we use them to:Ģ) Tell the program to expect a CSV file (.mode csv), thenģ) Import the CSV data into a new table (.import email1.csv email1). By default SQLite will create an in-memory database but by issuing an "open" command a new database will be created. Open a command prompt in your SQLite directory. Download the "bundle of command line tools" for your operating system from the downloads page.Įxport both spreadsheets as CSV files and put them in the same directory as the SQLite exes you downloaded - it's just easier to reference in our examples without having to use fully-qualified paths. Import the Data into SQLiteįirst you need to download SQLite. Which email addresses exist in both? We'll figure it out quickly with SQLite. Here are two spreadsheets with fake data in them. It's even easy to script using Powershell. It's very popular and is often used as a file format. SQLite is fast, self-contained, lightweight, and is file-based. Stop looking at Excel formula reference websites and instead use the SQL statements that you already know and love. I find it easier to export my spreadsheets as CSV files, import them into SQLite, and run regular old sql queries. But I'm ditching it in favor of plain old sql. It's finicky and I feel like it never works right. I'm often asked to find rows in one list that aren't in another list (or some similar task) and I spend too much time with vlookup as a result.
0 Comments
Leave a Reply. |