by softruz » Mon Jan 14, 2013 11:53 am
Muy buenas Compañero, te pongo la clase que cree yo para utilizar SQLite en nuestros proyectos.
Espero que te sirva.
//////////////////////////////////////////
// Codigo Fuente
////////////////////////////////////
#include "fivewin.ch"
// ERROR
#define SQLITE_OK 0 /* Successful result */
#define SQLITE_ERROR 1 /* SQL error or missing database */
#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
#define SQLITE_PERM 3 /* Access permission denied */
#define SQLITE_ABORT 4 /* Callback routine requested an abort */
#define SQLITE_BUSY 5 /* The database file is locked */
#define SQLITE_LOCKED 6 /* A table in the database is locked */
#define SQLITE_NOMEM 7 /* A malloc() failed */
#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
#define SQLITE_FULL 13 /* Insertion failed because database is full */
#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
#define SQLITE_EMPTY 16 /* Database is empty */
#define SQLITE_SCHEMA 17 /* The database schema changed */
#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
#define SQLITE_MISMATCH 20 /* Data type mismatch */
#define SQLITE_MISUSE 21 /* Library used incorrectly */
#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
#define SQLITE_AUTH 23 /* Authorization denied */
#define SQLITE_FORMAT 24 /* Auxiliary database format error */
#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
#define SQLITE_NOTADB 26 /* File opened that is not a database file */
#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
// FIN ERROR
#define adSchemaColumns 4
#define adCmdUnspecified -1 // Does not specify the command type argument.
#define adCmdText 1 //Evaluates CommandText as a textual definition of a command or stored procedure call.
#define adCmdTable 2 //Evaluates CommandText as a table name whose columns are all returned by an internally generated SQL query.
#define adCmdStoredProc 4 // Evaluates CommandText as a stored procedure name.
#define adCmdUnknown 8 //Default. Indicates that the type of command in the CommandText property is not known.
#define adCmdFile 256 //Evaluates CommandText as the file name of a persistently stored Recordset. Used with Recordset.Open or Requery only.
#define adCmdTableDirect 512
class tSQLite
data oConn // Conexion con la BBDD
data lError init .f.
data nError init 0
method new(cDatabase) constructor
method Query(cSQL)
method Execute(cSQL)
method GetValue(oRs,cItem)
method end()
method SQliteGetType(nType)
method SQLiteColumnsProperties(cTable)
end class
method new(cDatabase) class tSQLite
local oRs:=nil
::lError:=.f.
try
::oConn:=TOleAuto():new("ADODB.Connection")
catch
::lError:=.t.
end
try
::oConn:ConnectionString:="DRIVER=SQLite3 ODBC Driver;Database="+alltrim(cDatabase)+";LongNames=0;Timeout=1000;NoTXN=0;SyncPragma=NORMAL;StepAPI=0;"
::oConn:Open()
// Vemos si realmente es una SQLite3
catch
::lError:=.t.
end
// Comprobamos si la conecion realmente se ha establecido
oRs:=::Query("PRAGMA database_list")
return Self
/*
oRs:MoveNext()
oRs:MoveFirst()
oRs:MoveLast()
oRs:RecordCount()
*/
method Query(cSQL) class tSQLite
local oRs
oRs:=CreateObject("ADODB.recordset")
try
oRs:Open(cSQL,::oConn,3)
::lError:=.f.
catch
::lError:=.t.
end catch
return oRs
method GetValue(oRs,cItem) class tSQLite
return oRs:fields(cItem):value
method Execute(cSQL) class tSQLite
local oE
::nError:=0
try
::oConn:Execute(cSQL)
::lError:=.f.
catch
::lError:=.t.
oE:= CreateObject("ADODB.Error")
FOR EACH oE IN ::oConn:Errors
::nError:=oE:NativeError
NEXT
oConnection:oConn:Errors:Clear()
end catch
return ::lError
method end() class tSQLite
::oConn:close()
return
method SQliteGetType(nType) class tSQLite
local aTypes:={{"adEmpty",0,nil},;
{"adSmallInt",2,"int"},;
{"adInteger",3,"int"},;
{"adSingle",4,"int"},;
{"adDouble",5,"int"},;
{"adCurrency",6,nil},;
{"adDate",7,"date"},;
{"adBSTR",8,"char"},;
{"adIDispatch",9,nil},;
{"adError",10,"int"},;
{"adBoolean",11,"tinyint"},;
{"adVariant",12,"char"},;
{"adIUnknown",13,nil},;
{"adDecimal",14,"int"},;
{"adTinyInt",16,"tinyint"},;
{"adUnsignedTinyInt",17,"tinyint"},;
{"adUnsignedSmallInt",18,"int"},;
{"adUnsignedInt",19,"int"},;
{"adBigInt",20,"int"},;
{"adUnsignedBigInt",21,"int"},;
{"adFileTime",64,nil},;
{"adGUID",72,"char"},;
{"adBinary",128,nil},;
{"adChar",129,"char"},;
{"adWChar",130,"char"},;
{"adNumeric",131,"int"},;
{"adUserDefined",132,nil},;
{"adDBDate",133,"date"},;
{"adDBTime",134,"char"},;
{"adDBTimeStamp",135,"timestamp"},;
{"adChapter",136,nil},;
{"adPropVariant",138,nil},;
{"adVarNumeric",139,"int"},;
{"adVarChar",200,"char"},;
{"adLongVarChar",201,"char"},;
{"adVarWChar",202,"char"},;
{"adLongVarWChar",203,"char"},;
{"adVarBinary",204,nil},;
{"adLongVarBinary",205,nil}}
local nPos:=0
nPos:=ascan(aTypes,{|v| v[2]=nType})
return iif(nPos=0,nil,aTypes[nPos,3])