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;
}
相关推荐
YMatrix 官方技术社区8 小时前
YMatrix 存储引擎解密:MARS3 存储引擎如何超越传统行存、列存实现“时序+分析“场景性能大幅提升?
开发语言·数据库·时序数据库·数据库架构·智慧工厂·存储引擎·ymatrix
辞砚技术录9 小时前
MySQL面试题——索引2nd
数据库·mysql·面试
linweidong9 小时前
C++thread pool(线程池)设计应关注哪些扩展性问题?
java·数据库·c++
欧亚学术10 小时前
突发!刚刚新增17本期刊被剔除!
数据库·论文·sci·期刊·博士·scopus·发表
黑白极客10 小时前
怎么给字符串字段加索引?日志系统 一条更新语句是怎么执行的
java·数据库·sql·mysql·引擎
大厂技术总监下海11 小时前
数据湖加速、实时数仓、统一查询层:Apache Doris 如何成为现代数据架构的“高性能中枢”?
大数据·数据库·算法·apache
LeenixP11 小时前
RK3576-Debian12删除userdata分区
linux·运维·服务器·数据库·debian·开发板
知行合一。。。11 小时前
Python--03--函数入门
android·数据库·python
X***078811 小时前
理解 MySQL 的索引设计逻辑:从数据结构到实际查询性能的系统分析
数据库·mysql·sqlite
爬山算法11 小时前
Hibernate(31)Hibernate的原生SQL查询是什么?
数据库·sql·hibernate