int wxSqlitePreparedStatement::GetParameterCount()
{
  ResetErrorCodes();

  int nReturn = 0;
  wxSqliteStatementVector::iterator start = m_Statements.begin();
  wxSqliteStatementVector::iterator stop = m_Statements.end();
  while (start != stop)
  {
    nReturn += sqlite3_bind_parameter_count((sqlite3_stmt*)(*start));
    start++;
  }
  return nReturn;
}
Exemplo n.º 2
0
static void objv_db_sqlite_bind_data(objv_zone_t * zone,sqlite3_stmt * stmt,objv_object_t * data, sqlite3_destructor_type type) {

    int c = sqlite3_bind_parameter_count(stmt);

    for(int i=1; i<=c; i++) {

        const char * name = sqlite3_bind_parameter_name(stmt, i);
        objv_object_t * key = NULL;
        objv_object_t * v;

        if(name) {
            key = (objv_object_t *) objv_string_new(zone, name);
        }
        else {
            key = (objv_object_t *) objv_string_new_format(zone, "@%d",i -1);
        }

        v = objv_object_objectValueForKey(data, key, NULL);

        if(v == NULL) {
            sqlite3_bind_null(stmt, i);
        }
        else if(objv_object_isKindOfClass(v, OBJV_CLASS(Value))) {
            {
                objv_value_t * value = (objv_value_t *) v;
                if(value->type == OBJV_TYPE(int)) {
                    sqlite3_bind_int(stmt, i, value->intValue);
                }
                else if(value->type == OBJV_TYPE(uint)) {
                    sqlite3_bind_int(stmt, i, value->uintValue);
                }
                else if(value->type == OBJV_TYPE(long)) {
                    sqlite3_bind_int64(stmt, i, value->longValue);
                }
                else if(value->type == OBJV_TYPE(ulong)) {
                    sqlite3_bind_int64(stmt, i, value->ulongValue);
                }
                else if(value->type == OBJV_TYPE(longLong)) {
                    sqlite3_bind_int64(stmt, i, value->longLongValue);
                }
                else if(value->type == OBJV_TYPE(ulongLong)) {
                    sqlite3_bind_int64(stmt, i, value->ulongLongValue);
                }
                else if(value->type == OBJV_TYPE(float)) {
                    sqlite3_bind_double(stmt, i, value->floatValue);
                }
                else if(value->type == OBJV_TYPE(double)) {
                    sqlite3_bind_double(stmt, i, value->doubleValue);
                }
                else if(value->type == OBJV_TYPE(boolean)) {
Exemplo n.º 3
0
static int
sqlite3_clear_bindings (sqlite3_stmt *stmt)
{
  int n = sqlite3_bind_parameter_count (stmt);
  int index;

  for (index = 1; index <= n; index++)
    {
      int result = sqlite3_bind_null (stmt, index);
      if (result != SQLITE_OK)
	return result;
    }

  return SQLITE_OK;
}
Exemplo n.º 4
0
ECode CStmt::BindParameterCount(
    /* [out] */ Int32* count)
{
    VALIDATE_NOT_NULL(count);
#if HAVE_SQLITE3 && HAVE_SQLITE_COMPILE
    hvm *v = (hvm *)mHandle;

    if (v && v->vm && v->h) {
        *count = sqlite3_bind_parameter_count((sqlite3_stmt *) v->vm);
    } else {
        return E_NULL_POINTER_EXCEPTION;
    }
#else
    return E_SQL_FEATURE_NOT_SUPPORTED_EXCEPTION;
#endif
    return NOERROR;
}
Exemplo n.º 5
0
bool SQLiteCommand::bindParameterByIndex(size_t index, shared_ptr<DBValue> value) {
	if (index == 0) {
		return true;
	}
	const size_t size = myStatements.size();
	int number = index;
	for (size_t i = 0; i < size; ++i) {
		sqlite3_stmt *statement = myStatements[i];
		const int count = sqlite3_bind_parameter_count(statement);
		if (number > count) {
			number -= count;
			continue;
		}
		return bindParameter(statement, number, value);
	}
	return true;
}
Exemplo n.º 6
0
int CppSQLite3Statement::bindParameterIndex(const char* szParam)
{
	checkVM();

	int nParam = sqlite3_bind_parameter_index(mpVM, szParam);

int nn = sqlite3_bind_parameter_count(mpVM);
const char* sz1 = sqlite3_bind_parameter_name(mpVM, 1);
const char* sz2 = sqlite3_bind_parameter_name(mpVM, 2);

	if (!nParam)
	{
		char buf[128];
		sprintf(buf, "Parameter '%s' is not valid for this statement", szParam);
		throw CppSQLite3Exception(CPPSQLITE_ERROR, buf, DONT_DELETE_MSG);
	}

	return nParam;
}
Exemplo n.º 7
0
 void Statement::initParameters() {
     if (!_stmt) {
         return;
     }
     
     clearParameters();
     
     _parametersCount = sqlite3_bind_parameter_count(_stmt);
     if (_parametersCount <= 0) {
         return;
     }
     
     for (int i = 1; i <= _parametersCount; ++i) {
         const char *name = sqlite3_bind_parameter_name(_stmt, i);
         if (name && name[0]) {
             _nameParameters[name] = i;
         }
     }
 }
Exemplo n.º 8
0
static int dbd_sqlite3_pvquery(apr_pool_t *pool, apr_dbd_t *sql, int *nrows,
                               apr_dbd_prepared_t *statement, va_list args)
{
    const char **values;
    int i, nargs;

    if (sql->trans && sql->trans->errnum) {
        return sql->trans->errnum;
    }

    nargs = sqlite3_bind_parameter_count(statement->stmt);
    values = apr_palloc(pool, sizeof(*values) * nargs);

    for (i = 0; i < nargs; i++) {
        values[i] = apr_pstrdup(pool, va_arg(args, const char*));
    }

    return dbd_sqlite3_pquery(pool, sql, nrows, statement, nargs, values);
}
Exemplo n.º 9
0
static char *sql_pwdb_lookup(const char *user)
{
	sqlite3 *db = srv.db_cxn;
	sqlite3_stmt *stmt = NULL;
	int rc, step = 0;
	char *password = NULL;

	if (debugging > 1)
		applog(LOG_DEBUG, "pw lookup for %s", user ? user : "******");

	if (!user)
		return NULL;

	step++;
	rc = sqlite3_prepare_v2(db, srv.db_stmt_pwdb,
				strlen(srv.db_stmt_pwdb), &stmt, NULL);
	if (rc != SQLITE_OK)
		goto err_out;

	step++;
	if (sqlite3_bind_parameter_count(stmt)) {
		rc = sqlite3_bind_text(stmt, 1, user, strlen(user), SQLITE_STATIC);
		if (rc != SQLITE_OK)
			goto err_out;
	}

	step++;
	rc = sqlite3_step(stmt);
	if (rc == SQLITE_ROW)
		password = strdup((char *)sqlite3_column_text(stmt, 0));
	else if (rc != SQLITE_DONE)
		goto err_out;

	sqlite3_finalize(stmt);
	return password;

err_out:
	applog(LOG_ERR, "pwdb sql step %d failed: %s",
	       step, sqlite3_errmsg(db));
	sqlite3_finalize(stmt);
	return NULL;
}
/* ctor */
sqlite_statement::sqlite_statement (
  sqlite _database
, char const * _query
)
/*
 * index starts at 1 and then
 * increments.
 */
: stmt (NULL)
  /*
   * The following state cannot be set only thrown, and
   * signifies a new un-stepped statement.
   */
, state (SQLITE_MISUSE)
, db (_database)
, column_count (0)
, stepped(false)
, bind_parameter_count (0)
// will be set to 1 for when auto binding.
, index (0) {

sqlite_check_error ( sqlite3_prepare_v2 (
  _database.get()
, _query
, -1 /* Query must be alawys null terminated. */
, & (this->stmt)
, 0 /* There is never an unused portion of the statement. */
));
  if (this->stmt == NULL)
  throw sqlite_exception("Null statement", SQLITE_MISUSE);

this->bind_parameter_count
  = sqlite3_bind_parameter_count(this->stmt);
  /*
   * If the statement has no parameters to bind, then step
   * right away.
   * if step finds a result, the statement will be treated
   * like a input statement. otherwise complete.
   */
  if (0 == bind_parameter_count) this->step();
}
Exemplo n.º 11
0
ECode CStmt::Bind(
    /* [in] */ Int32 pos,
    /* [in] */ const ArrayOf<Byte>& value)
{
#if HAVE_SQLITE3 && HAVE_SQLITE_COMPILE
    hvm *v = (hvm *)mHandle;

    if (v && v->vm && v->h) {
        Int32 npar = sqlite3_bind_parameter_count((sqlite3_stmt *) v->vm);
        Int32 ret = 0;
        Int32 len = 0;

        if (pos < 1 || pos > npar) {
            return E_ILLEGAL_ARGUMENT_EXCEPTION;
        }

        if (value.GetPayload()) {
            len = value.GetLength();
            if (len > 0) {
                ret = sqlite3_bind_blob((sqlite3_stmt *) v->vm,
                            pos, value.GetPayload(), len, sqlite3_free);
            } else {
                ret = sqlite3_bind_blob((sqlite3_stmt *) v->vm,
                            pos, "", 0, SQLITE_STATIC);
            }
        } else {
            ret = sqlite3_bind_null((sqlite3_stmt *) v->vm, pos);
        }
        if (ret != SQLITE_OK) {
            mError_code = ret;
            return E_SQL_EXCEPTION;
        }
    } else {
        return E_NULL_POINTER_EXCEPTION;
    }
#else
    return E_SQL_FEATURE_NOT_SUPPORTED_EXCEPTION;
#endif
    return NOERROR;
}
Exemplo n.º 12
0
// doc: The sqlite3_bind_*() routines must be called after sqlite3_prepare() or sqlite3_reset() and before sqlite3_step().
//      Bindings are not cleared by the sqlite3_reset() routine. Unbound parameters are interpreted as NULL.
bool SqliteSetupBindings(JSContext *cx, sqlite3_stmt *pStmt, JS::HandleObject argObj, JS::HandleObject curObj) {

	JS::RootedValue val(cx);
	int anonParamIndex = 0;
	const char *name;

	int count = sqlite3_bind_parameter_count(pStmt);
	for ( int param = 1; param <= count; param++ ) { // The first host parameter has an index of 1, not 0.

		// doc: Parameters of the form "?" have no name. ... If the value n is out of range or if the n-th parameter is nameless, then NULL is returned.
		name = sqlite3_bind_parameter_name(pStmt, param);

		// doc: name is UTF-8 encoding

		if ( name == NULL ) {

			JL_CHKM(argObj, E_PARAM, E_NUM(anonParamIndex), E_DEFINED);
			JL_CHK( JL_GetElement(cx, argObj, anonParamIndex, &val) ); // works with {0:2,1:2,2:2,length:3} and [2,2,2]
			anonParamIndex++;
		} else
		if ( name[0] == '@' ) {

			JL_CHKM(argObj, E_PARAM, E_NAME(name), E_DEFINED);
			JL_CHK( JS_GetProperty(cx, argObj, name+1, &val) );
		} else
		if ( name[0] == ':' ) {
			
			JL_CHKM(curObj, E_PARAM, E_NAME(name), E_DEFINED);
			JL_CHK( JS_GetProperty(cx, curObj, name+1, &val) );
		} else {

			JL_ERR(E_PARAM, E_NOTSUPPORTED);
		}
		
		JL_CHK( jsvalToSqlite( cx, SqliteTargetBind( pStmt, param ), val ) );
	}
	return true;
	JL_BAD;
}
Exemplo n.º 13
0
static int
bindtable(lua_State *T, sqlite3_stmt *stmt)
{
	int parameters = sqlite3_bind_parameter_count(stmt);
	const char *name;
	const char *err;
	int i;

	for (i = 1; i <= parameters; i++) {
		int ret;

		name = sqlite3_bind_parameter_name(stmt, i);
		lua_settop(T, 2);
		if (name == NULL || name[0] == '?')
			lua_rawgeti(T, 2, i);
		else if (name[0] == '@')
			lua_getfield(T, 2, name + 1);
		else
			lua_getfield(T, 2, name);

		ret = _bind_arg(T, 3, stmt, i);

		if (ret != SQLITE_OK) {
			err = sqlite3_errmsg(sqlite3_db_handle(stmt));
			goto error;
		}
	}
	return 0;

error:
	(void)sqlite3_clear_bindings(stmt);
	luaL_where(T, 1);
	if (name == NULL || name[0] == '?')
		lua_pushfstring(T, "error binding %d: %s", i, err);
	else
		lua_pushfstring(T, "error binding '%s': %s", name, err);
	lua_concat(T, 2);
	return -1;
}
Exemplo n.º 14
0
        bool insert_record(record_type const& rec) const
        {
#ifdef MAPNIK_DEBUG
            assert( unsigned(sqlite3_bind_parameter_count(stmt_)) == rec.size());
#endif
            record_type::const_iterator itr = rec.begin();
            record_type::const_iterator end = rec.end();
            int count = 1;
            for (; itr!=end;++itr)
            {
                binder op(stmt_,count++);
                if (!boost::apply_visitor(op,*itr))
                {
                    return false;
                }
            }

            sqlite3_step(stmt_);
            sqlite3_reset(stmt_);

            return true;
        }
Exemplo n.º 15
0
static GError *
_stmt_apply_GV_parameters(sqlite3_stmt *stmt, GVariant **params)
{
	int i;
	GVariant *p;
	guint count_binds, count_params;

	count_binds = sqlite3_bind_parameter_count(stmt);
	count_params = g_strv_length((gchar**)params);

	if (count_params != count_binds)
		return NEWERROR(CODE_INTERNAL_ERROR, "Bad parameters : %u expected, %u received",
				count_binds, count_params);

	for (i=1; (p=*params) ;i++,params++) {
		GError *err = _stmt_apply_GV_parameter(stmt, i, p);
		if (NULL != err)
			return err;
	}

	return NULL;
}
Exemplo n.º 16
0
int wxSqlitePreparedStatement::FindStatementAndAdjustPositionIndex(int* pPosition)
{
  // Don't mess around if there's just one entry in the vector
  if (m_Statements.size() == 0)
    return 0;
    
  // Go through all the elements in the vector
  // Get the number of parameters in each statement
  // Adjust the nPosition for the the broken up statements
  for (unsigned int i=0; i<m_Statements.size(); i++)
  {
    int nParametersInThisStatement = sqlite3_bind_parameter_count(m_Statements[i]);
    if (*pPosition > nParametersInThisStatement)
    {
      *pPosition -= nParametersInThisStatement;    // Decrement the position indicator by the number of parameters in this statement
    }
    else
    {
      // We're in the correct statement, return the index
      return i;
    }
  }
  return -1;
}
Exemplo n.º 17
0
void SQLiteCommand::prepareBindContext() {
	if (myBindContext.size() > 0) {
		return;
	}

	size_t number = 0;

	for (size_t i = 0; i < myStatements.size(); ++i) {
		sqlite3_stmt *statement = myStatements[i];
		const int count = sqlite3_bind_parameter_count(statement);
		for (int j = 1; j <= count; ++j) {
			++number;
			const char *name = sqlite3_bind_parameter_name(statement, j);
			if (name == 0) {
				myBindContext.push_back(BindParameter(number));
			} else {
				const std::string namestr(name);
				if (std::find_if(myBindContext.begin(), myBindContext.end(), BindParameterComparator(namestr)) == myBindContext.end()) {
					myBindContext.push_back(BindParameter(number, namestr));
				}
			}
		}
	}
}
Exemplo n.º 18
0
int CppSQLite3Statement::bindParameterIndex(const char* szParam)
{
    checkVM();

    int nParam = sqlite3_bind_parameter_index(mpVM, szParam);

    /*int nn =*/ sqlite3_bind_parameter_count(mpVM);
    /*const char* sz1 =*/ sqlite3_bind_parameter_name(mpVM, 1);
    /*const char* sz2 =*/ sqlite3_bind_parameter_name(mpVM, 2);

    if (!nParam)
    {
        char buf[128];
        #if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
            sprintf_s(buf, 128, "Parameter '%s' is not valid for this statement", szParam);
        #else
            sprintf(buf, "Parameter '%s' is not valid for this statement", szParam);
        #endif

        throw CppSQLite3Exception(CPPSQLITE_ERROR, buf, DONT_DELETE_MSG);
    }

    return nParam;
}
Exemplo n.º 19
0
void SQLiteStatementImpl::bindImpl()
{
	_stepCalled = false;
	_nextResponse = 0;
	if (_pStmt == 0) return;

	sqlite3_reset(_pStmt);

	int paramCount = sqlite3_bind_parameter_count(_pStmt);
	if (0 == paramCount)
	{
		_canBind = false;
		return;
	}

	BindIt bindEnd = bindings().end();
	std::size_t availableCount = 0;
	Bindings::difference_type bindCount = 0;
	Bindings::iterator it = _bindBegin;
	for (; it != bindEnd; ++it)
	{
		availableCount += (*it)->numOfColumnsHandled();
		if (availableCount <= paramCount) ++bindCount;
		else break;
	}

	if (availableCount < paramCount)
		throw ParameterCountMismatchException();

	Bindings::difference_type remainingBindCount = bindEnd - _bindBegin;
	if (bindCount < remainingBindCount)
	{
		bindEnd = _bindBegin + bindCount;
		_canBind = true;
	}
	else if (bindCount > remainingBindCount)
		throw ParameterCountMismatchException();

	std::size_t boundRowCount;
	if (_bindBegin != bindings().end())
	{
		boundRowCount = (*_bindBegin)->numOfRowsHandled();

		Bindings::iterator oldBegin = _bindBegin;
		for (std::size_t pos = 1; _bindBegin != bindEnd && (*_bindBegin)->canBind(); ++_bindBegin)
		{
			if (boundRowCount != (*_bindBegin)->numOfRowsHandled())
				throw BindingException("Size mismatch in Bindings. All Bindings MUST have the same size");

			(*_bindBegin)->bind(pos);
			pos += (*_bindBegin)->numOfColumnsHandled();
		}

		if ((*oldBegin)->canBind())
		{
			//container binding will come back for more, so we must rewind
			_bindBegin = oldBegin;
			_canBind = true;
		}
		else _canBind = false;
	}
}
Exemplo n.º 20
0
int Database::Statement::parametersCount(void) const
{
	return sqlite3_bind_parameter_count(mStmt);
}
Exemplo n.º 21
0
__declspec(dllexport) int WINAPI sqlite3_bind_parameter_count_interop(sqlite3_stmt *stmt)
{
  return sqlite3_bind_parameter_count(stmt);
}
Exemplo n.º 22
0
static tb_bool_t tb_database_sqlite3_statement_bind(tb_database_sql_impl_t* database, tb_database_sql_statement_ref_t statement, tb_database_sql_value_t const* list, tb_size_t size)
{
    // check
    tb_database_sqlite3_t* sqlite = tb_database_sqlite3_cast(database);
    tb_assert_and_check_return_val(sqlite && sqlite->database && statement && list && size, tb_false);

    // the param count
    tb_size_t param_count = (tb_size_t)sqlite3_bind_parameter_count((sqlite3_stmt*)statement);
    tb_assert_and_check_return_val(size == param_count, tb_false);
   
    // walk
    tb_size_t i = 0;
    for (i = 0; i < size; i++)
    {
        // the value
        tb_database_sql_value_t const* value = &list[i];

        // done
        tb_int_t    ok = SQLITE_ERROR;
        tb_byte_t*  data = tb_null;
        switch (value->type)
        {
        case TB_DATABASE_SQL_VALUE_TYPE_TEXT:
            tb_trace_i("sqlite3: test %lu %s", i, value->u.text.data);
            ok = sqlite3_bind_text((sqlite3_stmt*)statement, (tb_int_t)(i + 1), value->u.text.data, (tb_int_t)tb_database_sql_value_size(value), tb_null);
            break;
        case TB_DATABASE_SQL_VALUE_TYPE_INT64:
        case TB_DATABASE_SQL_VALUE_TYPE_UINT64:
            ok = sqlite3_bind_int64((sqlite3_stmt*)statement, (tb_int_t)(i + 1), tb_database_sql_value_int64(value));
            break;
        case TB_DATABASE_SQL_VALUE_TYPE_INT32:
        case TB_DATABASE_SQL_VALUE_TYPE_INT16:
        case TB_DATABASE_SQL_VALUE_TYPE_INT8:
        case TB_DATABASE_SQL_VALUE_TYPE_UINT32:
        case TB_DATABASE_SQL_VALUE_TYPE_UINT16:
        case TB_DATABASE_SQL_VALUE_TYPE_UINT8:
            ok = sqlite3_bind_int((sqlite3_stmt*)statement, (tb_int_t)(i + 1), (tb_int_t)tb_database_sql_value_int32(value));
            break;
        case TB_DATABASE_SQL_VALUE_TYPE_BLOB16:
        case TB_DATABASE_SQL_VALUE_TYPE_BLOB8:
            ok = sqlite3_bind_blob((sqlite3_stmt*)statement, (tb_int_t)(i + 1), value->u.blob.data, (tb_int_t)value->u.blob.size, tb_null);
            break;
        case TB_DATABASE_SQL_VALUE_TYPE_BLOB32:
            {
                if (value->u.blob.stream)
                {
                    // done
                    do
                    {
                        // the stream size
                        tb_hong_t size = tb_stream_size(value->u.blob.stream);
                        tb_assert_and_check_break(size >= 0);

                        // make data
                        data = tb_malloc0_bytes((tb_size_t)size);
                        tb_assert_and_check_break(data);

                        // read data
                        if (!tb_stream_bread(value->u.blob.stream, data, (tb_size_t)size)) break;

                        // bind it
                        ok = sqlite3_bind_blob((sqlite3_stmt*)statement, (tb_int_t)(i + 1), data, (tb_int_t)size, tb_database_sqlite3_statement_bind_exit);

                    } while (0);
                }
                else ok = sqlite3_bind_blob((sqlite3_stmt*)statement, (tb_int_t)(i + 1), value->u.blob.data, (tb_int_t)value->u.blob.size, tb_null);
            }
            break;
#ifdef TB_CONFIG_TYPE_HAVE_FLOAT
        case TB_DATABASE_SQL_VALUE_TYPE_FLOAT:
        case TB_DATABASE_SQL_VALUE_TYPE_DOUBLE:
            ok = sqlite3_bind_double((sqlite3_stmt*)statement, (tb_int_t)(i + 1), (tb_double_t)tb_database_sql_value_double(value));
            break;
#endif
        case TB_DATABASE_SQL_VALUE_TYPE_NULL:
            ok = sqlite3_bind_null((sqlite3_stmt*)statement, (tb_int_t)(i + 1));
            break;
        default:
            tb_trace_e("statement: bind: unknown value type: %lu", value->type);
            break;
        }

        // failed?
        if (SQLITE_OK != ok)
        {
            // exit data
            if (data) tb_free(data);
            data = tb_null;

            // save state
            sqlite->base.state = tb_database_sqlite3_state_from_errno(sqlite3_errcode(sqlite->database));

            // trace
            tb_trace_e("statement: bind value[%lu] failed, error[%d]: %s", i, sqlite3_errcode(sqlite->database), sqlite3_errmsg(sqlite->database));
            break;
        }
    }

    // ok?
    return (i == size)? tb_true : tb_false;
}
Exemplo n.º 23
0
unsigned SQLiteStatement::bindParameterCount() const {
  ASSERT(m_isPrepared);
  if (!m_statement)
    return 0;
  return sqlite3_bind_parameter_count(m_statement);
}
Exemplo n.º 24
0
bool QSpatiaLiteResult::exec()
{
    const QVector<QVariant> values = boundValues();

    d->skippedStatus = false;
    d->skipRow = false;
    d->rInf.clear();
    clearValues();
    setLastError(QSqlError());

    int res = sqlite3_reset(d->stmt);
    if (res != SQLITE_OK) {
        setLastError(qMakeError(d->access, QCoreApplication::translate("QSpatiaLiteResult",
                     "Unable to reset statement"), QSqlError::StatementError, res));
        d->finalize();
        return false;
    }
    int paramCount = sqlite3_bind_parameter_count(d->stmt);
    if (paramCount == values.count()) {
        for (int i = 0; i < paramCount; ++i) {
            res = SQLITE_OK;
            const QVariant value = values.at(i);

            if (value.isNull()) {
                res = sqlite3_bind_null(d->stmt, i + 1);
            } else {
                switch (value.type()) {
                case QVariant::ByteArray: {
                    const QByteArray *ba = static_cast<const QByteArray*>(value.constData());
                    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::UInt:
                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("QSpatiaLiteResult",
                             "Unable to bind parameters"), QSqlError::StatementError, res));
                d->finalize();
                return false;
            }
        }
    } else {
        setLastError(QSqlError(QCoreApplication::translate("QSpatiaLiteResult",
                        "Parameter count mismatch"), QString(), QSqlError::StatementError));
        return false;
    }
    d->skippedStatus = d->fetchNext(d->firstRow, 0, true);
    if (lastError().isValid()) {
        setSelect(false);
        setActive(false);
        return false;
    }
    setSelect(!d->rInf.isEmpty());
    setActive(true);
    return true;
}
Exemplo n.º 25
0
int main(int argc, char *argv[]) {
    sqlite3 *db = NULL;
    sqlite3_stmt *stmt = NULL;
    int flags = SQLITE_OPEN_READWRITE;
    int rc = 0;
    int idx = -1;
    /* Stringa SQL con i parametri nominativi - named parameters.
    A ogni stringa alfanumerica, usata nelle tre versioni supportate, viene
    associato dalla funzione sqlite3_prepare_v2() un indice numerico. */
    char *sql_str = "INSERT INTO addressbook (fullname, alias, email)"
                    "VALUES(:name, @aka, $mail)";

    if (argc != 5) {
        fprintf(stderr, "Usage: %s <DB>,<name>,<alias>,<email>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* Le stringhe ricevute in input diventano costanti */
    const char *const str_fullname = (argc && argv[2]) ? argv[2] : "";
    const char *const str_alias = (argc && argv[3]) ? argv[3] : "";
    const char *const str_email = (argc && argv[4]) ? argv[4] : "";

    // Library initialization
    if (sqlite3_initialize() != SQLITE_OK) {
        fprintf(stderr, "Err. Unable to initialize the library.\n");
        exit(EXIT_FAILURE);
    }

    // Open database connection
    rc = sqlite3_open_v2(argv[1], &db, flags, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Err. can't create database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(EXIT_FAILURE);
    }

    // Prepare the statement
    rc = sqlite3_prepare_v2(db, sql_str,-1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Err. Can't prepare the statement.\n");
        sqlite3_close(db);
        exit(EXIT_FAILURE);
    }

    /* Bind del primo parametro
    Si utilizza la funzione sqlite3_bind_text() per associare il parametro al
    valore che in questo caso e' 'text', poiche' trattasi di stringa.
    L'indice del parametro viene salvato nella variabile intera 'idx', ottenuto
    dal valore di ritorno della funzione sqlite3_bind_parameter_index(): */
    idx = sqlite3_bind_parameter_index(stmt, ":name");
    rc = sqlite3_bind_text(stmt, idx, str_fullname, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Err. Binding the value (%i).\n", rc);
        return 1;
    }

    // Bind del secondo parametro
    idx = sqlite3_bind_parameter_index(stmt, "@aka");
    rc = sqlite3_bind_text(stmt, idx, str_alias, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Err. Binding the value (%i).\n", rc);
        return 1;
    }

    // Bind del terzo parametro
    idx = sqlite3_bind_parameter_index(stmt, "$mail");
    rc = sqlite3_bind_text(stmt, idx, str_email, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Err. Binding the value (%i).\n", rc);
        return 1;
    }

    /* Stampa del totale dei parametri, del nome degli stessi e del 
    rispettivo indice, mediante le funzioni specifiche */
    printf("Total bound parameters: %d\n", sqlite3_bind_parameter_count(stmt));
    
    printf("\'%5s\' index \'%d\'\n", sqlite3_bind_parameter_name(stmt, 1), \
            sqlite3_bind_parameter_index(stmt, ":name"));
    printf("\'%5s\' index \'%d\'\n", sqlite3_bind_parameter_name(stmt, 2), \
            sqlite3_bind_parameter_index(stmt, "@aka"));
    printf("\'%5s\' index \'%d\'\n", sqlite3_bind_parameter_name(stmt, 3), \
            sqlite3_bind_parameter_index(stmt, "$mail"));

    // Execute the statement
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE) {
        fprintf(stderr, "Err. Stepping through the statement.\n");
    } else
        puts("... Statement successfully executed.");

    // Release prepared statement resources
    sqlite3_finalize(stmt);
    // Close database connection
    sqlite3_close_v2(db);
    // Shutdown library initialization
    sqlite3_shutdown();

    return(EXIT_SUCCESS);
}
Exemplo n.º 26
0
uint32 Statement::neededParameters()
{
	return sqlite3_bind_parameter_count(m_statement);
}
Exemplo n.º 27
0
CAMLprim value caml_sqlite3_bind_parameter_count(value v_stmt)
{
  sqlite3_stmt *stmt = safe_get_stmtw("bind_parameter_count", v_stmt)->stmt;
  return Val_int(sqlite3_bind_parameter_count(stmt));
}
Exemplo n.º 28
0
CSQLite3Statement::CSQLite3Statement(sqlite3* pDB, sqlite3_stmt* pVM)
{
    m_pDB = pDB;
    m_pVM = pVM;
    m_iCols =  sqlite3_bind_parameter_count(m_pVM);
}
static jint nativeGetParameterCount(JNIEnv* env, jclass clazz, jlong connectionPtr,
        jlong statementPtr) {
    sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);

    return sqlite3_bind_parameter_count(statement);
}
Exemplo n.º 30
0
 int parameters()
 {
    return sqlite3_bind_parameter_count(this->stmt);
 }