C ++ Builder 2007 application database SQLite (reprint)

The first time you use the SQLite database, and BCB2007 a stranger, which is a combination of both it made me uncomfortable. Today, simply call in the BCB SQLite, I spent an afternoon time, it also showed how superficial my knowledge, on the other hand also shows that both of these I do not really familiar with.

Online search can be applied to more SQLite VC ++ 6.0 and C ++ Builder article 6.0, according to these prior methods have not succeeded; of course, these methods are not per se can not, personally think it should be now or whether vs BCB version higher than before, compatibility is not good.

BCB use SQLite most important thing is to generate sqlite3.lib file. The SQLite official download only gave sqlite3.dll and sqlite3.def file, and does not provide us with the required sqlite3.lib. There are two options, one is to use a DLL function using dynamically loaded inside, I have not tried this method, it is said that trouble, and if at the time of program execution, in theory, is a bit slow (because every time it must be with LoadLibrary GetProcAddress). Another option is to generate their own LIB file.

The entire online method:

<1> VC ++ 2005: Start a command line, enter the installation directory of VC, my directory is C: \ Program Files \ Microsoft Visual Studio 8 \ VC \ bin directory in the following a lib.exe, right, use it can generate sqlite3.lib required documents. The official download sqlite3.def SQLite file into the same directory and enter the following at the command line:

C:\Program Files\Microsoft Visual Studio 8\VC\bin>LIB /MACHINE:IX86 /DEF:sqlite.def

At this point, does not run successfully, the reason is unable to load mspdb80.dll, the solution is simple: find from Common7 \ IDE copy the file directory just under perform just fine again.

This was sqlite3.lib file. Copy the file to sqlite3.h sqlite3.lib sqlite3.dll Project / Solution, fill #include "sqlite3.h" at the top of the library files needed to write cpp file, and through the project - Add Existing Item> lib join the project / solutions, so you can call all the functions sqlite3.dll inside.

<2> Borland C ++ Builder 6: use lib file with vc completely different, not to be confused, BCB itself provides a command-line tool is used to generate a sqlite3.lib file, specific methods are as follows:

Found in the BCB IMPLIB.EXE installation directory, copy it or its catalog, and the sqlite3.dll files copied to the same directory, then execute the following commands (if the two files into the root directory of C) :

C:\implib sqlite3 sqlite3.dll

This creates the desired specific files in the BCB sqlite3.lib, particularly relatively simple to use, is omitted.

I have tried both methods. The second way in which I use in BCB2007, no matter how debugging will not work, and now have not been resolved.

The first method can be used successfully to generate sqlite3.lib although in vs2005 in, but it generates a static link library files can not be directly transplanted to BCB2007 used. There is a small problem, is the difference between the issue and the conversion lib on OMF and COFF format lib format.

What specific OMF and COFF is not here to get to the bottom. OMF is a difference BCB format LIB is used; the COFF format vc LIB is used. COFF to OMF conversion coff2omf.exe to use. Know enough to solve this problem: vc generated lib is COFF format, using BCB conversion tool coff2omf.exe, the BCB file in the installation directory, in my C: \ Program Files \ CodeGear \ RAD Studio \ 5.0 \ bin. Specific command is as follows:

C:\Program Files\CodeGear\RAD Studio\5.0\bin>coff2omf sqlite3.lib c:\sqlite3.lib

You should first use sqlite3.lib vc generation to the next directory coff2omf.exe lies. The second parameter is the above command lib converted files, both file name is the same, so the file lib next converted into another directory.

So far, all the problems have been resolved.

Just a small problem actually get me one afternoon, but the harvest is not small oh ~ ~

My first little show about a SQLite application:

//---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <sstream>
#include "sqlite3.h"
using namespace std;
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
sqlite3 *pDB;
int createTable()
{
char *errMsg;
std::string dropTab="drop table test_tab;";
string strSQL="create table test_tab(f1 int,f2 long);";
int res=sqlite3_exec(pDB,dropTab.c_str(),0,0,&errMsg);
if(res!=SQLITE_OK)
{
cout<<"执行sql出错."<<errMsg<<endl;
//return -1;
}
res=sqlite3_exec(pDB,strSQL.c_str(),0,0,&errMsg);
if(res!=SQLITE_OK)
{
cout<<"执行创建table的SQL出错."<<endl;
return -1;
}
else
{
cout<<"创建table的SQL成功执行."<<endl;
}
return 0;
}
int insert1()
{
char *errMsg;
int res=sqlite3_exec(pDB,"begin transaction;",0,0,&errMsg);
for(int i=1;i<10;i++)
{
stringstream strsql;
strsql<<"insert into test_tab values(";
strsql<<i<<","<<(i+10)<<");";
string str=strsql.str();
res=sqlite3_exec(pDB,str.c_str(),0,0,&errMsg);
if(res!=SQLITE_OK)
{
cout<<"执行SQL出错."<<errMsg<<endl;
return -1;
}
}
res=sqlite3_exec(pDB,"commit transaction;",0,0,&errMsg);
cout<<"SQL成功执行."<<endl;
return 0;
}
static int callback(void *NotUsed,int argc,char **argv,char **azColName)
{
int i;
for(i=0;i<argc;i++)
{
cout<<azColName[i]<<" = "<<(argv[i]?argv[i]:"NULL")<<",";
}
cout<<endl;
return 0;
}
int select1()
{
char *errMsg;
string strSQL="select * from test_tab;";
int res=sqlite3_exec(pDB,strSQL.c_str(),callback,0,&errMsg);
if(res!=SQLITE_OK)
{
cout<<"执行SQL出错."<<errMsg<<endl;
return -1;
}
else
{
cout<<"SQL执行成功."<<endl;
}
return 0;
}
int main(int argc, char* argv[])
{
int res=sqlite3_open("test_tab.db",&pDB);
if(res)
{
cout<<"Can't open database:"<<sqlite3_errmsg(pDB);
sqlite3_close(pDB);
-1 return;
}
RES = the createTable ();
IF (RES = 0!)
{
return 0;
}
RES = insert1 ();
IF (RES = 0!)
{
return 0;
}
the select1 ();
System ( "PAUSE" );
return 0;
}
// ------------------------------------------ ---------------------------------
The result:

reprinted from: http: //hi.baidu.com/fifarzh /blog/item/3ca4bced0d1a90d2b31cb1e5.html/cmtid/8b32b94af2dad02809f7eff8
# VC ++

Guess you like

Origin www.cnblogs.com/blogpro/p/11446037.html