Below code snippet is my execise translation C code from http://zetcode.com/db/sqlitec/
When it runs,It prints "ALL DONE".
But it doesn't work properly,that is,the blob data does not insert into the db. Confirm test1.db and sample.jpg are fine in the folder.I tested the original C code with PellesC and it runs as expected.
Help would be appreciated.
Code: Select all
#include "sqlite3.bi"
#include "crt.bi"
function main_function() as integer
dim fp as file ptr
fp=fopen("sample.jpg","rb")
if(fp=null) then
fprintf(stderr,!"Cann't open image file\n")
return 1
end if
fseek(fp,0,SEEK_END)
if(ferror(fp)<>0) then
fprintf(stderr,!"fseek() failed\n")
dim as integer r =fclose(fp)
if(r<>0) then
fprintf(stderr,!"Cannot close file handler\n")
end if
end if
return 1
dim flen as integer=ftell(fp)
if(flen=-1) then
perror(!"error occurred\n")
dim as integer r=fclose(fp)
if(r<>0) then
fprintf(stderr,!"Cannot close file handler\n")
end if
return 1
end if
fseek(fp,0,SEEK_SET)
if(ferror(fp)) then
fprintf(stderr,!"fseek() failed\n")
dim as integer r=fclose(fp)
if(r<>0) then
fprintf(stderr,!"Cannot close file handler\n")
end if
return 1
end if
'dim datas(flen+1) as byte
dim as ulong datas(flen+1)
dim as integer res=fread(@datas(0),1,flen,fp)
if(ferror(fp)) then
fprintf(stderr,!"fread() failed\n")
dim as integer r=fclose(fp)
if(r<>0) then
fprintf(stderr,!"Cann't close file handler\n")
end if
return 1
end if
dim as integer r=fclose(fp)
if(r<>0) then
fprintf(stderr,"Cann't close file handler\n")
end if
dim db as sqlite3 ptr
dim err_msg as zstring ptr=null
dim as integer rc=sqlite3_open("test1.db",@db)
if(rc<>SQLITE_OK) then
fprintf(stderr,!"Cannot open database :%s\n",sqlite3_errmsg(db))
sqlite3_close(db)
return 1
end if
dim as sqlite3_stmt ptr pStmt
dim as string sql="Insert into images(data) values(?)"
rc=sqlite3_prepare(db,strptr(sql),-1,@pStmt,null)
if(rc<>SQLITE_OK) then
fprintf(stderr,!"Cannot prepare statement:%s\n",sqlite3_errmsg(db))
return 1
end if
if(sqlite3_bind_blob(pStmt,1,@datas(0),res,SQLITE_STATIC)<>SQLITE_OK) then
fprintf(stderr,!"Cannot insert blob data to db:%s\n",sqlite3_errmsg(db))
return 1
end if
rc=sqlite3_step(pStmt)
if(rc<>SQLITE_DONE) then
printf(!"Execution failed:%s",sqlite3_errmsg(db))
end if
sqlite3_finalize(pStmt)
sqlite3_close(db)
printf(!"Everything is DONE!\n") ''''-------->This line doesn't get printed in the console
sleep
return 0
end function
main_function
? "ALL DONE!" ''''--------------------->This line gets printed in the console
sleep