• Welcome, Guest. Please login.
 
May 26, 2019, 12:59:03 am

News:

Welcome to the SQLitening support forums!


Array to disk

Started by cj, March 18, 2014, 07:19:05 pm

Previous topic - Next topic

cj

March 18, 2014, 07:19:05 pm Last Edit: April 09, 2014, 02:08:49 pm by cj
Sometimes it is nice to be able to view or email results to someone.
This takes a recordset which is in an array and saves to a file and
optionally shells to the output file.  I use .TXT so NOTEPAD views it.

#INCLUDE "sqlitening.inc"   'ArrayToDisk.bas
#INCLUDE "win32api.inc"

FUNCTION PBMAIN ()
  LOCAL sArray() AS STRING  'Q9c would not include element 0, column names
  slOpen "sample.db3"
  slSelAry "select rowid,product from parts order by rowid desc", sArray(), "Q9"
  ArrayToDisk sArray(), "JUNK.TXT", 1
END FUNCTION

FUNCTION ArrayToDisk(sArray() AS STRING, sFileName AS STRING, OPTIONAL ViewFile AS LONG) AS LONG
  LOCAL hFile AS LONG
  hFile = FREEFILE
  OPEN sFileName FOR OUTPUT AS #hFile
  IF ERR THEN FUNCTION = ERR:BEEP:EXIT FUNCTION
  PRINT #hFile, sArray()
  IF ERR THEN BEEP:FUNCTION = ERR
  CLOSE #hFile
  IF ERR = 0 AND ISMISSING(ViewFile) = 0 THEN
     SLEEP 500
     ShellIt sFileName
  END IF
END FUNCTION

SUB Shellit(sFileName AS STRING)
  ShellExecute BYVAL 0, "OPEN", sFileName+$NUL, BYVAL 0, $NUL, BYVAL 1
END SUB

cj

April 08, 2014, 12:39:30 pm #1 Last Edit: April 09, 2014, 02:07:36 pm by cj
This version views output using NOTEPAD with ShellIt filename or
WORDPAD by calling with ShellIt filename, -1

#INCLUDE "sqlitening.inc"   'ArrayToDisk.bas

FUNCTION PBMAIN ()
  LOCAL sArray() AS STRING  'Q9c would not include element 0, column names
  slOpen "sample.db3"
  slSelAry "select rowid,product from parts order by rowid desc", sArray(), "Q9"
  ArrayToDisk sArray(), "JUNK.TXT", 1 '1= also view file
END FUNCTION

FUNCTION ArrayToDisk(sArray() AS STRING, sFileName AS STRING, OPTIONAL ViewFile AS LONG) AS LONG
  LOCAL hFile AS LONG
  hFile = FREEFILE
  OPEN sFileName FOR OUTPUT AS #hFile
  IF ERR THEN FUNCTION = ERR:BEEP:EXIT FUNCTION
  PRINT #hFile, sArray()
  IF ERR THEN BEEP:FUNCTION = ERR
  CLOSE #hFile
  IF ERR = 0 AND ISMISSING(ViewFile) = 0 THEN
     SLEEP 500
     Shellit sFileName,-1 'pass a second value to use wordpad instead of notepad
     shellit sFilename    'use notepad
  END IF
END FUNCTION

SUB ShellIt(sFileName AS STRING, OPTIONAL WordPad AS LONG)
  LOCAL x AS LONG
  IF ISMISSING(WordPad) THEN
    x = SHELL("notepad.exe " + sFileName)
  ELSE
    x = SHELL("c:\program files\windows nt\accessories\wordpad.exe " + sFileName)
  END IF
END SUB