Skip to content

A Python script to extract database schemas from .DBF files and generate SQLite-compatible SQL statements.

License

Notifications You must be signed in to change notification settings

DevaanshPathak/DBFtoTXT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

DBF Schema Extractor

This project extracts the database schema from .DBF files in a specified folder and generates corresponding SQL CREATE TABLE statements. The extracted schemas are written to a text file (dbf_schemas.txt) for further use or reference.

Features

  • Parses .DBF files to read field names, types, and lengths.
  • Maps DBF field types to SQLite-compatible data types.
  • Generates SQL CREATE TABLE statements for all .DBF files in the specified folder.

Requirements

  • Python: Version 3.12
  • DBFread: Version 2.0.7

Installation

  1. Install Python 3.12 from the official Python website: https://www.python.org/downloads/.
  2. Install the DBFread library:
    pip install dbfread==2.0.7

Usage

  1. Clone or download the project to your local machine.
  2. Update the dbf_folder variable in the script with the path to the folder containing your .DBF files.
  3. Run the script:
    python extract_dbf_schemas.py
  4. The output file, dbf_schemas.txt, will be generated in the same directory as the script.

Code Overview

The script:

  1. Reads all .DBF files in the specified folder.
  2. Converts each file's schema to an SQL CREATE TABLE statement using the field mapping function.
  3. Writes the SQL statements to a text file.

Mapping DBF to SQLite Types
The map_field_type function handles the mapping of DBF field types to SQLite-compatible data types:

  • C: TEXT(length)
  • N: INTEGER (for lengths ≤ 10) or REAL
  • F: REAL
  • D: DATE
  • L: BOOLEAN
  • M: TEXT
  • Others: Default to TEXT

Example Output

For a .DBF file named example.DBF with the following fields:

  • ID (Numeric, Length: 5)
  • Name (Character, Length: 50)
  • Birthdate (Date)

The generated schema will look like:

CREATE TABLE example (
    ID INTEGER,
    Name TEXT(50),
    Birthdate DATE
);

Contributing

Feel free to fork the repository and submit pull requests for enhancements or bug fixes.


License

This project is licensed under the MIT License.


Support

For issues or questions, please open an issue in the repository or contact at devaanshpathak08@gmail.com

About

A Python script to extract database schemas from .DBF files and generate SQLite-compatible SQL statements.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages