Re: [sqlite] Problem with SQLite in BCB 4
Correction: I didnt delete this one ;) Database::Database(char* filename) { ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Problem with SQLite in BCB 4
Solved. I found very interesting post on forum http://marc.info/?l=sqlite-users&m=117751256117132&w=2 So... I deleted some part of code: unit1.cpp: // 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"); and definitions (unit1.h) 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*); and added sqlite3.lib to project - now I dont get any errors. Thanks for Your help - its nice to see so many answers for my question :) ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Problem with SQLite in BCB 4
Thank you very much for your clarification! This is what I suspected. Regards, Samuel - Original Message From: Igor Tandetnik a1rex wrote: > What about void *p = sqlite3_column_blob()? > From my tests it looks that pointer p survives sqlite3_finalize(). > Is it just a coincidence? It "survives" in the same sense as in this example: char* p = (char*)malloc(10); strcpy(p, "Hello"); free(p); printf(p); Chances are high the last line will print "Hello", simply because the now-free memory referred to by now-dangling pointer p didn't have the chance to be overwritten with something else yet __ Looking for the perfect gift? Give the gift of Flickr! http://www.flickr.com/gift/ ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Problem with SQLite in BCB 4
a1rex wrote: > What about void *p = sqlite3_column_blob()? > From my tests it looks that pointer p survives sqlite3_finalize(). > Is it just a coincidence? It "survives" in the same sense as in this example: char* p = (char*)malloc(10); strcpy(p, "Hello"); free(p); printf(p); Chances are high the last line will print "Hello", simply because the now-free memory referred to by now-dangling pointer p didn't have the chance to be overwritten with something else yet. Nevertheless, the code is obviously invalid. -- Igor Tandetnik ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Problem with SQLite in BCB 4
- Original Message >From: Simon Davies >The return from sqlite3_column_text is not valid after subsequent calls to any >of >sqlite3_step/sqlite3_reset/sqlite3_finalize. Yes. What about void *p = sqlite3_column_blob()? >From my tests it looks that pointer p survives sqlite3_finalize(). Is it just a coincidence? On the other hand I experienced invalidations of blob handles obtained by sqlite3_blob_open() after updating records in an UNRELATED table. I could not find description of this behavior in the SQLite documentation. It may be nature of the beats, a bug or a "feature". Unfortunately this behavior may keep me away from using incremental read/write. Regards, Samuel __ Make your browsing faster, safer, and easier with the new Internet Explorer® 8. Optimized for Yahoo! Get it Now for Free! at http://downloads.yahoo.com/ca/internetexplorer/ ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Problem with SQLite in BCB 4
>> 2010/3/6 Chimerian : >> values.push_back((char*)sqlite3_column_text(statement, col)); >> // HERE IS ERROR ! >From: Simon Davies >What error? >I can not see why THIS line should provoke any error; This line will produce a runtime error when sqlite3_column_text(statement, col); returns NULL pointer (and NULL value is a valid value for any affinity if not restricted by NO NULL clause) > but be aware that you are creating a vector of INVALID pointers. No. values.push_back is not creating vector of invalid pointers. It is creating vector of strings. The problem is no valid string can be created from NULL pointer. This function will fail trying to obtain the length of the string from NULL pointer. >The return from sqlite3_column_text is not valid after subsequent calls to any of >sqlite3_step/sqlite3_reset/sqlite3_finalize. VERY TRUE! It is easy to forget about it! >You need to make a copy of the string and store that, not the pointer >returned from sqlite3_column_text. True. But this is done automatically by values.push_back() The vector, like all other Standard Library containers, stores copies - these copies are made with the copy constructor. Regards, Samuel __ Make your browsing faster, safer, and easier with the new Internet Explorer® 8. Optimized for Yahoo! Get it Now for Free! at http://downloads.yahoo.com/ca/internetexplorer/ ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Problem with SQLite in BCB 4
2010/3/6 Chimerian : > 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: > > . . . .> for(int col = 0; col < cols; col++) > { > > values.push_back((char*)sqlite3_column_text(statement, col)); > // HERE IS ERROR ! What error? I can not see why THIS line should provoke any error; but be aware that you are creating a vector of INVALID pointers. The return from sqlite3_column_text is not valid after subsequent calls to any of sqlite3_step/sqlite3_reset/sqlite3_finalize. You need to make a copy of the string and store that, not the pointer returned from sqlite3_column_text. > } > results.push_back(values); > } > else > { > break; > } . . . > How to fix it ? See above (possibly) > > 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 > > Regards, Simon ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Problem with SQLite in BCB 4
Chimerian, I compiled and tested the tutorial example (which you pointed out) with VC++ on XP Pro. Example is fine, except with the problem of not checking for NULL pointer, which I fixed for you. DB is created and values are stored and retrieved. No problem with SQLite or STL. I do not have Borland C++ Builder 6, but I assume it has debugger and you can put a break point to see where char *ptr points. You can also instead of using sqlite3.dll compile sqlite3.c source code and step through the qlite3_column_text(statement, col) function to see how it goes. I strongly suggest to test this example “AS IS” without any Borland GUI interference. Just pure C++ and STL. (There is always a remote possibility for memory overrun or bad linking job). So try the example alone without the pollution first. Good luck. - Original Message >From: Chimerian >To: sqlite-users@sqlite.org >Sent: Mon, March 8, 2010 12:18:53 PM >Subject: Re: [sqlite] Problem with SQLite in BCB 4 >Unfortunately I cant print the value of val before it's pushed to the >values vector because when do that I got this same error - I think >the problem is with pointer: >char * ptr = (char*)sqlite3_column_text(statement, col); >Maybe it hold wrong address ? __ Looking for the perfect gift? Give the gift of Flickr! http://www.flickr.com/gift/ ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Problem with SQLite in BCB 4
Unfortunately I cant print the value of val before it's pushed to the values vector because when do that I got this same error - I think the problem is with pointer: char * ptr = (char*)sqlite3_column_text(statement, col); Maybe it hold wrong address ? I compile this code on Borland C++ Builder 6 Personal and still have error. Summary: I have error on Windows XP / Windows 7 and in Borland Builder 4 Standart / 6 Personal ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Problem with SQLite in BCB 4
Well, sorry to hear that. I tested the function: vector > CSGDb::query(char* query, sqlite3 *database) { sqlite3_stmt *statement; vector > 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 values; for(int col = 0; col < cols; col++) { std::string val; char * ptr = (char*)sqlite3_column_text(statement, col); if(ptr) { val = ptr; } else val = ""; values.push_back(val); } results.push_back(values); } else { break; } } sqlite3_finalize(statement); } return results; } and it works for me fine. Could you print the value of val before it is pushed to the values vector? Of course the problem is not Sqlite problem but the proper usage of STL. - Original Message From: Chimerian To: sqlite-users@sqlite.org Sent: Sun, March 7, 2010 4:23:30 PM Subject: Re: [sqlite] Problem with SQLite in BCB 4 Unfortunately it still doesn't work. I have error in line values.push_back(val); I tried to run program on Windows XP - I have this same error. Links to error screens: http://chimerian.net/error1.jpg http://chimerian.net/error2.jpg __ Looking for the perfect gift? Give the gift of Flickr! http://www.flickr.com/gift/ ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Problem with SQLite in BCB 4
Unfortunately it still doesn't work. I have error in line values.push_back(val); I tried to run program on Windows XP - I have this same error. Links to error screens: http://chimerian.net/error1.jpg http://chimerian.net/error2.jpg if(result == SQLITE_ROW) { vector values; for(int col = 0; col < cols; col++) { std::string val; char * ptr = (char*)sqlite3_column_text(statement, col); if(ptr) { val = ptr; } else val = ""; // now below is a error values.push_back(val); } results.push_back(values); } else { break; } ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Problem with SQLite in BCB 4
You cannot push_back(NULL) null pointer. Fix is below: if(result == SQLITE_ROW) { vector values; for(int col = 0; col < cols; col++) { std::string val; char * ptr = (char*)sqlite3_column_text(statement, col); if(ptr) { val = ptr; } else val = ""; values.push_back(val); // JUZ NIE WYWALA } results.push_back(values); } else { break; } Enjoy, Samuel BTW nice usage of STL + SQlite. - Original Message From: Chimerian To: sqlite-users@sqlite.org Sent: Sat, March 6, 2010 12:05:51 PM Subject: [sqlite] Problem with SQLite in BCB 4 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: //--- #include #pragma hdrstop #include #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 > Database::query(char* query) { sqlite3_stmt *statement; vector > 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 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 > result = db->query("SELECT a, b FROM a;"); for(vector >::iterator it = result.begin(); it < result.end(); ++it) {
[sqlite] Problem with SQLite in BCB 4
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: //--- #include #pragma hdrstop #include #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 > Database::query(char* query) { sqlite3_stmt *statement; vector > 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 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 > result = db->query("SELECT a, b FROM a;"); for(vector >::iterator it = result.begin(); it < result.end(); ++it) { vector row = *it; cout << "Values: (A=" << row.at(0) << ", B=" << row.at(1) << ")" << endl; } db->close(); } //--- and file Unit1.h //--- #ifndef Unit1H #define Unit1H //--- #include #include #include #include #include "sqlite3.h" #include #include //--- 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 > 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**); in