0

I have a strange problem. Cant add function to sqlite/ Write on c++/Qt. Function should do upper to Utf-8 simbols;

    this->db = QSqlDatabase::addDatabase("QSQLITE");
    this->db.setDatabaseName(db);
    this->db.open();
    QVariant v = this->db.driver()->handle();
    sqlite3 *handler = *static_cast<sqlite3 **>(v.data());
    sqlite3_create_function( handler, "myUpper", 1, SQLITE_ANY, NULL, myFunc, NULL, NULL )

and the function is:

void myFunc( sqlite3_context * ctx, int argc, sqlite3_value ** argv )
{
        if( argc != 1 ) return;
        QString str;
        switch(sqlite3_value_type(argv[0]))
        {
                case SQLITE_NULL:
                {
                        sqlite3_result_text( ctx, "NULL", 4, SQLITE_STATIC );
                        break;
                }
                case SQLITE_TEXT:
                {
                        QString wstr((char*)sqlite3_value_text16(argv[0]));
                        wstr = wstr.toUpper();
                        sqlite3_result_text16( ctx, wstr.toStdString().c_str(), wstr.size() , SQLITE_TRANSIENT );
                        break;
                }
                default:
                        sqlite3_result_text( ctx, "NULL", 4, SQLITE_STATIC );
                break;
        }
}
ba__friend
  • 5,783
  • 2
  • 27
  • 20
pod2metra
  • 1
  • 1

1 Answers1

2

You didn't specify the problem, but I suppose you should pass a pointer to the function:

sqlite3_create_function(handler, "myUpper", 1, SQLITE_ANY, NULL, &myFunc, NULL, NULL)

EDIT: I you're passing to the function wstr.toStdString().c_str(). This is not allowed because the pointer returned is only temporary and will go out of scope. What I would do is:

...
case SQLITE_TEXT: {
   QString wstr((char*)sqlite3_value_text(argv[0]));
   wstr = wstr.toUpper();
   QByteArray array = wstr.toLocal8Bit();
   const char* cstr = array.data();
   sqlite3_result_text(ctx, cstr, wstr.size() , SQLITE_TRANSIENT);
   break;
}
...

I didn't try it so check this.

Luca Carlon
  • 9,546
  • 13
  • 59
  • 91
  • This isn't solved problem. Program suddenly shutdown, then i'm trying to open base and init function. – pod2metra Jul 07 '11 at 06:21
  • this is'n solved problem. Problem's exectly the same as it was. – pod2metra Jul 08 '11 at 07:06
  • Problem is... you didn't say what the problem you're experiencing is... Have you tried to put some debugging logs or running in debug? – Luca Carlon Jul 08 '11 at 07:20
  • THE PROBLEM IS THAT THE PROGRAM CRASHES WHEN I OPEN DB – pod2metra Jul 08 '11 at 07:29
  • The problem become more interesting. This bug catches on x64 system. On x86 there isn't this problem. Can you explain this? And help in this aspect. – pod2metra Jul 08 '11 at 08:23
  • I'm beginning to think this I posted some time ago may be related: http://stackoverflow.com/questions/6289623/using-sqlite-custom-functions-with-qt. Never solved that problem. – Luca Carlon Jul 08 '11 at 08:29