Import a `.del` file (a delimited file, exported from DB2) into SQLite

It is possible to import a .del file (a delimited file, such as one exported from DB2) into SQLite, but you might need to process the file depending on its format. SQLite supports importing data from CSV files, so the key is to convert your .del file into a format SQLite can understand, such as a standard CSV.

Steps to Import DB2 .del File into SQLite:

  1. Inspect the .del File Format:

    • Check the delimiter used in the .del file (commonly |, \t, or another character).
    • Verify if the file includes a header row with column names.
    • Look for any special encoding or escape characters.
  2. Convert .del to CSV:

    • If the .del file uses a delimiter other than a comma (,), replace the delimiter with a comma. You can use tools like sed, awk, or Python scripts.

    • Example using sed (assuming | as the delimiter):

      bash 复制代码
      sed 's/|/,/g' T_db_table.del > T_db_table.csv
    • Ensure all fields are properly quoted if necessary (e.g., strings with commas).

  3. Create the SQLite Table:

    • Manually define the schema in SQLite to match the structure of your .del file.

    • Example:

      sql 复制代码
      CREATE TABLE T_db_table (
          column1 TEXT,
          column2 INTEGER,
          column3 REAL
          -- Add more columns as needed
      );
  4. Import the Converted File into SQLite:

    • Use the .import command in the SQLite CLI:

      bash 复制代码
      sqlite3 your_database.db
      .mode csv
      .import T_db_table.csv T_db_table
    • If the file has a header row, SQLite will skip it automatically. If it doesn't, ensure the .csv file matches the order of columns in your table.

  5. Verify the Data:

    • Query the table to ensure the data was imported correctly:

      sql 复制代码
      SELECT * FROM T_db_table LIMIT 10;
  6. Automate if Necessary:

    • If you have multiple files, write a script to automate the conversion and import process.

Considerations for Migration to ARM Environment:

  • Data Validation: Ensure the data types in SQLite align with the original DB2 schema to avoid truncation or conversion errors.
  • Encoding : Confirm the .del file encoding (e.g., UTF-8) matches the expected encoding for SQLite.
  • Index and Constraints: Recreate any indexes or constraints from DB2 in SQLite for testing purposes.

When migrating from DB2 to SQLite, column types don't have a one-to-one match because the systems have different type systems. However, I can provide a general mapping guide based on common DB2 types and their closest equivalents in SQLite.

General Column Type Mapping from DB2 to SQLite:

DB2 Type SQLite Type Explanation
CHAR(n) TEXT CHAR(n) is a fixed-length string in DB2. SQLite stores it as a variable-length string (TEXT).
VARCHAR(n) TEXT VARCHAR(n) is a variable-length string, similar to TEXT in SQLite.
CLOB TEXT CLOB (Character Large Object) in DB2 is treated as TEXT in SQLite.
INTEGER INTEGER Both DB2 and SQLite use INTEGER for integers.
BIGINT INTEGER SQLite stores large integers as INTEGER.
DECIMAL(p, s) REAL DECIMAL is stored as a floating-point number (REAL) in SQLite.
NUMERIC(p, s) REAL NUMERIC in DB2 is typically mapped to REAL in SQLite.
FLOAT, DOUBLE REAL Both FLOAT and DOUBLE are mapped to REAL in SQLite.
DATE TEXT SQLite stores dates as TEXT in the ISO 8601 format (YYYY-MM-DD).
TIME TEXT TIME is also stored as TEXT in SQLite (HH:MM:SS).
TIMESTAMP TEXT TIMESTAMP in DB2 is stored as TEXT in SQLite (YYYY-MM-DD HH:MM:SS).
BINARY(n) BLOB BINARY is mapped to BLOB (Binary Large Object) in SQLite.
VARBINARY(n) BLOB VARBINARY is also mapped to BLOB in SQLite.
BOOLEAN INTEGER SQLite uses INTEGER for boolean values, where 0 is false and 1 is true.

Example Mapping for Your Table:

If your DB2 table includes columns like CHAR(2) or VARCHAR(512), you could map them to TEXT in SQLite, as both types store strings.

For instance:

  • CHAR(2) in DB2 → TEXT in SQLite (Fixed-length string)
  • VARCHAR(512) in DB2 → TEXT in SQLite (Variable-length string)

Example DB2 to SQLite Mapping:

If you have a DB2 table like this:

sql 复制代码
CREATE TABLE T_db_table(
    column1 CHAR(2),      -- Fixed-length string
    column2 VARCHAR(512), -- Variable-length string
    column3 DECIMAL(10,2),-- Decimal number
    column4 INTEGER,      -- Integer
    column5 DATE          -- Date
);

