|
| 1 | +--- |
| 2 | +updated: 2024-10-28 |
| 3 | +difficulty: Beginner |
| 4 | +content_type: Tutorial |
| 5 | +pcx_content_type: tutorial |
| 6 | +title: Import a CSV file to D1 using Wrangler CLI |
| 7 | +products: |
| 8 | + - D1 |
| 9 | +languages: |
| 10 | + - JavaScript |
| 11 | + - TypeScript |
| 12 | + - SQL |
| 13 | +--- |
| 14 | + |
| 15 | +import { Render, Steps } from "~/components"; |
| 16 | + |
| 17 | +In this tutorial, you will learn how to import a CSV file to D1 using the Wrangler CLI. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +<Render file="prereqs" product="workers" /> |
| 22 | + |
| 23 | +## 1. Install and Verify Wrangler |
| 24 | + |
| 25 | +You'll use Wrangler to interact with D1. If you don't have it installed, follow the steps in the [Wrangler installation guide](/workers/wrangler/install-and-update/). |
| 26 | + |
| 27 | +If you're unsure if Wrangler is installed, run the following command to verify: |
| 28 | + |
| 29 | +```bash |
| 30 | +wrangler --version |
| 31 | +``` |
| 32 | + |
| 33 | +## 2. Set up a D1 Database |
| 34 | + |
| 35 | +You can create a D1 database using the [Wrangler CLI](/workers/wrangler/install-and-update/) using the following command: |
| 36 | + |
| 37 | + ```bash |
| 38 | + # Create a D1 database |
| 39 | + npx wrangler d1 create d1-csv-tutorial |
| 40 | + ``` |
| 41 | + |
| 42 | +Edit or create your `wrangler.toml` file to include the following: |
| 43 | + |
| 44 | +```toml |
| 45 | +[[ d1_databases ]] |
| 46 | +binding = "DB" |
| 47 | +database_id = "your-database-id" |
| 48 | +database_name = "d1-csv-tutorial" |
| 49 | +``` |
| 50 | + |
| 51 | +## 3. Convert CSV to SQL |
| 52 | + |
| 53 | +D1 does not support importing CSV files directly. You need to convert your CSV to a SQL file. If you don't have a CSV file, you can use the NASA meteorite dataset from [here](https://data.nasa.gov/Space-Science/Meteorite-Landings/gh4g-9sfh/about_data). |
| 54 | + |
| 55 | +You'll be using SQLite to convert your CSV to a SQL file. For instructions on installing SQLite, refer to the [SQLite installation guide](https://www.sqlite.org/download.html). |
| 56 | + |
| 57 | +<Steps> |
| 58 | +1. Create a SQLite database. |
| 59 | + |
| 60 | + ```bash |
| 61 | + sqlite3 meteorites.db |
| 62 | + ``` |
| 63 | + |
| 64 | +2. Inside the SQLite shell, create a table. You're matching the column names to the column names in your meteorites CSV file. If you're using a different CSV file, make sure to match the table and column names to the data contained in your CSV file. |
| 65 | + |
| 66 | + ```sql |
| 67 | + CREATE TABLE meteorites ( |
| 68 | + name TEXT, |
| 69 | + id INTEGER PRIMARY KEY, |
| 70 | + nametype TEXT, |
| 71 | + recclass TEXT, |
| 72 | + mass REAL, |
| 73 | + fall TEXT, |
| 74 | + year INTEGER, |
| 75 | + reclat REAL, |
| 76 | + reclong REAL, |
| 77 | + GeoLocation TEXT |
| 78 | + ); |
| 79 | + ``` |
| 80 | + |
| 81 | +3. Import the CSV file into the database. |
| 82 | + |
| 83 | + ```sql |
| 84 | + .mode csv |
| 85 | + .import meteorites.csv meteorites |
| 86 | + .exit |
| 87 | + ``` |
| 88 | + |
| 89 | +4. Export the Database to a SQL file. |
| 90 | + |
| 91 | + ```bash |
| 92 | + sqlite3 -header -csv meteorites.db ".dump" > meteorites.sql |
| 93 | + ``` |
| 94 | + |
| 95 | +</Steps> |
| 96 | + |
| 97 | +## 4. Import SQL Data into Cloudflare D1 |
| 98 | + |
| 99 | +Now that you have your SQL file, you can import it into your D1 database using the following command: |
| 100 | + |
| 101 | +```bash |
| 102 | +npx wrangler d1 import d1-csv-tutorial meteorites.sql |
| 103 | +``` |
| 104 | + |
| 105 | +Once your import is complete, you can verify that the data was imported by running the following command: |
| 106 | + |
| 107 | +```bash |
| 108 | +npx wrangler d1 execute d1-csv-tutorial --command="SELECT COUNT(*) FROM meteorites;" |
| 109 | +``` |
| 110 | + |
| 111 | +Or you can preview the data by running the following command: |
| 112 | + |
| 113 | +```bash |
| 114 | +npx wrangler d1 execute d1-csv-tutorial --command="SELECT * FROM meteorites;" |
| 115 | +``` |
| 116 | + |
| 117 | +You will now see your target D1 table populated with the example data. |
| 118 | + |
| 119 | +## Summary |
| 120 | + |
| 121 | +By completing this tutorial, you have |
| 122 | + |
| 123 | +1. Set up a D1 database. |
| 124 | +2. Converted a CSV file to a SQL file. |
| 125 | +3. Imported the SQL file into a D1 database. |
| 126 | +4. Queried the data in the D1 database. |
| 127 | + |
| 128 | +## Next steps |
| 129 | + |
| 130 | +- Learn more about [D1](/d1/) |
| 131 | +- Learn more about [Wrangler](/workers/wrangler/) |
0 commit comments