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;
}
相关推荐
草莓熊Lotso29 分钟前
Linux 文件描述符与重定向实战:从原理到 minishell 实现
android·linux·运维·服务器·数据库·c++·人工智能
大模型玩家七七33 分钟前
基于语义切分 vs 基于结构切分的实际差异
java·开发语言·数据库·安全·batch
岳麓丹枫0012 小时前
PostgreSQL 中 pg_wal 目录里的 .ready .done .history 文件的生命周期
数据库·postgresql
陌上丨8 小时前
Redis的Key和Value的设计原则有哪些?
数据库·redis·缓存
AI_56788 小时前
AWS EC2新手入门:6步带你从零启动实例
大数据·数据库·人工智能·机器学习·aws
ccecw8 小时前
Mysql ONLY_FULL_GROUP_BY模式详解、group by非查询字段报错
数据库·mysql
JH30738 小时前
达梦数据库与MySQL的核心差异解析:从特性到实践
数据库·mysql
数据知道9 小时前
PostgreSQL 核心原理:如何利用多核 CPU 加速大数据量扫描(并行查询)
数据库·postgresql
麦聪聊数据10 小时前
Web 原生架构如何重塑企业级数据库协作流?
数据库·sql·低代码·架构
未来之窗软件服务10 小时前
数据库优化提速(四)新加坡房产系统开发数据库表结构—仙盟创梦IDE
数据库·数据库优化·计算机软考