Results 1 to 5 of 5

Thread: How to create a custom function in a QSqlDatabase: regex in sqlite and pyqt

  1. #1
    Join Date
    May 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How to create a custom function in a QSqlDatabase: regex in sqlite and pyqt

    hello!

    We define the python type REGEXP function in sqlite and python following this example on stackoverflow: Problem with regexp python and sqlite

    How can we do the same thing in PyQT, ie. with a QSqlDatabase?

    More precisely, we use the REGEXP function to create a view:

    Qt Code:
    1. Create view temp as select * from somewhere where columnname REGEXP 'myregex';
    To copy to clipboard, switch view to plain text mode 

    This works well, as long as we do the select from python. We would like to show the result in a QTableView (via a QSqlTableModel filled with the view). As the view uses the REGEXP, we would have to link the python regex function to the QSqlDatabase.

    Is there a way of doing that?

    Thanks in advance!

  2. #2
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to create a custom function in a QSqlDatabase: regex in sqlite and pyqt

    Hi kmgrds,

    Do you have the answer because I would like to do the same?

  3. #3
    Join Date
    May 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Re: How to create a custom function in a QSqlDatabase: regex in sqlite and pyqt

    hi miraks,
    no sorry, no answers. also tried on http://stackoverflow.com/questions/1...qlite-and-pyqt but didn't received any answer.
    we finally couldn't manage to get complete regex search going directly from qt.
    so if ever you find something, let us know...
    best
    kmgrds

  4. #4
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Post Re: How to create a custom function in a QSqlDatabase: regex in sqlite and pyqt

    I Don't how to that in PyQt, in C++ Qt:

    You need to wirte a function to do pattern matching for you, example:
    Qt Code:
    1. void qtregexp(sqlite3_context* ctx, int argc, sqlite3_value** argv)
    2. {
    3. QRegExp regex;
    4. QString str1((const char*)sqlite3_value_text(argv[0]));
    5. QString str2((const char*)sqlite3_value_text(argv[1]));
    6.  
    7. regex.setPattern(str1);
    8. regex.setCaseSensitivity(Qt::CaseInsensitive);
    9.  
    10. bool b = str2.contains(regex);
    11.  
    12. if (b)
    13. {
    14. sqlite3_result_int(ctx, 1);
    15. }
    16. else
    17. {
    18. sqlite3_result_int(ctx, 0);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    Then you need to get the handle to the SQLite database somehow, one way is to use SQLite's API, example:
    Qt Code:
    1. sqlite3 *sldb;
    2. sqlite3_open(":memory:", &sldb);
    To copy to clipboard, switch view to plain text mode 
    Last step is a call to sqlite3_create_function, example:
    Qt Code:
    1. sqlite3_create_function(sldb, "REGEXP", 2, SQLITE_UTF8, NULL, &qtregexp, NULL, NULL);
    To copy to clipboard, switch view to plain text mode 
    Hope this helps

  5. #5
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Post Re: How to create a custom function in a QSqlDatabase: regex in sqlite and pyqt

    Changing
    sqlite3_create_function(sldb, "REGEXP", 2, SQLITE_UTF8, NULL, &qtregexp, NULL, NULL);
    to
    Qt Code:
    1. sqlite3_create_function(sldb, "regexp", 2, SQLITE_UTF8, NULL, &qtregexp, NULL, NULL);
    To copy to clipboard, switch view to plain text mode 
    is better, it matches the SQLite's documentations.

Similar Threads

  1. Create a new Sqlite DB?
    By qlands in forum Qt Programming
    Replies: 2
    Last Post: 22nd September 2010, 13:06
  2. Replies: 0
    Last Post: 10th March 2010, 09:13
  3. examining an sqlite db file created by QSqlDatabase
    By gmseed in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2009, 16:54
  4. Sqlite & QSqlDatabase problem
    By rsimone in forum Newbie
    Replies: 5
    Last Post: 29th July 2009, 22:52
  5. SQLite - QSqlDatabase::transaction()
    By whitefurrows in forum Qt Programming
    Replies: 6
    Last Post: 5th May 2009, 17:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.