0

I have written a user defined function to calculate checksum for a given string. I need to insert the calculated hash value into the table. I used the following method

char msg[] = "Testing of the hash"
len = strlen(msg)
char *sql = "INSERT INTO TestHash (id,CheckSum) VALUES (?,hash(msg,len));"
sqlite3_prepare_v2(db, sql, -1, &res, 0);
sqlite3_step(res)

but sqlite throws a error "failed to prepare insert sql statement:no such column:msg". What is the right way to call a UDF in insert command.

Platform:linux Language:C database:sqlite

Thanks for the help in advance

2 Answers 2

2

You are already using a parameter for the ID value. Do the same for the other two values:

const char *sql = "INSERT INTO TestHash (id,CheckSum) VALUES (?,hash(?,?));"
/* error handling omitted */
rc = sqlite3_prepare_v2(db, sql, -1, &res, NULL);
sqlite3_bind_int(res, 1, ...);
sqlite3_bind_text(res, 2, msg, len, SQLITE_TRANSIENT);
sqlite3_bind_int(res, 3, len);
rc = sqlite3_step(res);

If the ID value should be NULL (for an autoincremented value), you should not use a parameter for it.

Your hash function should not need the length parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

Both your method and Shadow's method work. which is better.
had problem with sqlite_binding_text ,was sending wrong column number for second parameter
1

msg is a C program level variable. In the sql command you cannot reference a C variable, you have to insert its content into the sql statement as a string literal. The same applies to the len variable.

You basically need to generate the following string within your C program:

char *sql = "INSERT INTO TestHash (id,CheckSum) VALUES (?,hash('Testing of the hash',19));"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.