Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sqlite beginnings
#1
Thanks to v1c for porting the sqlite lib, though I'm having a bit of trouble. I translated a sample program and it compiles and mostly works but not quite.

It asks for a database file and for a sql query to execute. It does this then has a callback function that lists all of the results, however I dont think i translated the callback function properly. Instead of outputting text/words it outputs numbers (perhaps pointer values?) - its really weird.

You need fb 0.13 to compile as it uses the 'zString ptr' datatype.

Download sqlite_beginings.zip

Can anyone check if I translated it properly? I've included the C file and the Basic translation.
Reply
#2
there the corrected version works well now have fun

Code:
'$include once: "sqlite3.bi"


sub printBytes ( byval bytes as byte ptr )

   if( bytes = 0 ) then
      print "NULL";
   else
      while( *bytes <> 0 )
         print chr$(*bytes );
         bytes += 1
      wend
   end if
end sub

function callback cdecl (byval NotUsed as any ptr, byval argc as integer, byval argv as byte ptr ptr, byval azColName as byte ptr ptr)
  dim i as integer


  for i = 0 to argc - 1
    if *argv[i] then
      printBytes azColName[i]
      print " = ";
      printBytes argv[i]
      print
    else
      print *azColName[i]; " = NULL";
    end if
  next
  print
  callback = 0
end function

dim rc as integer
dim db as sqlite3 ptr
dim zErrMsg as byte ptr
zErrMsg = 0

dim query as string, database_name as string

input "Database file: "; database_name
rc = sqlite3_open(database_name, @db)

if rc then
  print "Can't open database: "; sqlite3_errmsg(db)
  sqlite3_close (db)
  end
else
  print "Database opened successfully."
end if

print "Type sql statements to be executed"
input "query"; query
rc = sqlite3_exec(db, query, @callback, 0, @zErrMsg)
if rc <> SQLITE_OK then
  print "SQL error: ";
  printBytes zErrMsg
else
  print "SQL successfully worked."
end if

sqlite3_close(db)

no guarantee that it works for tables with more then 1column hehe nah joke gonna try it...
quote="NecrosIhsan"]
[Image: yagl1.png]
[/quote]
Reply
#3
improved again:
Code:
'$include once: "sqlite3.bi"


function cString (byval bytes as byte ptr) as string
  dim s as string
  if( bytes = 0 ) then
    cString = "NULL"
  else
    s = ""
    while( *bytes <> 0 )
      s += chr$(*bytes)
      bytes += 1
    wend
    cString = s
  end if
end function

function callback cdecl (byval NotUsed as any ptr, byval argc as integer, byval argv as byte ptr ptr, byval azColName as byte ptr ptr)
  dim i as integer

  for i = 0 to argc - 1
    if *argv[i] then
      print cString(azColName[i]); " = "; cString(argv[i])
    else
      print cString(azColName[i]); " = NULL"
    end if
  next

  callback = 0
end function

dim rc as integer
dim db as sqlite3 ptr
dim zErrMsg as byte ptr
zErrMsg = 0

dim query as string, database_name as string

input "Database file"; database_name
rc = sqlite3_open(database_name, @db)

if rc then
  print "Can't open database: "; sqlite3_errmsg(db)
  sqlite3_close (db)
  end
end if

print "Type an sql statement to be executed."
input "query"; query
rc = sqlite3_exec(db, query, @callback, 0, @zErrMsg)
if rc <> SQLITE_OK then
  print "SQL error: "; cString(zErrMsg)
end if

sqlite3_close(db)

Cheers marzec, got me on the right track.
Reply
#4
Cool, no need for string copying, zstring's are your friends ;)

Code:
'$include once: "sqlite3.bi"

declare sub showusage( )

declare function callback cdecl (byval NotUsed as any ptr, _
                                 byval argc as integer, byval argv as zstring ptr ptr, _
                                  byval azColName as zstring ptr ptr)

    dim rc as integer
    dim db as sqlite3 ptr
    dim zErrMsg as zstring ptr
    zErrMsg = 0

    dim query as string, database_name as string

    database_name = command$(1)
    if( len( database_name ) = 0 ) then
        showusage
        end 1
    end if
    
    rc = sqlite3_open(database_name, @db)

    if rc then
          print "Can't open database: "; *sqlite3_errmsg(db)
          sqlite3_close (db)
          end
    end if

    query = command$(2)
    if( len( query ) = 0 ) then
        showusage
        end 1
    end if

    rc = sqlite3_exec(db, query, @callback, 0, @zErrMsg)
    if rc <> SQLITE_OK then
          print "SQL error: "; *zErrMsg
    end if

    sqlite3_close(db)

'':::::
function callback cdecl (byval NotUsed as any ptr, _
                         byval argc as integer, byval argv as zstring ptr ptr, _
                         byval azColName as zstring ptr ptr)
  dim i as integer

  for i = 0 to argc - 1
    if *argv[i] <> 0 then
      print *azColName[i]; " = "; *argv[i]
    else
      print *azColName[i]; " = NULL"
    end if
  next

  callback = 0
end function

'':::::
sub showusage( )
    print "Usage: "; command$(0); " database ""query"""
end sub
Reply
#5
Ah, thanks. Didnt quite understand that zstring crap. Wink
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)