I have a problem with SQLite in BCB 4.0. I wrote my own application -
I found very useful information on
http://www.dreamincode.net/forums/index.php?showtopic=122300
On form (Form1) I have: Memo1 and Button3.
File Unit1.cpp:
<code>
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <fstream.h>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// DLL handle
HANDLE DLLHandle = LoadLibrary("sqlite3.dll");
// SQLite class
Database::Database(char* filename)
{
sqlite3_open = (int (__cdecl *)(const char *,
sqlite3**))GetProcAddress(DLLHandle,"sqlite3_open");
sqlite3_close = (int (__cdecl
*)(sqlite3*))GetProcAddress(DLLHandle,"sqlite3_close");
sqlite3_prepare_v2 = (int (__cdecl *)(sqlite3*, const char*, int,
sqlite3_stmt**, const char**))GetProcAddress(DLLHandle,"sqlite3_prepare_v2");
sqlite3_column_count = (int (__cdecl
*)(sqlite3_stmt*))GetProcAddress(DLLHandle,"sqlite3_column_count");
sqlite3_step = (int (__cdecl
*)(sqlite3_stmt*))GetProcAddress(DLLHandle,"sqlite3_step");
sqlite3_column_text = (const unsigned char (__cdecl *)(sqlite3_stmt*,
int iCol))GetProcAddress(DLLHandle,"sqlite3_column_text");
sqlite3_finalize = (int (__cdecl
*)(sqlite3_stmt*))GetProcAddress(DLLHandle,"sqlite3_finalize");
database = NULL;
open(filename);
}
Database::~Database()
{
}
bool Database::open(char* filename)
{
if(sqlite3_open(filename, &database) == SQLITE_OK)
{
Form1->Memo1->Lines->Add("You have access to base.");
return true;
}
return false;
}
vector<vector<string> > Database::query(char* query)
{
sqlite3_stmt *statement;
vector<vector<string> > results;
if(sqlite3_prepare_v2(database, query, -1, &statement, 0) == SQLITE_OK)
{
int cols = sqlite3_column_count(statement);
int result = 0;
while(true)
{
result = sqlite3_step(statement);
if(result == SQLITE_ROW)
{
vector<string> values;
for(int col = 0; col < cols; col++)
{
values.push_back((char*)sqlite3_column_text(statement, col)); // HERE IS
ERROR !
}
results.push_back(values);
}
else
{
break;
}
}
sqlite3_finalize(statement);
}
return results;
}
void Database::close()
{
sqlite3_close(database);
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Database *db;
db = new Database("Database.sqlite");
db->query("CREATE TABLE a (a INTEGER, b INTEGER);");
db->query("INSERT INTO a VALUES(1, 2);");
db->query("INSERT INTO a VALUES(5, 4);");
vector<vector<string> > result = db->query("SELECT a, b FROM a;");
for(vector<vector<string> >::iterator it = result.begin(); it < result.end();
++it)
{
vector<string> row = *it;
cout << "Values: (A=" << row.at(0) << ", B=" << row.at(1) << ")" <<
endl;
}
db->close();
}
//---------------------------------------------------------------------------
</code>
and file Unit1.h
<code>
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "sqlite3.h"
#include <vector.h>
#include <Grids.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TMemo *Memo1;
TButton *Button3;
void __fastcall Button3Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
class Database
{
public:
Database(char* filename);
~Database();
bool open(char* filename);
vector<vector<string> > query(char* query);
void close();
sqlite3 *database;
private:
int (*sqlite3_open)(const char *, sqlite3**);
int (*sqlite3_prepare_v2)(sqlite3*, const char*, int, sqlite3_stmt**,
const char**);
int (*sqlite3_column_count)(sqlite3_stmt*);
int (*sqlite3_step)(sqlite3_stmt*);
const unsigned char (*sqlite3_column_text)(sqlite3_stmt*, int iCol);
int (*sqlite3_finalize)(sqlite3_stmt*);
int (*sqlite3_close)(sqlite3*);
};
//---------------------------------------------------------------------------
#endif
</code>
I was copy sqlite.dll (http://www.sqlite.org/sqlitedll-3_6_22.zip) to
my project folder, and sqlite3.h
(http://www.sqlite.org/sqlite-amalgamation-3_6_22.zip) to
Borland\Include\
I can make database and add new record. I have problem with SELECT. I
got error on line:
<code>
values.push_back((char*)sqlite3_column_text(statement, col)); // TUTAJ WYWALA
BŁĄD !
</code>
How to fix it ?
I have Borland Builder 4 Standart and Windows 7. I use 3.6.22 version
of SQLite.
Link to my project (comments in polish): http://chimerian.net/4prog.rar
-------------------------------------------------
Atrakcyjne mieszkania i działki. Sprawdź oferty!
http://link.interia.pl/f260b
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users