CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Sqlite / C++

  1. #1
    Join Date
    Dec 2023
    Posts
    4

    Sqlite / C++

    I've created an sqlite application with Visual Studio C++, which works correctly.
    However, I cannot reorder indexes, after deleting a record - because sqlite creates a new table and inserts data from the old table, but refuses to drop this one and so renames the new table.

    This is my relevant code

    void ClegisView::OnBnClickedButton()
    {
    querySQL = "DELETE FROM legis WHERE n_ord = " +ord+ "";

    sqlite3_exec(dbase, querySQL, NULL, NULL, &err);

    queries = "CREATE table temp (n_ord INTEGER NOT NULL PRIMARY KEY, dip TEXT DEFAULT NULL, sum TEXT DEFAULT NULL, text TEXT DEFAULT NULL); INSERT INTO temp (dip, sum, text) SELECT dip, sum, text FROM legis; DROP table legis; ALTER table temp RENAME TO legis";

    sqlite3_exec(dbase, queries, NULL, NULL, &err);

    restart();
    }
    The sqlite statement above works if I use it in Sqlite Browser, from Windows terminal or in Python, but not with C++

    What must I do ?

    Thanks in advance

  2. #2
    Join Date
    Nov 2018
    Posts
    116

    Re: Sqlite / C++

    https://www.sqlite.org/c3ref/exec.html
    1. It returns a result.
    You should examine the result to see if it tells you anything.

    2. err is a pointer to an error message. What does it say?

    Remember to use [code][/code] tags when posting code.

    Posting the equivalent Python which 'works' might also be informative.

  3. #3
    Join Date
    Dec 2023
    Posts
    4

    Re: Sqlite / C++

    Quote Originally Posted by salem_c View Post
    https://www.sqlite.org/c3ref/exec.html
    1. It returns a result.
    You should examine the result to see if it tells you anything.

    2. err is a pointer to an error message. What does it say?

    Remember to use [code][/code] tags when posting code.

    Posting the equivalent Python which 'works' might also be informative.
    Thank you for the answer

    I receive the following error messsage "database table is locked"

    As I have said, the statement only executes the two first instructions - create and insert - and stopped when it was supposed to execute the third - drop table

    I cannot understand why this not occurs using the same statement, for instance, in sqlite browser or terminal

  4. #4
    Join Date
    Nov 2018
    Posts
    116

    Re: Sqlite / C++

    At a guess, not all the wrappers are the same.

    Try breaking the requests up.
    Code:
    void ClegisView::OnBnClickedButton()
    {
        querySQL = "DELETE FROM legis WHERE n_ord = " +ord+ "";
        sqlite3_exec(dbase, querySQL, NULL, NULL, &err);
    
        queries = "CREATE table temp (n_ord INTEGER NOT NULL PRIMARY KEY, dip TEXT DEFAULT NULL, sum TEXT DEFAULT NULL, text TEXT DEFAULT NULL);"
                  " INSERT INTO temp (dip, sum, text) SELECT dip, sum, text FROM legis";
        sqlite3_exec(dbase, queries, NULL, NULL, &err);
    
        moves = "DROP table legis; ALTER table temp RENAME TO legis";
        sqlite3_exec(dbase, moves, NULL, NULL, &err);
    
        restart();
    }

  5. #5
    Join Date
    Dec 2023
    Posts
    4

    Re: Sqlite / C++

    Quote Originally Posted by salem_c View Post
    At a guess, not all the wrappers are the same.

    Try breaking the requests up.
    Code:
    void ClegisView::OnBnClickedButton()
    {
        querySQL = "DELETE FROM legis WHERE n_ord = " +ord+ "";
        sqlite3_exec(dbase, querySQL, NULL, NULL, &err);
    
        queries = "CREATE table temp (n_ord INTEGER NOT NULL PRIMARY KEY, dip TEXT DEFAULT NULL, sum TEXT DEFAULT NULL, text TEXT DEFAULT NULL);"
                  " INSERT INTO temp (dip, sum, text) SELECT dip, sum, text FROM legis";
        sqlite3_exec(dbase, queries, NULL, NULL, &err);
    
        moves = "DROP table legis; ALTER table temp RENAME TO legis";
        sqlite3_exec(dbase, moves, NULL, NULL, &err);
    
        restart();
    }
    I tried - no success
    Same error message and same behaviour

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,821

    Re: Sqlite / C++

    Is this helpful
    https://sqlite.org/forum/info/09d27e962aa890b9

    This if for VB but see the discussion re why this error happens and how to fix it.
    https://www.codeproject.com/Question...e-lock-problem

    Both indicate you might need to call sqlite3_finalize() after the exec.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured