in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

VS 2008 C++ samples

Last post 07-06-2010 2:34 PM by stutak. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 02-03-2010 11:24 AM

    • stutak
    • Not Ranked
    • Joined on 02-03-2010
    • Posts 2

    VS 2008 C++ samples

    Hi all.

    Can someone give me working  c++ forms sample?

    Because I cant find some tutorial/man and recieving lot of error...

    For example:

        System::String^ connection_string = "Data Source=C:\\test.db";
        SQLiteConnection con = gcnew SQLiteConnection(connection_string);
        con.Open();
        SQLiteCommand cmd = gcnew SQLiteCommand(con);     // Here I've got error and I was unable to link command with connection(if I'll write this code:

    cmd.connection=con;//cannot convert parameter 1 from 'System::Data::SQLite::SQLiteConnection' to 'System::Data::Common::DbConnection ^'

    )

     

    Would be great if someone got working c++ samples for:

    1. Open database;

    2. Insert data;

    3. Read and display data; (is there way to read data in array[ ][ ] or something? )

     

     

     

     

    Thank You for this product.

  • 07-06-2010 3:49 AM In reply to

    • Juve86
    • Not Ranked
    • Joined on 07-06-2010
    • Posts 2

    Re: VS 2008 C++ samples

    Hi,

    To work with Visual Studio 2008 C++ you should be added library System.Data.SQLite.lib in Linkers (in). In addition sqlite3.h and sqlite3Ext.h should be in the project folder.
    Sample code:

        sqlite3 *db; // sqlite3 db struct
        char *ErrMsg = 0;
        int ret;

        // Open the test.db file
        ret = sqlite3_open("testpw2.db3", &db);
        ret = sqlite3_key(db, "2442", strlen("2442");

        if( ret ){
            // failed
            fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        }
        else
        {
            // success
            fprintf(stderr, "Open database successfully\n\n");
        }

        // ..
        // .. after open database
        // ..

        const char *pSQL[2];

        // Create a new myTable in database
        pSQL[0] = "create table myTable1 (FirstName varchar(30), LastName varchar(30), Age smallint)";
        //pSQL[0] = "select * from THuella";

        // Insert first data item into myTable
        pSQL[1] = "insert into myTable1 (FirstName, LastName, Age) values ('Hector', 'Navarro', 24)";
        
         // execute all the sql statement
        for(int i = 0; i < 2; i++)
        {
            ret = sqlite3_exec(db, pSQL[i], callback, 0, &ErrMsg);
            if( ret!=SQLITE_OK ){
                fprintf(stderr, "SQL error: %s\n", ErrMsg);
                sqlite3_free(ErrMsg);
                break;
            }
        }

        // ..
        // .. before close database
        // ..

        // Open the db file

       sqlite3_close(db);

     

  • 07-06-2010 4:19 AM In reply to

    Re: VS 2008 C++ samples

    Please note that the sample code kindly provided by Juve86's is pure C++, and thus does not take advantage of managed System.Data.SQLite at all, in which case the raw http://www.sqlite.org/ could be used instead.

    If you indeed want to use managed C++, then you need only make some minor adjustments to your code. Specifically, objects returned by 'gcnew' are references, so you need to use '^' in the associated variable declarations of 'con' and 'cmd'. For instance:

    
        System::String^ connection_string = "Data Source=C:\\test.db";
        SQLiteConnection^ con = gcnew SQLiteConnection(connection_string);
        con->Open();
        SQLiteCommand^ cmd = gcnew SQLiteCommand(con);
        // -- or --
        cmd->Connection = con;
    

    Note also the use of '->' rather than '.' when dereferencing those variables.

  • 07-06-2010 2:34 PM In reply to

    • stutak
    • Not Ranked
    • Joined on 02-03-2010
    • Posts 2

    Re: VS 2008 C++ samples

    Just in time 8)
    Never mind.
    Anyway - thanx.

Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems