2011-09-22 13 views
7

Microsoft Visual C++ 2010 Expressプロジェクト内でローカルデータベースを作成するにはどうすればよいですか?Microsoft Visual C++ 2010 Expressプロジェクト内でローカルデータベースを作成するにはどうすればよいですか?

申し訳ありませんが、ウェブでこの簡単な答えが見つかりません。私が見つけた唯一の答えは、Visual Studioの場合です:プロジェクト>新規アイテムの追加>ローカルデータベースを使用しています。しかし、このオプションはVisual C++ 2010 Express Editionでは使用できません。

「Microsoft SQL Server Compact 4」と「Microsoft SQL Server Denali」をインストールし、「Windows Update」から「Microsoft Visual C++ 2010 Express」を更新しようとしました。

+0

MSVCは、dBASEのプロジェクトを持っていました。すべての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を実行可能ファイルのディレクトリ または作業ディレクトリにコピーします。行く準備ができている。私はこの情報を期待し

    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); 
    

  • その後

は、何もアウトクエリを使用しないのは簡単ですが、SQL例えば、「SELECT」を使用したい場合は、このコードを使用することができます便利である!

出典:

関連する問題