⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qsql_sqlite.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                    res = sqlite3_bind_blob(d->stmt, i + 1, ba->constData(),                                            ba->size(), SQLITE_STATIC);                    break; }                case QVariant::Int:                    res = sqlite3_bind_int(d->stmt, i + 1, value.toInt());                    break;                case QVariant::Double:                    res = sqlite3_bind_double(d->stmt, i + 1, value.toDouble());                    break;                case QVariant::LongLong:                    res = sqlite3_bind_int64(d->stmt, i + 1, value.toLongLong());                    break;                case QVariant::String: {                    // lifetime of string == lifetime of its qvariant                    const QString *str = static_cast<const QString*>(value.constData());                    res = sqlite3_bind_text16(d->stmt, i + 1, str->utf16(),                                              (str->size()) * sizeof(QChar), SQLITE_STATIC);                    break; }                default: {                    QString str = value.toString();                    // SQLITE_TRANSIENT makes sure that sqlite buffers the data                    res = sqlite3_bind_text16(d->stmt, i + 1, str.utf16(),                                              (str.size()) * sizeof(QChar), SQLITE_TRANSIENT);                    break; }                }            }            if (res != SQLITE_OK) {                setLastError(qMakeError(d->access, QCoreApplication::translate("QSQLiteResult",                             "Unable to bind parameters"), QSqlError::StatementError, res));                d->finalize();                return false;            }        }    } else {        setLastError(QSqlError(QCoreApplication::translate("QSQLiteResult",                        "Parameter count mismatch"), QString(), QSqlError::StatementError));        return false;    }    d->skippedStatus = d->fetchNext(cache(), 0, true);    if (lastError().isValid()) {        setSelect(false);        setActive(false);        return false;    }    setSelect(!d->rInf.isEmpty());    setActive(true);    return true;}bool QSQLiteResult::gotoNext(QSqlCachedResult::ValueCache& row, int idx){    return d->fetchNext(row, idx, false);}int QSQLiteResult::size(){    return -1;}int QSQLiteResult::numRowsAffected(){    return sqlite3_changes(d->access);}QVariant QSQLiteResult::lastInsertId() const{    if (isActive()) {        qint64 id = sqlite3_last_insert_rowid(d->access);        if (id)            return id;    }    return QVariant();}QSqlRecord QSQLiteResult::record() const{    if (!isActive() || !isSelect())        return QSqlRecord();    return d->rInf;}QVariant QSQLiteResult::handle() const{    return qVariantFromValue(d->stmt);}/////////////////////////////////////////////////////////QSQLiteDriver::QSQLiteDriver(QObject * parent)    : QSqlDriver(parent){    d = new QSQLiteDriverPrivate();}QSQLiteDriver::QSQLiteDriver(sqlite3 *connection, QObject *parent)    : QSqlDriver(parent){    d = new QSQLiteDriverPrivate();    d->access = connection;    setOpen(true);    setOpenError(false);}QSQLiteDriver::~QSQLiteDriver(){    delete d;}bool QSQLiteDriver::hasFeature(DriverFeature f) const{    switch (f) {    case BLOB:    case Transactions:    case Unicode:    case LastInsertId:    case PreparedQueries:    case PositionalPlaceholders:    case SimpleLocking:        return true;    case QuerySize:    case NamedPlaceholders:    case BatchOperations:    case LowPrecisionNumbers:        return false;    }    return false;}static int qGetSqliteTimeout(QString opts){    enum { DefaultTimeout = 5000 };    opts.remove(QLatin1Char(' '));    if (opts.startsWith(QLatin1String("QSQLITE_BUSY_TIMEOUT="))) {        bool ok;        int nt = opts.mid(21).toInt(&ok);        if (ok)            return nt;    }    return DefaultTimeout;}/*   SQLite dbs have no user name, passwords, hosts or ports.   just file names.*/bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, const QString &, int, const QString &conOpts){    if (isOpen())        close();    if (db.isEmpty())        return false;    if (sqlite3_open16(db.constData(), &d->access) == SQLITE_OK) {        sqlite3_busy_timeout(d->access, qGetSqliteTimeout(conOpts));        setOpen(true);        setOpenError(false);        return true;    } else {        setLastError(qMakeError(d->access, tr("Error opening database"),                     QSqlError::ConnectionError));        setOpenError(true);        return false;    }}void QSQLiteDriver::close(){    if (isOpen()) {        if (sqlite3_close(d->access) != SQLITE_OK)            setLastError(qMakeError(d->access, tr("Error closing database"),                                    QSqlError::ConnectionError));        d->access = 0;        setOpen(false);        setOpenError(false);    }}QSqlResult *QSQLiteDriver::createResult() const{    return new QSQLiteResult(this);}bool QSQLiteDriver::beginTransaction(){    if (!isOpen() || isOpenError())        return false;    QSqlQuery q(createResult());    if (!q.exec(QLatin1String("BEGIN"))) {        setLastError(QSqlError(tr("Unable to begin transaction"),                               q.lastError().databaseText(), QSqlError::TransactionError));        return false;    }    return true;}bool QSQLiteDriver::commitTransaction(){    if (!isOpen() || isOpenError())        return false;    QSqlQuery q(createResult());    if (!q.exec(QLatin1String("COMMIT"))) {        setLastError(QSqlError(tr("Unable to commit transaction"),                               q.lastError().databaseText(), QSqlError::TransactionError));        return false;    }    return true;}bool QSQLiteDriver::rollbackTransaction(){    if (!isOpen() || isOpenError())        return false;    QSqlQuery q(createResult());    if (!q.exec(QLatin1String("ROLLBACK"))) {        setLastError(QSqlError(tr("Unable to rollback transaction"),                               q.lastError().databaseText(), QSqlError::TransactionError));        return false;    }    return true;}QStringList QSQLiteDriver::tables(QSql::TableType type) const{    QStringList res;    if (!isOpen())        return res;    QSqlQuery q(createResult());    q.setForwardOnly(true);    QString sql = QLatin1String("SELECT name FROM sqlite_master WHERE %1 "                                "UNION ALL SELECT name FROM sqlite_temp_master WHERE %1");    if ((type & QSql::Tables) && (type & QSql::Views))        sql = sql.arg(QLatin1String("type='table' OR type='view'"));    else if (type & QSql::Tables)        sql = sql.arg(QLatin1String("type='table'"));    else if (type & QSql::Views)        sql = sql.arg(QLatin1String("type='view'"));    else        sql.clear();    if (!sql.isEmpty() && q.exec(sql)) {        while(q.next())            res.append(q.value(0).toString());    }    if (type & QSql::SystemTables) {        // there are no internal tables beside this one:        res.append(QLatin1String("sqlite_master"));    }    return res;}static QSqlIndex qGetTableInfo(QSqlQuery &q, const QString &tableName, bool onlyPIndex = false){    QString schema;    QString table(tableName);    int indexOfSeparator = tableName.indexOf(QLatin1String("."));    if (indexOfSeparator > -1) {        schema = tableName.left(indexOfSeparator).append(QLatin1String("."));        table = tableName.mid(indexOfSeparator + 1);    }    q.exec(QLatin1String("PRAGMA ") + schema + QLatin1String("table_info ('") + table + QLatin1String("')"));    QSqlIndex ind;    while (q.next()) {        bool isPk = q.value(5).toInt();        if (onlyPIndex && !isPk)            continue;        QString typeName = q.value(2).toString().toLower();        QSqlField fld(q.value(1).toString(), qGetColumnType(typeName));        if (isPk && typeName == QLatin1String("integer"))            // integer primary key fields are auto-generated in sqlite            fld.setAutoValue(true);        fld.setRequired(q.value(3).toInt() != 0);        fld.setDefaultValue(q.value(4));        ind.append(fld);    }    return ind;}QSqlIndex QSQLiteDriver::primaryIndex(const QString &tblname) const{    if (!isOpen())        return QSqlIndex();    QSqlQuery q(createResult());    q.setForwardOnly(true);    return qGetTableInfo(q, tblname, true);}QSqlRecord QSQLiteDriver::record(const QString &tbl) const{    if (!isOpen())        return QSqlRecord();    QSqlQuery q(createResult());    q.setForwardOnly(true);    return qGetTableInfo(q, tbl);}QVariant QSQLiteDriver::handle() const{    return qVariantFromValue(d->access);}QString QSQLiteDriver::escapeIdentifier(const QString &identifier, IdentifierType /*type*/) const{    QString res = identifier;    res.replace(QLatin1Char('"'), QLatin1String("\"\""));    res.prepend(QLatin1Char('"')).append(QLatin1Char('"'));    res.replace(QLatin1Char('.'), QLatin1String("\".\""));    return res;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -