Russian Qt Forum
Июнь 17, 2024, 20:13 *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

Войти
 
  Начало   Форум  WIKI (Вики)FAQ Помощь Поиск Войти Регистрация  

Страниц: [1]   Вниз
  Печать  
Автор Тема: Sqlite3. Несколько использований одной самописной функции в одном запросе  (Прочитано 2124 раз)
johnpion
Гость
« : Сентябрь 13, 2012, 11:27 »

Вот код модуля для sqlite3:
Код:
п»ї#include "sqlite3ext.h"
#include <stdlib.h>
#include <stdio.h>
SQLITE_EXTENSION_INIT1

static void moneyFunc(
        sqlite3_context *context,
        int argc,
        sqlite3_value **argv
        ){
    char *result;

    int sum = sqlite3_value_int(argv[0]);
    int full = sum / 100;
    int dec = sum % 100;

    if (dec == 0){
        snprintf(result, 8, "%d,00", full);
    } else if (dec > 0 && dec < 10){
        snprintf(result, 8, "%d,0%d", full, dec);
    } else if (dec >= 10 && dec <= 99){
        snprintf(result, 8, "%d,%d", full, dec);
    } else return;

    sqlite3_result_text(context, result, 8, 0);
}

int sqlite3_extension_init(
        sqlite3 *db,
        char **pzErrMsg,
        const sqlite3_api_routines *pApi
        ){
    SQLITE_EXTENSION_INIT2(pApi)
            sqlite3_create_function(db, "money", 1, SQLITE_ANY, 0, moneyFunc, 0, 0);
    return 0;
}
 

Запрос вида
Код:
SELECT money(100)
возвращает 1,00. Всё ок, НО! Запрос вида
Код:
SELECT money(100), money(350)
Выдает 3,50 | 3,50 вместо 1,00 и 3,50. Т.е. в вызове используется последнее значение. Где я не прав?
Записан
GreatSnake
Джедай : наставник для всех
*******
Offline Offline

Сообщений: 2921



Просмотр профиля
« Ответ #1 : Сентябрь 13, 2012, 11:46 »

Насчёт select-а не скажу, а вот то, что пишите в не инициализированный result есть совсем не good Грустный
Записан

Qt 5.11/4.8.7 (X11/Win)
johnpion
Гость
« Ответ #2 : Сентябрь 13, 2012, 11:52 »

Исправил:
Код:
#include "sqlite3ext.h"
#include <stdlib.h>
#include <stdio.h>
SQLITE_EXTENSION_INIT1

static void moneyFunc(
        sqlite3_context *context,
        int argc,
        sqlite3_value **argv
        ){
    char *result = (char *) malloc (sizeof(char)*(8));

    int sum = sqlite3_value_int(argv[0]);
    int full = sum / 100;
    int dec = sum % 100;

    if (dec == 0){
        snprintf(result, 8, "%d,00", full);
    } else if (dec > 0 && dec < 10){
        snprintf(result, 8, "%d,0%d", full, dec);
    } else if (dec >= 10 && dec <= 99){
        snprintf(result, 8, "%d,%d", full, dec);
    } else return;

    sqlite3_result_text(context, result, 8, 0);
}

int sqlite3_extension_init(
        sqlite3 *db,
        char **pzErrMsg,
        const sqlite3_api_routines *pApi
        ){
    SQLITE_EXTENSION_INIT2(pApi)
            sqlite3_create_function(db, "money", 1, SQLITE_ANY, 0, moneyFunc, 0, 0);
    return 0;
}

Впихиваю в Qt в виде:
Код:
#include <QString>
#include <QObject>
#include <sqlite3.h>

static void moneyFunc(sqlite3_context *context, int argc, sqlite3_value **argv)
{
    int sum = sqlite3_value_int(argv[0]);
    int full = sum / 100;
    int dec = sum % 100;

    QString resultStr;
    resultStr.clear();
    resultStr = QString(QObject::tr("0,00"));

    if (dec == 0){
        resultStr = QString(QObject::tr("%1,00")).arg(full);
    } else if (dec > 0 && dec < 10){
        resultStr = QString(QObject::tr("%1,0%2")).arg(full).arg(dec);
    } else if (dec >= 10 && dec <= 99){
        resultStr = QString(QObject::tr("%1,%2")).arg(full).arg(dec);
    }

    sqlite3_result_text(context, resultStr.toAscii().data(), resultStr.size(), 0);
}
Та же проблема
« Последнее редактирование: Сентябрь 13, 2012, 12:35 от johnpion » Записан
Страниц: [1]   Вверх
  Печать  
 
Перейти в:  


Страница сгенерирована за 0.304 секунд. Запросов: 22.