DatabaseRows()
Posted: Mon Aug 11, 2008 7:41 am
Get the amount of rows after an "select" statement. To have the possibility
to work with arrays..
to work with arrays..
Code: Select all
If DatabaseQuery(#DB, "SELECT COUNT(*) FROM tblName")
If NextDatabaseRow(#DB)
ReDim MyArray.MyType(GetDatabaseLong(#DB, 0))
EndIf
FinishDatabaseQuery(#DB)
EndIf
please only as an optional parameter because I think that can reduce the performance of DatabaseQuery().dige wrote:Get the amount of rows after an "select" statement.
Code: Select all
Result = DatabaseQuery(Database, Query, ReturnRowsAffected) ; ReturnRowsAffected = #True or #False (default: #False)
Code: Select all
UseODBCDatabase()
OpenDatabase(0,"database","user","pw")
query.s="select * from something where thing='anything'bezirk_nummer=998"
DatabaseQuery(0,query)
Debug "columncount "+Str(DatabaseColumns(0))
While NextDatabaseRow(0) ;step forewards
r+1
Wend
Debug "rowcount"+Str(r) ; that's the rowcount
While PreviousDatabaseRow(0) ;step backwards alternative :using firstdatabaserow sets the cursor to the first resultline
Wend
CloseDatabase(0)
End
Code: Select all
;Find out how many records will be returned
GetTotal.s = "Select Count(*) FROM MyRecords " + Criteria.s ;Replace with your query
result = DatabaseQuery(ExampleDB, GetTotal.s)
FirstDatabaseRow(ExampleDB)
TotalRows.i = Val(GetDatabaseString(ExampleDB,0)) ;Store this in the TotalRows variable
FinishDatabaseQuery(ExampleDB) ;free the query
Code: Select all
;No previous database row in sqlite So count forward from first Databaserow
;CurrentRow is the required row number
i=1
FirstDatabaseRow(ExampleDB)
If CurrentRow.i = 1 ;Displaying first returned row
SetGadgetText(str_Record,GetDatabaseString(ExampleDB,0)) ;Display or use data
Else
;Look for the required display row
While NextDatabaseRow(ExampleDB)
;No other processing here to keep it as quick as possible
i=i+1
If i = CurrentRow.i
SetGadgetText(str_Record,GetDatabaseString(ExampleDB,0)) ;Display or use data
Break ;Reached the row wanted so quit While...Wend
EndIf
Wend
EndIf
Assuming you already know the current row number, the previous or next record could be retrieved quite simply, with this query:collectordave wrote:The lack of previous databaserow in sqlite is a pain...
Code: Select all
queryString.s = "SELECT * FROM tableName LIMIT 1 OFFSET " + Str(requiredRow)
Code: Select all
; Use database from help
Query$ = "select * from food where weight > 7 "
; Get count of rows
Rowcount.i = 0
If DatabaseQuery(0, "select count(*) as rowscount from (" + Query$ + ") as myRowsCount")
NextDatabaseRow(0)
Rowscount = GetDatabaseLong(0, 0)
FinishDatabaseQuery(0)
EndIf
Debug Rowcount
; Get data
If DatabaseQuery (0, Query$)
For i = 1 to Rowcount
NextDatabaseRow (0)
Debug GetDatabaseString(0, 0)
Next
FinishDatabaseQuery (0)
EndIf
Code: Select all
;{ Return number of records found in a table
Procedure.i dbCountRows(dbn.i, table$, query$="", field="*")
Protected result.i, s$
If IsDatabase(dbn)
s$="select count ("+field$+") from "+table$
If query$
s$+" where "+query$
EndIf
If DatabaseQuery(dbn, s$)
If NextDatabaseRow(dbn)
result=GetDatabaseLong(dbn, 0)
EndIf
FinishDatabaseQuery(dbn)
EndIf
EndIf
ProcedureReturn result
EndProcedure
;}