IonicWind Software

IWBasic => Database => Topic started by: splakidas on April 05, 2007, 01:40:53 PM

Title: storing a excel sheet in an array
Post by: splakidas on April 05, 2007, 01:40:53 PM
need help to store a excel sheet ie 20x20 in an array myarray[20,20]   
Title: Re: storing a excel sheet in an array
Post by: REDEBOLT on April 05, 2007, 08:24:22 PM
Could you use ODBC to read the array?
Title: Re: storing a excel sheet in an array
Post by: splakidas on April 06, 2007, 04:46:21 AM
yes but i need help is there any working example ?
Title: Re: storing a excel sheet in an array
Post by: REDEBOLT on April 06, 2007, 08:19:12 AM
OK. Here goes.  ;D

Code and spreadsheet are also in the attached zip file.

'=============================================================================
' Using ODBC to access data in an Excel spreadsheet.
'=============================================================================
'=============================================================================
' Operations note:  I couldn't make the program read the first row of the spreadsheet.
'    So I inserted a dummy row of all zeroes at the beginning of the spreadsheet.
'    This is most likely unacceptable.
'=============================================================================
openconsole

string strConnection
string somepath,driver,filename,options,selct
pointer pDB, pTables, pColumns
int hstmt,i,j,k,l,m,n
int myarray[20,20]
'=============================================================================
' Change this path to your spreadsheet source.
'=============================================================================
somepath = "F:\\File Server\\Installs\\EMERGENCE BASIC\\EXCELODBC\\"

'=============================================================================
' DSN-less connection string example.
'=============================================================================
' http://students.ukdw.ac.id/~sonny/odbc_dsnless.html#ODBCDriverForExcel
'strConnection =  "Driver={Microsoft Excel Driver (*.xls)};" + _
'              "DriverId=790;" + _
'              "Dbq=" + somepath + "EXCELODBC.xls;" + _
'              "DefaultDir=" + somepath

'=============================================================================
' My Home-brewed DSN-less connection string example.
' Connect to the database.
'=============================================================================
driver = "Microsoft Excel Driver (*.xls)"
filename = somepath + "EXCELODBC.xls"
options = ""

pDB = dbConnect(driver,filename,options)

if pdb = NULL then
print "Database connection is not established."
goto eoj
endif

print "Database connection is established."
PRINT

'=============================================================================
' List the table names.
'=============================================================================
pTables = dbListTables(pdb)
PRINT "Tables:"
IF pTables <> NULL
    FOR temp = EACH pTables as STRING
        PRINT #temp
PRINT "Cardinality:",dbCardinality(pdb,#temp)
PRINT
    NEXT
    ListRemoveAll(pTables,TRUE)
ENDIF
PRINT

'=============================================================================
' List the column names.
'=============================================================================
pColumns = dbListColumns(pdb,"Sheet1$") ' List the column names.
PRINT "Columns:"
IF pColumns <> NULL
    FOR temp2 = EACH pColumns as STRING
        PRINT #temp2,", ",
    NEXT
    PRINT
    ListRemoveAll(pColumns,TRUE)
ENDIF
PRINT

'=============================================================================
' Pause so you can read the screen.
'=============================================================================
PRINT "Press any key to continue..."
do:until inkey$<>""
print

'=============================================================================
' Read the database.
'=============================================================================
selct = "SELECT * FROM [Sheet1$]"
hstmt = dbExecSQL(pdb,selct)

error = dbGetErrorCode(hstmt)
IF LEN(error)
    PRINT
    PRINT "Error Code: ", error
PRINT selct
    PRINT "Error Text: ", dbGetErrorText(hstmt)
    PRINT
GOTO FreeIt
ELSE
i=0:j=0
    WHILE dbGet(hstmt)
print "Row #:", i+1
j = 1
while dbGetData(hstmt, j, myarray[i,j-1])
print myarray[i,j-1],
j++
endwhile
i++
print
if (i % 7) = 0 then
print
PRINT "Press any key to continue..."
do:until inkey$<>""
print
endif
    ENDWHILE
ENDIF

'=============================================================================
' Verify beginning and ending rows of the array.
'=============================================================================
print "First row: ",myarray[0,0], myarray[0,19]
print " Last row: ",myarray[19,0], myarray[19,19]
print

'=============================================================================
' Close the database.
'=============================================================================
label FreeIt
dbFreeSQL(hstmt)
dbDisconnect(pDB)

'=============================================================================
' Final exit.
'=============================================================================
label eoj
print "Done"
PRINT "Press any key to exit..."
do:until inkey$<>""

closeconsole


Title: Re: storing a excel sheet in an array
Post by: splakidas on April 06, 2007, 03:06:49 PM
REDEBOLT  it works !!!! Thank you very much for your help
Title: Re: storing a excel sheet in an array
Post by: REDEBOLT on April 06, 2007, 07:39:51 PM
My pleasure!

8)
Title: Re: storing a excel sheet in an array
Post by: Haim on April 07, 2007, 12:46:46 AM
Very nice example!
It does not read the first line because it expects the fiirst line to be column titles and not data.

Haim
Title: Re: storing a excel sheet in an array
Post by: REDEBOLT on April 07, 2007, 01:57:26 AM
Thanks, Haim, for the clarification.
:)

P.S. Most of the code was copied from the help file.
Title: Re: storing a excel sheet in an array
Post by: splakidas on April 12, 2007, 03:50:48 PM
REDEBOLT

i modyfy it to read strings instead of numbers
string strConnection
string somepath,driver,filename,options,selct
pointer pDB, pTables, pColumns
int hstmt,i,j,k,l,m,n
string myarray[3,5]  <-----------------this
'=============================================================================
' Change this path to your spreadsheet source.


but i dont know, must it be string or istring ??

and i would like to know also how can i determine the table size x,y ??
because if i open a sheet i would like to know the size of the table and then insert all the strings in it

Thank you
Title: Re: storing a excel sheet in an array
Post by: splakidas on April 15, 2007, 11:03:48 AM
REDEBOLT
is it easy to to modify your sample to load strings ?
Title: Re: storing a excel sheet in an array
Post by: REDEBOLT on April 15, 2007, 05:07:19 PM
Do you know how long your maximum sized strings are? :)
Title: Re: storing a excel sheet in an array
Post by: REDEBOLT on April 15, 2007, 05:55:35 PM
Here goes:

'=============================================================================
' Using ODBC to access data in an Excel spreadsheet.
'=============================================================================
'=============================================================================
' Operations note:  I couldn't make the program read the first row of the spreadsheet.
'    So I inserted a dummy row of all zeroes at the beginning of the spreadsheet.
'    This is most likely unacceptable.
'=============================================================================
openconsole

string strConnection
string somepath,driver,filename,options,selct
pointer pDB, pTables, pColumns
int hstmt,i,j,k,l,m,n,nTables,nColumns
string myarray[20,3]
'=============================================================================
' Change this path to your spreadsheet source.
'=============================================================================
somepath = "C:\\EXCELODBC\\"

'=============================================================================
' DSN-less connection string example.
'=============================================================================
' http://students.ukdw.ac.id/~sonny/odbc_dsnless.html#ODBCDriverForExcel
'strConnection =  "Driver={Microsoft Excel Driver (*.xls)};" + _
'              "DriverId=790;" + _
'              "Dbq=" + somepath + "EXCELODBC.xls;" + _
'              "DefaultDir=" + somepath

'=============================================================================
' My Home-brewed DSN-less connection string example.
' Connect to the database.
'=============================================================================
driver = "Microsoft Excel Driver (*.xls)"
filename = somepath + "EXCELODBC3.xls"
options = ""

pDB = dbConnect(driver,filename,options)

if pdb = NULL then
print "Database connection is not established."
goto eoj
endif

print "Database connection is established."
PRINT

'=============================================================================
' Pause so you can read the screen.
'=============================================================================
PRINT "Press any key to continue..."
do:until inkey$<>""
print

'=============================================================================
' List the table names.
'=============================================================================
pTables = dbListTables(pdb)

PRINT "Tables:"
IF pTables <> NULL
    FOR temp = EACH pTables as STRING
        PRINT #temp
PRINT "Cardinality:",dbCardinality(pdb,#temp)
PRINT
nTables++ ' Count the tables.
    NEXT
    ListRemoveAll(pTables,TRUE)
print "Number of Tables = ",nTables
print
ENDIF
PRINT

'=============================================================================
' List the column names.
'=============================================================================
pColumns = dbListColumns(pdb,"Sheet1$") ' List the column names.
PRINT "Columns:"
IF pColumns <> NULL
    FOR temp2 = EACH pColumns as STRING
        PRINT #temp2,", ",
nColumns++ ' Count the columns.
    NEXT
    PRINT
print
print "Number of Columns = ",nColumns
print
    ListRemoveAll(pColumns,TRUE)
ENDIF
PRINT

'=============================================================================
' Pause so you can read the screen.
'=============================================================================
PRINT "Press any key to continue..."
do:until inkey$<>""
print

'=============================================================================
' List the column names.
'=============================================================================
pColumns = dbListColumns(pdb,"Sheet2$") ' List the column names.
nColumns = 0
PRINT "Columns:"
IF pColumns <> NULL
    FOR temp2 = EACH pColumns as STRING
        PRINT #temp2,", ",
nColumns++ ' Count the columns.
    NEXT
    PRINT
print
print "Number of Columns = ",nColumns
print
    ListRemoveAll(pColumns,TRUE)
ENDIF
PRINT

'=============================================================================
' Pause so you can read the screen.
'=============================================================================
PRINT "Press any key to continue..."
do:until inkey$<>""
print

'=============================================================================
' Read the database.
'=============================================================================
selct = "SELECT * FROM [Sheet2$]"
hstmt = dbExecSQL(pdb,selct)

error = dbGetErrorCode(hstmt)
IF LEN(error)
    PRINT
    PRINT "Error Code: ", error
PRINT selct
    PRINT "Error Text: ", dbGetErrorText(hstmt)
    PRINT
GOTO FreeIt
ELSE
i=0:j=0
    WHILE dbGet(hstmt)
print "Row #:", i+1, "\x20\x20",
j = 1
while dbGetData(hstmt, j, myarray[i,j-1])
print myarray[i,j-1],"\x20\x20",
j++
endwhile
i++
print
if (i % 20) = 0 then
print
PRINT "Press any key to continue..."
do:until inkey$<>""
print
endif
    ENDWHILE
ENDIF

'=============================================================================
' Verify beginning and ending rows of the array.
'=============================================================================
i-=1
print
print "First row: ",myarray[0,0], "\x20\x20", myarray[0,1], "\x20\x20", myarray[0,2]
print " Last row: ",myarray[i,0], "\x20\x20", myarray[i,1], "\x20\x20", myarray[i,2]
print

'=============================================================================
' Close the database.
'=============================================================================
label FreeIt
dbFreeSQL(hstmt)
dbDisconnect(pDB)

'=============================================================================
' Final exit.
'=============================================================================
label eoj
print "Done"
PRINT "Press any key to exit..."
do:until inkey$<>""

closeconsole