Skip to content

Lesson 6 ยท Reading and Writing Data

Databases and Writing Data in KNIME

Connect KNIME to a database, push filtering and grouping into SQL with DB nodes, read results, and save output to CSV, Excel, table files, or database tables.

Files are only half the story. Business data often lives in databases, and results need to go somewhere useful. This lesson covers KNIME's database workflow and its writer nodes.

The database idea in KNIME

The database nodes are included with KNIME Analytics Platform. A database workflow starts with a connector node, which creates a database session. There are dedicated connectors for systems such as PostgreSQL and MySQL, a file-based connector for databases like SQLite, and a generic DB Connector for any database with a JDBC driver.

After connecting, you can pick a table with DB Table Selector and build on it with nodes whose names start with "DB". These nodes do not pull data into KNIME right away. They build a SQL query step by step. The query runs and the rows travel to your computer when a DB Reader node executes.

flowchart LR
  A["SQLite Connector"] --> B["DB Table Selector"]
  B --> C["DB Row Filter"]
  C --> D["DB GroupBy"]
  D --> E["DB Reader"]
  E --> F["KNIME table"]

Only the final reader brings the result back as an ordinary KNIME table.

Why work inside the database

If a table has ten million rows but you need totals for one month, reading all of them to your laptop wastes time and memory. Filtering and grouping in the database sends back a small result. This is the key rule: do the heavy reduction where the data lives.

The stand-in below is a real SQL query that does what a filter node plus a group node would build. You can see why only a few rows come back.

SQL Stand-in: the SQL that a DB filter and group would build

Press Run (or Ctrl + Enter) to see the result here.

The SQL that KNIME generates will differ in style and depends on your database. You can inspect the generated query in the node's output when you need to. For SQL fundamentals, see the SQL tutorial.

Custom queries

When visual nodes are not enough, you can write your own SQL, for example with the DB Query Reader node. Keep queries readable and test them on small data first. If a value in your query comes from a variable, be careful: text pasted into SQL can change what the query does. Prefer the parameter features KNIME provides, and never build SQL from untrusted input.

Writing back to a database

KNIME can create tables and insert, update, merge, or delete rows. Nodes such as DB Writer, DB Insert, and related ones cover these tasks, and some databases support bulk loading for large volumes. You can also group changes into a transaction so they either all succeed or are all rolled back.

Writing is where mistakes become expensive. Test against a copy or a test schema, check the number of rows that will be affected, and use an account with only the permissions the workflow needs. Never run a delete or update against production data without review.

When you are done, you can close the session with a DB Connection Closer node. Also avoid adding several connector nodes to the same database when one can be shared.

Writing files

For files, KNIME offers writer nodes:

Node Best for
CSV Writer Plain text exchange with other tools
Excel Writer Reports for people who use spreadsheets, including adding sheets
Table Writer Saving KNIME tables with their column types

Before saving, ask: can this file be overwritten? Is the file name clear? Does it contain personal data that should not be copied around? A name that includes the period, such as revenue-2026-11.xlsx, is easier to audit than final2.xlsx.

Verify what you wrote

After writing, read the file or table back and compare row counts and totals. This is a cheap test that catches truncation, wrong delimiters, or a partial load.

Python Stand-in: write CSV, read it back, compare

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Connecting safely

A connector node needs an address, a database name, and a way to prove who you are. Typing a password directly into a node can store it with the workflow. Prefer supplying credentials when the workflow runs, using credential configuration nodes or whatever secret management your organization provides. Ask your database administrator for an account made for this purpose, with read-only rights unless the workflow must write.

For a database that has no dedicated connector, the generic connector needs a JDBC driver. Drivers can be registered in KNIME or installed as described in the database guide. Download drivers only from the database vendor or another source your organization trusts.

Types cross a boundary

Databases and KNIME have different type systems. A decimal with many digits, a timestamp with a time zone, or a long text field may arrive as a different type than you expect. The database extension has type mapping settings for this. After the first read, inspect the types and compare a few values with what a database tool shows. Pay special attention to money, identifiers with leading zeros, and timestamps near midnight, where a time zone shift can move a record to a different day.

Transactions in plain words

Imagine a workflow that deletes last month's summary rows and then inserts new ones. If the insert fails after the delete has succeeded, the table is left empty for that month. A transaction groups both steps so that either both take effect or neither does. Use one whenever a partial result would be harmful, and test the failure case on purpose with a copy of the data.

Choosing an output

Ask who reads the result and what they will do with it. A finance team may want a workbook. Another program may want a database table with a stable schema. A later KNIME workflow may prefer the table format, which preserves column types. A stable column list is a promise to whoever consumes the output, so change it deliberately and tell them first.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. When does data usually travel from the database into KNIME in a chain of DB nodes?

    DB nodes build a query; DB Reader runs it and returns rows.
  2. 2. Why filter in the database instead of after reading everything?

    Pushing work to the database avoids moving unneeded rows.
  3. 3. What is a safe first step before a workflow updates database rows?

    Testing and checking affected rows protects real data.

Summary

  • A connector node creates a database session; DB nodes build SQL.
  • DB Reader runs the query and returns an ordinary table.
  • Reduce data in the database, and be careful with custom SQL and writes.
  • Use CSV, Excel, or Table Writer depending on who reads the result.
  • Read outputs back and compare counts and totals.

Next: Filtering Rows and Columns

Your progress is saved in this browser. No account needed.