Skip to content

z-kk/csv2dbsrc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

csv2db nimble

create db util source from csv

How to use csv2dbsrc

Use in Nim project directory

first, exec csv2dbsrc

$ csv2dbsrc
select database type
1: sqlite
2: mysql
>> 1
input database file name
>> test.db
make database table csv referencing path/to/sample.csv

then copy and modify sample.csv in same directory.
the csv file name should be the table name.

sample.csv

name comment data_type default_val not_null is_primary
id user id INTEGER 1 1
user_name user name TEXT 1
address TEXT
tel TEXT '000-0000-0000'
group_id user's group id INTEGER 1 1
updated_at updated date time DATETIME '9999-12-31' 1

then exec csv2dbsrc again to make dbtables.nim and each tableName.nim file.

Made files usage

import dbtables in your own source.

openDb*

proc openDb*(): DbConn =

createTables*

proc createTables*(db: DbConn) =

exec createTableNameTable each tables.

type

TableNameCol* = enum

table column names.

TableNameTable* = object

table object.

setDataTableNameTable*

proc setDataTableNameTable*(data: var TableNameTable, colName, value: string) =

set value to TableNameTable object by string.

createTableNameTable*

proc createTableNameTable*(db: DbConn) =

exec create table if not exists table_name

tryInsertTableNameTable*

proc tryInsertTableNameTable*(db: DbConn, rowData: TestTableTable): int64 =

exec insert into table_name and return last_insert_rowid.

insertTableNameTable*

proc insertTableNameTable*(db: DbConn, rowData: TableNameTable) =

exec insert into table_name

insertTableNameTable*

proc insertTableNameTable*(db: DbConn, rowDataSeq: seq[TableNameTable]) =

exec insert into table_name for each rowData.

selectTableNameTable*

proc selectTableNameTable*(db: DbConn, whereStr = "", orderBy: seq[string], whereVals: varargs[string, `$`]): seq[TableNameTable] =

exec select * from table_name and return the result.

selectTableNameTable*

proc selectTestTableTable*(db: DbConn, whereStr = "", whereVals: varargs[string, `$`]): seq[TestTableTable] =

exec select * from table_name without order and return the result.

updateTableNameTable*

proc updateTableNameTable*(db: DbConn, rowData: TableNameTable) =

exec update table_name
rowData must be got by selectTableNameTable and table must have primary key.

updateTableNameTable*

proc updateTableNameTable*(db: DbConn, rowDataSeq: seq[TableNameTable]) =

exec update table_name for each rowData.

dumpTableNameTable*

proc dumpTableNameTable*(db: DbConn, dirName = "csv") =

make db table data csv.

insertCsvTableNameTable*

proc insertCsvTableNameTable*(db: DbConn, fileName: string) =

insert rows from fileName csv. this proc not check exists rows.

restoreTableNameTable*

proc restoreTableNameTable*(db: DbConn, dirName = "csv") =

drop table and make table by data csv.

Pull Request please

Test for mysql is not enough.

If you can use Japanese, please make pull request in Japanese!

About

create db table from csv

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages