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

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

本文关键字:项目 创建 数据库 Express 2010 Microsoft c++ Visual      更新时间:2023-10-16

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

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

我尝试安装"Microsoft SQL Server Compact 4"answers"Microsoft SQL Server denalii",以及更新"Microsoft Visual c++ 2010 express";from "Windows Update"

好了,我终于有办法了。很遗憾,我必须回答我自己的问题……

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

  • 下载sqlitedll*.zip -解压。def和。dll文件。
  • 用像"c:program . exe"这样的命令生成lib文件文件微指令~ 1 vc98 bin lib/def: sqlite3.def"。用命令来做吗提示,在带有.def文件的目录中,使用适当的lib.exe的路径。您可能需要先运行vcvars32.bat,即也在bin目录中。将生成的.lib复制到适当的并将其设置为vc++中的库目录。(或者在一个基于)。
  • 下载sqlite-source*.zip文件,解压缩sqlite3.h文件从内部到合适的目录。将其设置为包含目录在vc++。(同样,您可以在每个项目的基础上进行。)
  • 在您的项目中,按需要添加#include,添加sqlite3.lib将sqlite3.dll复制到您的项目的可执行目录中或工作目录,你应该准备好了。

然后,很容易使用no-out查询,但如果您想使用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);

我希望这个信息是有用的!

来源:

  • http://www.gamedev.net/topic/332251-sqlite3-and-visual-c/page_ p _3157685 # entry3157685
  • http://milky.manishsinha.net/2009/03/30/sqlite-with-c/