I am relatively new to sqlite using C++, I found a statement like
"INSERT INTO files "
"(md5path_1, md5path_2, parent)"
"VALUES (:md5_1, :md5_2, :p)"
Why are those colons used?
These are called Bind Variables
With the use of bind variables the statement remains the same, therefore there is only one statement cached as opposed to many.
This is taken from the introduction to SQLite
SQLite allows a parameter wherever a string literal, numeric constant,
or NULL is allowed. (Parameters may not be used for column or table
names.) A parameter takes one of the following forms:
?
?NNN
:AAA
$AAA
#AAA
In the examples above, NNN is an integer value
and AAA is an identifier. A parameter initially has a value of NULL.
Prior to calling sqlite3_step() for the first time or immediately
after sqlite3_reset(), the application can invoke the sqlite3_bind()
interfaces to attach values to the parameters. Each call to
sqlite3_bind() overrides prior bindings on the same parameter.
So, it's a placeholder you can bind later to a variable using sqlite3_bind()
They called "bind variables". Later in the code you'll definitely see something like
exec_query("INSERT....", val1, val2, val3)
where val1 - val3 will contain value to be inserted.
Using variables has many advantages. In case of insert statement:
It helps to avoid SQL-injections. Thus no one will be able to ruin your query by passing special statements to variables.
You don't have to compose sql-statement every time you need it. You can just use that one from your question passing new parameters each time you run it
Related
I refer to the following SOW post and the answer of Stuart Rossiter.
I thought it was right to open a new thread about this, as the problem can be looked at a little differently after all these years. Now I get the following error: "The method create_ShiftChange(double, TimeUnits) in the Main type is not applicable for the arguments (int, Integer)."
As I noted in my comment from Stuart Rossiter's solution, I believe the function create_ShiftChange(...) had different input arguments a few years ago.
The cast from getTimeoutToNextValue() to double is not a problem. However, the cast of the second argument getNextValue() from Integer to TimeUnits presents me with a challenge.
Does anyone have a solution for my problem or do I have to look for a detour, since the "old" create_ShiftChange(...) also has a different meaning due to the other input arguments? Thanks for the help!
There hasn't been a change in the create_* functions (methods) for dynamic events. There are two forms:
One where you explicitly specify the time units for when it should be scheduled (so with 2 initial arguments of type double and TimeUnits). TimeUnits is a Java enum (effectively what an AnyLogic option list is under the covers) with values like TimeUnits.MINUTE; auto-complete will show you the alternatives.
One where you implicitly assume the time units of the model as a whole, as in its properties (so with 1 initial argument of type double).
The dynamic event in question has a single int argument (i.e., its 'event-specific' data comprises just an integer), so the relevant create_* function variants have this as their final argument (i.e., they have 3 and 2 arguments respectively).
In your case, you are not using a dynamic event with a single argument (otherwise the method create_ShiftChange(double, TimeUnits) it's complaining about wouldn't exist — it would be create_ShiftChange(double, TimeUnits, int) instead) and, since you've called it with two integers, the compiler (incorrectly) assumes you were trying to use the 2 argument form, hence the error message.
So either add the argument to the dynamic event or, if in your case you're using a different set of arguments (or no arguments) for your dynamic event, change accordingly.
You simply need to type TimeUnits. (note the dot!) and then use code-complete. This shows you all the options you have available, choose the one you need.
Background: This is an enum defined by AnyLogic to be used for time units. When you see things like that, always type it out and try code-complete
I need to get information from a class function. Said class has overloaded operators for basically any standard type. Therefore
double foo = exampleObject.getInformation();
and
std::string faa = exampleObject.getInformation();
Would both work. If the information can not be transformed into a double, foo will be set to 0. The initialization of faa will always work. (It can always be expressed as a string)
My problem is: I want to get the information and save it as a double variable, if that can not be done as the information is not of numeric type, I want the variable to be a string. I basically need a variable that can change its type. How do I do this? I'm sorry if that is a very basic question, C++ is not my main programming language.
Have you tried using Function Templates?
They won't change the type of a variable but will allow you to write your code in a way that works with more than 1 data type.
If c++ is not your main, I would recommend checking the checking the documentation for Function Templates on cplusplus.com
Here => https://cplusplus.com/doc/oldtutorial/templates/
I have a C++ binary library.
There are almost one hundred C++ functions with different name, but same parameter types and same return type. And the values they parameter can be are equal.
I now want to test all of them with all of parameter values. And maybe return values to txt files.
How can I realize this? I want to store function names in a string list, and use eval() like python, but C++ doesn't have this feature.
Thank you!
In C++ you can create an array of function pointers and then loop over that array, calling each of the functions and giving always the same values to parameters from corresponding unchanging variables.
I was told a long time ago that in FORTRAN, everything is passed by value. Therefore I would need to do this (provided mySubroutine is suitably defined elsewhere):
double precision :: myArray(2)
myArray(1:2) = (/ 2.3d0, 1.5d0 /)
CALL mySubroutine(myArray)
However, I also found that the program compiles and runs as expected if I do this
CALL mySubroutine((/ 2.3d0, 1.5d0 /))
without needing to define an intermediary array myArray. I thought that I was passing myArray into mySubroutine by reference. What is going on under the hood in the second version? Is the compiler unpacking the subroutine call, declaring a temporary variable only to pass it by reference?
To a large extent, trying to classify Fortran procedure calling with pass-by-reference and pass-by-value is not too helpful. You can find more detail on that in response to questions like this one and this one.
In short, generally procedure references are such that changes to a variable in a procedure are reflected in the variable where the procedure was referenced. In some cases a compiler may choose to do copy-in/copy-out, and in others it effectively must. Equally, the value attribute of a dummy argument specifies that an anonymous copy be made.
Where this question adds something a little different is in the use of an expression such as in
call mySubroutine([2.3d0, 1.5d0]) ! Using F2003 array constructor syntax
Is the compiler creating a temporary variable?
Admittedly, this is perhaps just a looseness in terminology but it's worth saying that there is certainly no variable involved. [2.3d0, 1.5d0] is an expression, not a variable. Crucially this means that it cannot be modified (appear in a variable definition context) in the procedure. Restrictions that apply in the case using an expression rather than a (temporary) variable include:
the dummy argument associated with an expression may not have the intent(inout) or the intent(out) attribute;
if the dummy argument hasn't an intent attribute then that argument may not be modified if the associated actual argument is an expression.
Now, if the dummy argument has the value attribute the effect of the procedure is the same whichever way it is referenced.
To conclude, the program may work just as well with an expression instead of an intermediate variable. If it doesn't that's because of violation of some aspect of Fortran. How it works is a problem for the compiler not the programmer.
What is the exact difference between scalar SQL functions and aggregate SQL functions in SQLite?
I am getting a problem with the following code when attempting to create a new function in SQLite:
int sqlite3_create_function(
sqlite3 *db,
const char *zFunctionName,
int nArg,
int eTextRep,
void *pApp,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*)
);
For a scalar SQL function, xStep and xFinal must be null, and for an aggregate SQL function, xFunc must be null. I need to know which one to use and what the difference is.
A scalar function is a function that operates on scalar values -- that is, it takes one (or more) input values as arguments directly and returns a value.
An aggregate function is a function that operates on aggregate data -- that is, it takes a complete set of data as input and returns a value that is computed from all the values in the set.
By the way, these are the standard definitions of "scalar" and "aggregate" that you can find in any dictionary, and all these links are in the top five Google search results if you search for "scalar function" and "aggregate function". That's OK, we want StackOverflow to become the "definitive" answer for questions like this, but in the interest of "teaching a man to fish" I feel compelled to remind you that you should do your own homework before asking other people to help answer your questions.