2011-09-22 49 views
7

如何在Microsoft Visual C++ 2010 Express項目中創建本地數據庫?如何在Microsoft Visual C++ 2010 Express項目中創建本地數據庫?

對不起,但我在網上找不到這個簡單的答案。我發現的唯一答案是Visual Studio:使用項目>添加新項目>本地數據庫。但是這個選項在Visual C++ 2010 Express版中不可用。

我嘗試從 「Windows更新」 安裝 「微軟的SQL Server Compact 4」 和 「Microsoft SQL Server的德納裏」,並更新 「微軟的Visual C++ 2010速成」。

+0

MSVC曾經有質數據庫項目。所有在VS2008附近被移除,幾乎沒有人在C++中寫入dbase代碼。並不是很多用戶注意到,IDE支持總是缺乏。 –

回答

7

好的,我終於得到了一個解決方案。遺憾的是我必須回答我自己的問題...

我用SQLite庫(http://www.sqlite.org/)。這是一個有點複雜,因爲SQLite的文檔是有點模糊,但我做了如下:

  • 下載sqlitedll * .zip文件 - 提取的.def和地方.dll文件。
  • 使用類似於「c:\ program files \ micros〜1 \ vc98 \ bin \ lib」/def:sqlite3.def的命令生成lib文件,從命令 提示符下,在具有DEF文件中,用適當的 路徑的LIB.EXE。您可能需要VCVARS32.BAT先運行,這是 也在bin目錄中,產生的.LIB複製到適當的 的地方,並設置爲一個庫目錄在VC++。(或者在一個 每個項目的基礎上。)
  • 下載sqlite-source * .zip文件,並從內部提取sqlite3.h文件 到一個合適的目錄。在VC++中包含目錄 (同樣, )
  • 在您的項目中,#include根據需要將sqlite3.lib 添加到您的項目中,將sqlite3.dll複製到您的可執行文件的目錄 或工作目錄中,並且您應該準備好去。

然後,很容易使用沒有出查詢,但如果你想使用一個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); 

我希望這個信息有用!

來源:

相關問題