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;
}
相关推荐
quweiie2 分钟前
thinkphp8.0链接SQL SERVER2022数据库
数据库·sqlserver·thinkphp
Databend1 小时前
如何打造AI时代的数据基石 | Databend Meetup 上海站
数据库
老华带你飞1 小时前
海产品销售系统|海鲜商城购物|基于SprinBoot+vue的海鲜商城系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·海鲜商城购物系统
合作小小程序员小小店1 小时前
桌面开发,在线%超市销售管理%系统,基于vs2022,c#,winform,sql server数据
开发语言·数据库·microsoft·c#
SelectDB1 小时前
字节跳动:Apache Doris + AI 一站式融合数据引擎的探索与实践
数据库·apache
a***13142 小时前
vscode配置django环境并创建django项目(全图文操作)
vscode·django·sqlite
IvorySQL2 小时前
PostgreSQL 18 - 时间约束 (Temporal Constraints)
数据库·postgresql·开源
q***61412 小时前
从MySQL迁移到PostgreSQL的完整指南
数据库·mysql·postgresql
N***73853 小时前
SQL锁机制
java·数据库·sql
小羊在奋斗3 小时前
MySQL表的约束:从基础到核心(附场景+案例)
android·数据库·mysql