Search code examples
kdb+q-lang

How to export every table to csv in a kdb+ database?


Assume my kdb+ database has a few tables. How can I export all tables to csv files where the name of each csv is same as the table name?


Solution

  • There may be a number of ways to approach this, one solution could be:

     q)t1:([]a:1 2 3;b:1 2 3)
     q)t2:([]a:1 2 3;b:1 2 3;c:1 2 3)
     q){save `$(string x),".csv"} each tables[]
       `:t1.csv`:t2.csv
    

    ref: http://code.kx.com/q/ref/filewords/#save

    If you wish to specify the directory of the file being saved down then you could enhance the function like so:

    q){[dir;tSym] save ` sv dir,(`$ raze string tSym,`.csv)}[`:C:/Users/dhughes1/Documents;] each tables[]
      `:C:/Users/dhughes1/Documents/t1.csv`:C:/Users/dhughes1/Documents/t2.csv