You could map it to SQLite like this:

sql 复制代码
CREATE TABLE T_db_table (
    column1 TEXT,         -- CHAR(2) mapped to TEXT
    column2 TEXT,         -- VARCHAR(512) mapped to TEXT
    column3 REAL,         -- DECIMAL(10,2) mapped to REAL
    column4 INTEGER,      -- INTEGER stays INTEGER
    column5 TEXT          -- DATE mapped to TEXT (ISO format)
);

Some Additional Considerations:

  1. Precision : If you are working with DECIMAL or NUMERIC types in DB2 with specified precision and scale, SQLite doesn't enforce the precision, so it treats them as REAL. This can result in rounding or precision issues if you need high precision.

  2. Date/Time: SQLite stores date and time values as text in ISO 8601 format, which is a string representation. Make sure you format the date/time correctly before importing if it's not already in that format.

  3. String Length : In SQLite, TEXT columns are flexible and don't have a length constraint (unlike CHAR(n) or VARCHAR(n) in DB2). So, there's no need to worry about setting a column length in SQLite.

UPDATE t_db_table SET custtype=TRIM(custtype), TYPE=TRIM(type);

Makefile

bash 复制代码
CC = gcc
CFLAGS = -Wall -g
LDFLAGS = -lsqlite3
TARGET = sqlite_query_demo

all: $(TARGET)

$(TARGET): sqlite_query_demo.c
	$(CC) $(CFLAGS) -o $(TARGET) sqlite_query_demo.c $(LDFLAGS)

clean:
	rm -f $(TARGET) *.o

// sqlite_query_demo.c

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>

void execute_query(sqlite3 *db, const char *custno, const char *type) {
    const char *sql_template = "SELECT * FROM T_db_table WHERE custtype='1' AND custno=? AND type=?";
    sqlite3_stmt *stmt;
    int rc;

    // Prepare the SQL statement
    rc = sqlite3_prepare_v2(db, sql_template, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
        return;
    }

    // Bind the parameters
    sqlite3_bind_text(stmt, 1, custno, -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 2, type, -1, SQLITE_STATIC);

    // Execute the query and print the results
    printf("Results:\n");
    while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
        int columns = sqlite3_column_count(stmt);
        for (int i = 0; i < columns; i++) {
            printf("%s = %s\t", sqlite3_column_name(stmt, i), sqlite3_column_text(stmt, i));
        }
        printf("\n");
    }

    if (rc != SQLITE_DONE) {
        fprintf(stderr, "Error while executing statement: %s\n", sqlite3_errmsg(db));
    }

    // Finalize the statement
    sqlite3_finalize(stmt);
}

int main(int argc, char *argv[]) {
    if (argc != 4) {
        fprintf(stderr, "Usage: %s <database> <custno> <type>\n", argv[0]);
        return EXIT_FAILURE;
    }

    const char *db_path = argv[1];
    const char *custno = argv[2];
    const char *type = argv[3];

    sqlite3 *db;
    int rc;

    // Open the database
    rc = sqlite3_open(db_path, &db);
    if (rc) {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        return EXIT_FAILURE;
    }

    execute_query(db, custno, type);

    // Close the database
    sqlite3_close(db);

    return EXIT_SUCCESS;
}
相关推荐
极限实验室18 分钟前
APM(一):Skywalking 与 Easyearch 集成
数据库·云原生
饕餮争锋24 分钟前
SQL条件中WHERE 1=1 的功能
数据库·sql
玄斎1 小时前
MySQL 单表操作通关指南:建库 / 建表 / 插入 / 增删改查
运维·服务器·数据库·学习·程序人生·mysql·oracle
编织幻境的妖1 小时前
SQL查询连续登录用户方法详解
java·数据库·sql
编程小Y2 小时前
MySQL 与 MCP 集成全解析(核心原理 + 实战步骤 + 应用场景)
数据库·mysql·adb
零度@2 小时前
SQL 调优全解:从 20 秒到 200 ms 的 6 步实战笔记(附脚本)
数据库·笔记·sql
Miss_Chenzr2 小时前
Springboot优卖电商系统s7zmj(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
lvbinemail3 小时前
Grafana模板自动复制图表
数据库·mysql·zabbix·grafana·监控
Miss_Chenzr3 小时前
Springboot旅游景区管理系统9fu3n(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·旅游
小虾米vivian3 小时前
dmetl5 运行失败,提示违反协议?
数据库·达梦数据库