如何在Microsoft Visual c++ 2010 Express项目中创建本地数据库?

时间:2022-09-01 18:28:33

How can I create a local database inside a Microsoft Visual C++ 2010 Express project?

如何在Microsoft Visual c++ 2010 Express项目中创建本地数据库?

I'm sorry, but I can't find this simple answer in the web. The only answer I've found is for Visual Studio: using project > add new item > local database. But this option isn't available in Visual c++ 2010 Express edition.

对不起,我在网上找不到这个简单的答案。我找到的唯一答案是Visual Studio:使用project >添加新的条目>本地数据库。但是这个选项在Visual c++ 2010 Express edition中没有。

I tried installing "Microsoft SQL Server Compact 4" and "Microsoft SQL Server Denali", and updating "Microsoft Visual C++ 2010 Express" from "Windows Update".

我尝试安装“Microsoft SQL Server Compact 4”和“Microsoft SQL Server Denali”,并从“Windows Update”中更新“Microsoft Visual c++ 2010 Express”。

1 个解决方案

#1


7  

Ok, I got a solution at last. Regrettably I must answer my own question...

好吧,我终于找到解决办法了。很遗憾,我必须回答我自己的问题……

I used SQLite library (http://www.sqlite.org/). It was a little complicated because the sqlite documentation is a bit vague, but I did as follows:

我使用了SQLite库(http://www.sqlite.org/)。这有点复杂,因为sqlite文档有点模糊,但是我这样做了:

  • Download sqlitedll*.zip - extract .def and .dll files somewhere.
  • 下载sqlitedll *。zip -提取.def和.dll文件。
  • Generate the lib file with a command like "c:\program files\micros~1\vc98\bin\lib" /def:sqlite3.def". Do that from a command prompt, in the directory with the .def file in, with the appropriate path to your lib.exe. You may need to run vcvars32.bat first, which is also in the bin directory. Copy the resulting .lib to an appropriate place, and set that as a library directory in VC++. (Or do it on a per-project basis.)
  • 用“c:\程序文件\micros~1\vc98\bin\lib”/def:sqlite3.def”这样的命令生成lib文件。从命令提示符中,在.def文件所在的目录中,使用到lib.exe的适当路径执行。您可能需要运行vcvars32。bat first,它也在bin目录中。将结果的.lib复制到适当的位置,并将其设置为vc++中的库目录。(或者在每个项目的基础上做。)
  • Download the sqlite-source*.zip file, and extract the sqlite3.h file from within to a suitable directory. Set that as an include directory in VC++. (Again, you could do it on a per-project basis.)
  • 下载sqlite-source *。压缩文件,并提取sqlite3。h文件从内部到合适的目录。将其设置为vc++中的include目录。(同样,您可以在每个项目的基础上完成它。)
  • In your project, #include as required, add sqlite3.lib to your project, copy the sqlite3.dll to your executable's directory or working directory, and you should be ready to go.
  • 在您的项目中,#include按需添加sqlite3。lib向您的项目复制sqlite3。dll到您的可执行文件目录或工作目录,您应该准备好了。

Then, is easy to use no-out queries, but if you want to use a SQL "SELECT" for example, you could use this code:

然后,很容易使用无外查询,但如果您想使用SQL“SELECT”,例如,您可以使用以下代码:

std::string queries;
// A prepered statement for fetching tables
sqlite3_stmt *stmt;
// Create a handle for database connection, create a pointer to sqlite3
sqlite3 *handle;
// try to create the database. If it doesnt exist, it would be created
// pass a pointer to the pointer to sqlite3, in short sqlite3**
int retval = sqlite3_open("local.db",&handle);
// If connection failed, handle returns NULL
if(retval){
    System::Windows::Forms::MessageBox::Show("Database connection failed");
    return;
}       
// Create the SQL query for creating a table
char create_table[100] = "CREATE TABLE IF NOT EXISTS users (uname TEXT PRIMARY KEY,pass TEXT NOT NULL,activated INTEGER)";
// Execute the query for creating the table
retval = sqlite3_exec(handle,create_table,0,0,0);
// Insert first row and second row
queries = "INSERT INTO users VALUES('manish','manish',1)";
retval = sqlite3_exec(handle,queries.c_str(),0,0,0);
queries = "INSERT INTO users VALUES('mehul','pulsar',0)";
retval = sqlite3_exec(handle,queries.c_str(),0,0,0);
// select those rows from the table
queries = "SELECT * from users";
retval = sqlite3_prepare_v2(handle,queries.c_str(),-1,&stmt,0);
if(retval){
    System::Windows::Forms::MessageBox::Show("Selecting data from DB Failed");
    return ;
}
// Read the number of rows fetched
int cols = sqlite3_column_count(stmt);
while(1){
    // fetch a row’s status
    retval = sqlite3_step(stmt);
    if(retval == SQLITE_ROW){
    // SQLITE_ROW means fetched a row
    // sqlite3_column_text returns a const void* , typecast it to const char*
        for(int col=0 ; col<cols;col++){
            const char *val = (const char*)sqlite3_column_text(stmt,col);
            System::Windows::Forms::MessageBox::Show(stdstr2systemstr(sqlite3_column_name(stmt,col))+" = "+stdstr2systemstr(val));
        }
    }
    else
    if(retval == SQLITE_DONE){
            // All rows finished
            System::Windows::Forms::MessageBox::Show("All rows fetched");
            break;
        }
        else{
            // Some error encountered
            System::Windows::Forms::MessageBox::Show("Some error encountered");
            return ;
        }
    }
    // Close the handle to free memory
    sqlite3_close(handle);

I expect this info be useful!

我希望这个信息有用!

Sources:

来源:

#1


7  

Ok, I got a solution at last. Regrettably I must answer my own question...

好吧,我终于找到解决办法了。很遗憾,我必须回答我自己的问题……

I used SQLite library (http://www.sqlite.org/). It was a little complicated because the sqlite documentation is a bit vague, but I did as follows:

我使用了SQLite库(http://www.sqlite.org/)。这有点复杂,因为sqlite文档有点模糊,但是我这样做了:

  • Download sqlitedll*.zip - extract .def and .dll files somewhere.
  • 下载sqlitedll *。zip -提取.def和.dll文件。
  • Generate the lib file with a command like "c:\program files\micros~1\vc98\bin\lib" /def:sqlite3.def". Do that from a command prompt, in the directory with the .def file in, with the appropriate path to your lib.exe. You may need to run vcvars32.bat first, which is also in the bin directory. Copy the resulting .lib to an appropriate place, and set that as a library directory in VC++. (Or do it on a per-project basis.)
  • 用“c:\程序文件\micros~1\vc98\bin\lib”/def:sqlite3.def”这样的命令生成lib文件。从命令提示符中,在.def文件所在的目录中,使用到lib.exe的适当路径执行。您可能需要运行vcvars32。bat first,它也在bin目录中。将结果的.lib复制到适当的位置,并将其设置为vc++中的库目录。(或者在每个项目的基础上做。)
  • Download the sqlite-source*.zip file, and extract the sqlite3.h file from within to a suitable directory. Set that as an include directory in VC++. (Again, you could do it on a per-project basis.)
  • 下载sqlite-source *。压缩文件,并提取sqlite3。h文件从内部到合适的目录。将其设置为vc++中的include目录。(同样,您可以在每个项目的基础上完成它。)
  • In your project, #include as required, add sqlite3.lib to your project, copy the sqlite3.dll to your executable's directory or working directory, and you should be ready to go.
  • 在您的项目中,#include按需添加sqlite3。lib向您的项目复制sqlite3。dll到您的可执行文件目录或工作目录,您应该准备好了。

Then, is easy to use no-out queries, but if you want to use a SQL "SELECT" for example, you could use this code:

然后,很容易使用无外查询,但如果您想使用SQL“SELECT”,例如,您可以使用以下代码:

std::string queries;
// A prepered statement for fetching tables
sqlite3_stmt *stmt;
// Create a handle for database connection, create a pointer to sqlite3
sqlite3 *handle;
// try to create the database. If it doesnt exist, it would be created
// pass a pointer to the pointer to sqlite3, in short sqlite3**
int retval = sqlite3_open("local.db",&handle);
// If connection failed, handle returns NULL
if(retval){
    System::Windows::Forms::MessageBox::Show("Database connection failed");
    return;
}       
// Create the SQL query for creating a table
char create_table[100] = "CREATE TABLE IF NOT EXISTS users (uname TEXT PRIMARY KEY,pass TEXT NOT NULL,activated INTEGER)";
// Execute the query for creating the table
retval = sqlite3_exec(handle,create_table,0,0,0);
// Insert first row and second row
queries = "INSERT INTO users VALUES('manish','manish',1)";
retval = sqlite3_exec(handle,queries.c_str(),0,0,0);
queries = "INSERT INTO users VALUES('mehul','pulsar',0)";
retval = sqlite3_exec(handle,queries.c_str(),0,0,0);
// select those rows from the table
queries = "SELECT * from users";
retval = sqlite3_prepare_v2(handle,queries.c_str(),-1,&stmt,0);
if(retval){
    System::Windows::Forms::MessageBox::Show("Selecting data from DB Failed");
    return ;
}
// Read the number of rows fetched
int cols = sqlite3_column_count(stmt);
while(1){
    // fetch a row’s status
    retval = sqlite3_step(stmt);
    if(retval == SQLITE_ROW){
    // SQLITE_ROW means fetched a row
    // sqlite3_column_text returns a const void* , typecast it to const char*
        for(int col=0 ; col<cols;col++){
            const char *val = (const char*)sqlite3_column_text(stmt,col);
            System::Windows::Forms::MessageBox::Show(stdstr2systemstr(sqlite3_column_name(stmt,col))+" = "+stdstr2systemstr(val));
        }
    }
    else
    if(retval == SQLITE_DONE){
            // All rows finished
            System::Windows::Forms::MessageBox::Show("All rows fetched");
            break;
        }
        else{
            // Some error encountered
            System::Windows::Forms::MessageBox::Show("Some error encountered");
            return ;
        }
    }
    // Close the handle to free memory
    sqlite3_close(handle);

I expect this info be useful!

我希望这个信息有用!

Sources:

来源: