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

标签 c++ database visual-c++ sqlite

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

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

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

最佳答案

好的,我终于找到了解决方案。很遗憾,我必须回答我自己的问题......

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

  • Download sqlitedll*.zip - extract .def and .dll files somewhere.
  • 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.)
  • 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.)
  • 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.

然后,很容易使用 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);

我希望这些信息有用!

来源:

关于c++ - 如何在 Microsoft Visual C++ 2010 Express 项目中创建本地数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7522355/

相关文章:

c++ - _popen 结果为字符串 : a special case

c++ - 按姓氏对列表进行排序

java - 处理海量数据时,在数据存储中定义实体的正确方法是什么?

c++ - 当 C++ 程序在 Windows 上终止时,终止 stub 调用的最后一个函数是什么?

winapi - 使用对 GlobalLock()\GlobalUnlock() 的正确方法是什么?

c++ - 如何将 std::chrono::zoned_time 转换为 std::string

c++ - 如何在具有相同第一个索引的二维矩阵中添加值 (c++)

c++ - 我将如何创建一个输入概率输出 z 分数的函数? (C++)

mysql - 求职网站数据库设计

php - 如何使用 table2 中的检查字段将数据插入 table1 (Codeigniter)