Modify records while iterating over them in SQLite - sql

I'm trying to update all the columns of an SQLite database if it meets certain conditions.
I can't just use an UPDATE statement, the actual production database has diverse values, think of it as a game with many "bases" and I need to simulate them producing and consuming resources, and I'm not sure how to do that in SQL.
Here's a Python script to create the sample database where I'm testing this.
Now, the callback's code to modify the table column returns code 5, and the values are not being updated. I'm not sure how to fix this.
import sqlite3
import uuid
db = sqlite3.connect("main.db")
cur = db.cursor()
cur.execute("DROP TABLE IF EXISTS things")
cur.execute("CREATE TABLE IF NOT EXISTS things (numid INTEGER PRIMARY KEY, uuid TEXT, value INTEGER)")
for i in range(1, 2000):
cur.execute(
"INSERT INTO things (uuid, value) VALUES (?, ?)", (str(uuid.uuid4()), 1)
)
db.commit()
And this is the C code that I'm using to go through the entire database. Make sure to link with the SQLite library.
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct things_t
{
int numid;
int value;
} things_t;
static int callback(void *voidthing, int argc, char **argv, char **azColName)
{
int i;
things_t *thing = (things_t *)voidthing;
for (i = 0; i < argc; i++)
{
if (strcmp(azColName[i], "value") == 0)
{
thing->value = atoi(argv[i]);
thing->value++;
}
if (strcmp(azColName[i], "numid") == 0)
{
thing->numid = atoi(argv[i]);
}
}
sqlite3 *db;
int rc = sqlite3_open("main.db", &db);
printf("open file rc: %d (thing numid: %d, thing value: %d)\n", rc,
thing->numid, thing->value);
sqlite3_stmt *stmt;
sqlite3_prepare(db, "UPDATE things SET value = ? WHERE numid = ?", -1,
&stmt, NULL);
sqlite3_bind_int(stmt, 1, thing->value);
sqlite3_bind_int(stmt, 2, thing->numid);
rc = sqlite3_step(stmt);
printf("step rc: %d (thing numid: %d, thing value: %d)\n", rc,
thing->numid, thing->value);
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
int main()
{
char *err = "";
sqlite3 *db;
sqlite3_open("main.db", &db);
things_t thing;
sqlite3_exec(db, "SELECT * FROM things LIMIT 200", callback, &thing,
&err);
sqlite3_close(db);
}

I've decided to just use SQL subqueries to do this.
For example, this query would show how much resources each faction would lose or gain per turn (once again, continuing the game example).
SELECT (SELECT faction_name FROM factions WHERE owner = numid) as owner, SUM(production - consumption) as resdiff FROM buildings GROUP BY owner;
Here's a dump of the sample database where these queries (including the one above) are being tested:
BEGIN TRANSACTION;
CREATE TABLE factions (numid INTEGER PRIMARY KEY, faction_name TEXT, resources INTEGER);
INSERT INTO factions VALUES(1,'Arch users',900);
INSERT INTO factions VALUES(2,'Debian users',912);
INSERT INTO factions VALUES(3,'Gentoo users',100);
CREATE TABLE buildings (numid INTEGER PRIMARY KEY, owner INTEGER, production INTEGER, consumption INTEGER, FOREIGN KEY(owner) REFERENCES factions(numid));
INSERT INTO buildings VALUES(1,1,339,291);
INSERT INTO buildings VALUES(2,1,211,440);
INSERT INTO buildings VALUES(3,1,773,445);
INSERT INTO buildings VALUES(4,2,143,498);
INSERT INTO buildings VALUES(5,2,339,113);
INSERT INTO buildings VALUES(6,3,10,9);
INSERT INTO buildings VALUES(7,2,144,14);
COMMIT;
I'll just update the data with SQL, then extract the results with other queries (and as part of the game example, presumably show them to the player).

Related

How to get the name of the last column from the SQL table without traversing in SQLite3?

My database contains 'n' number of columns.
For example:
Name city phone number ..............,
Exactly I don't know about the total number of columns and name of the last column.
The name of the columns will be changed at run time (such as The name of columns are not constant fields).
#include <stdio.h> /* needed for vsnprintf */
#include <stdlib.h> /* needed for malloc-free */
#include <string.h>
#include <sqlite3.h>
char * sql = PRAGMA table_info(table_name);
int i = 0;
rc = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
printf(" PRAGMA: rc = %d\n", rc);
if(rc != SQLITE_OK )
{
printf("Error: %s:Unable to Query the SQL Column from the table.\n", zErrMsg);
}
else if(rc == SQLITE_OK)
{
while(sqlite3_step(stmt) == SQLITE_ROW)
{
int totCol = sqlite3_column_count(stmt);
for(i=0; i<totCol ;i++)
{
printf("Column-Name: %s\n", sqlite3_column_text(stmt, i));
}
}
}
Through the above pragma command, I am traversing the entire columns in the SQL table and found the name of the last column.
Is there any way to know the name of the last column without traversing the entire columns in the SQL table.
Note: I don't want to do the following things
Executing more than one SQL statement.
Traversing the entire columns to find the last column from the SQL table.
I believe that you can achieve this using the following as the basis :-
SELECT name FROM pragma_table_info('table_name') ORDER BY cid DESC LIMIT 1;
As described in PRAGMA Statements - PRAGMA functions

How to bind a LIKE wildcard in SQLite3

I'd like to run the SQL query: SELECT COUNT(id) AS nbr FROM user WHERE name LIKE '%John%', and get the number of records matched by the keyword John.
So I've written the following code :
unsigned int nbr;
char *sql, *like;
sqlite3 *db;
sqlite3_stmt *stmt;
/* Database connection etc. */
like = _make_string("%%%s%%", keyword); /* keyword: John */
printf("Like clause: %s\n", like);
sql = _make_string("SELECT COUNT(id) AS nbr FROM user WHERE name LIKE ?");
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, like, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) != SQLITE_ROW) {
/* error, SQL execution failed */
}
nbr = sqlite3_column_int64(stmt, 1);
printf("Number of record: %u\n", nbr);
/* Free strings, finalize statement, disconnect db etc. */
For reason of simplicity, some return value tests are ignored in the snippet. _make_string(const char *fmt, ...) is a printf-like function which can form strings, and it did return %John% to me.
However, my code always returns 0 record. I have tried the SQL in sqlite3 command line shell, but it has found 2 records.
Any idea please ?
I've hard coded the LIKE clause and removed the sqlite3_bind_text function, and it doesn't change the result.
sql = _make_string("SELECT COUNT(id) AS nbr FROM client WHERE name LIKE '%John%'");
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (sqlite3_step(stmt) != SQLITE_ROW) {
/* error */
}
nbr = sqlite3_column_int64(stmt, 1);
printf("Number of record: %u\n", nbr);
The program always returns 0 records.
Well, I've found the answer in the mailing list archive of SQLite Why bind indexes start from 1 and column indexes start from 0?
In fact, the bind index starts from 1, but the column index starts from 0. So the code should be modified as the following
nbr = sqlite3_column_int64(stmt, 0);
printf("Number of record: %u.\n", nbr);
Now the program returns 2 (records) !

How to handle errors in a select statement when attempting an insert or fail?

Is there a good way to handle errors in a select statement when attempting an insert or fail? Specifically, I want to insert elements into a table, but the select statement used to generate these elements is failing. I would like to have all the elements where the select statement succeeded to be inserted, but for the overall statement to fail. I thought that insert or fail would do this, but it does not. More specifically, imagine if we defined a new SQLite function "log"
#include <string>
#include <sqlite3ext.h>
#include <cmath>
SQLITE_EXTENSION_INIT1
extern "C" {
int sqlite3_log_init(
sqlite3 * db,
char ** err,
sqlite3_api_routines const * const api
);
}
// Compute the log of the input
void mylog(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
// Grab the number
auto num = sqlite3_value_double(argv[0]);
// If positive, take the log
if(num > 0.)
sqlite3_result_double(context, log(num));
// Otherwise, throw an error
else {
auto msg = std::string("Can't take the log of a nonpositive number");
sqlite3_result_error(context,msg.c_str(),msg.size());
}
}
// Initialize the functions
int sqlite3_log_init(
sqlite3 *db,
char **err,
sqlite3_api_routines const * const api
){
SQLITE_EXTENSION_INIT2(api)
// Register the log function
if( int ret = sqlite3_create_function(
db, "log", 1, SQLITE_ANY, 0, mylog, 0, 0)
) {
*err=sqlite3_mprintf("Error registering log: %s",
sqlite3_errmsg(db));
return ret;
}
// If we've made it this far, we should be ok
return SQLITE_OK;
}
This can be compiled with
g++ -std=c++14 log.cpp -shared -o log.so -fPIC
Basically, the above function takes the log of its element. For example,
sqlite> select log(1);
0.0
sqlite> select log(0);
Error: Can't take the log of a nonpositve number
Now, consider the following sequence of SQL operations
sqlite> .load "./log.so"
sqlite> create table foo (num real);
sqlite> insert into foo values (2.), (1.), (0.);
sqlite> create table bar (num real);
sqlite> insert or fail into bar select log(num) from foo;
Error: Can't take the log of a nonpositve number
sqlite> select * from bar;
sqlite>
Basically, the table bar is empty because the select statement failed on 0. What I want to have happen is for the table bar to contain the elements log(2.) and log(1.), but the error to still be thrown. Is there a way to have that happen?
SQLite's ON CONFLICT clause applies only to UNIQUE, NOT NULL, CHECK, and PRIMARY KEY constraints, so you would not be able to use INSERT OR IGNORE.
Once a user-defined function returns an error, it is not possible to suppress it.
You could say that the function's result is undefined, and let it return NULL (which you can then filter out).
Alternatively, get only those rows which have valid values:
INSERT INTO bar SELECT log(num) FROM foo WHERE num > 0;

unable to add columns to a existing table using C

The following program tries to insert a new row and a new column to an already existing database with a table "people" containing four cols (id, lastname, firstname, phonenumber). While the row gets inserted successfully, the column is not getting added.
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
#include <string.h>
int main()
{
PGconn *conn;
PGresult *res;
int rec_count;
int row;
int col;
conn = PQconnectdb("dbname=test host=localhost user=abc1 password=xyz1");
if(PQstatus(conn) == CONNECTION_BAD) {
puts("We were unable to connect to the database");
exit(0);
}
res = PQexec(conn,"INSERT INTO people VALUES (5, 'XXX', 'YYY', '7633839276');");
if(PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "Insertion Failed1: %s", PQerrorMessage(conn));
PQclear(res);
}
else
printf("Successfully inserted value in Table..... \n");
res = PQexec(conn,"update people set phonenumber=\'5055559999\' where id=3");
if(PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "Insertion Failed2: %s", PQerrorMessage(conn));
PQclear(res);
}
res = PQexec(conn, "ALTER TABLE people ADD comment VARCHAR(100) DEFAULT 'TRUE'");
if(PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "Insertion Failed3: %s", PQerrorMessage(conn));
PQclear(res);
}
rec_count = PQntuples(res);
printf("We received %d records.\n", rec_count);
puts("==========================");
for(row=0; row<rec_count; row++) {
for(col=0; col<3; col++) {
printf("%s\t", PQgetvalue(res, row, col));
}
puts("");
}
puts("==========================");
PQclear(res);
PQfinish(conn);
return 0;
}
The program after being compiled and linked gives the following output:
$ ./test
Successfully inserted value in Table.....
We received 0 records.
==========================
==========================
In the postgresql environment, the table "people" is updated with an extra row and a column containing "TRUE".
This is my first program with C embedded with postgresql. Please help!!
This line:
res = PQexec(conn, "EXEC('UPDATE people SET comment = ''TRUE'';');");
should be:
res = PQexec(conn, "UPDATE people SET comment = 'TRUE'");
The EXEC syntax is part of embedded SQL in C with ECPG. It's not to be combined with PQexec from the C libpq library, which is clearly what you're using given the rest of the source code.
Also the result of an UPDATE with no RETURNING clause has PQresultStatus(res)==PGRES_COMMAND_OK, not PGRES_TUPLES_OK
PGRES_COMMAND_OK
Successful completion of a command returning no data.
See Command Execution Functions
You also want to test any PGresult returned by PQexec, not only the result of your last query, because any query may fail.
How can you retrieve data without doing a SELECT statement ?
Never used PQexec, but i guess the code to add might be like this:
res = PQexec(conn, "ALTER TABLE people ADD comment VARCHAR(100) DEFAULT 'TRUE'");
if(PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "Insertion Failed3: %s", PQerrorMessage(conn));
PQclear(res);
}
//Add PQexec and select statement here.
//Something "like" this.
res = PQexec(conn, "SELECT * FROM people;");
if(PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "Select Failed: %s", PQerrorMessage(conn));
PQclear(res);
}
rec_count = PQntuples(res);
printf("We received %d records.\n", rec_count);

Oracle Pro*C updating table with cursor failed

I have a table like this:
CREATE TABLE book_info (
book_id VARCHAR(32) not null,
title varchar(255) not null,
author varchar(255) not null,
folder_path varchar(255) not null,
primary key(book_id)
);
And i insert this data on it:
insert into book_info values('BOOK1', 'APUE', 'Richard Stevens', '/home/user1/unix_programming_books');
insert into book_info values('BOOK2', 'Unix Network programming', 'Richard Stevens', '/home/user1/unix_programming_books');
insert into book_info values('BOOK3', 'Core Python Applications Programming', 'Wesley J. Chun', '/home/user1/python_programming_books');
I'm trying to update this table using Oracle PRO*C, but i can't! below is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
EXEC SQL INCLUDE SQLCA;
EXEC SQL INCLUDE ORACA;
#define USER_LEN 10
#define PASS_LEN 10
VARCHAR user[USER_LEN];
VARCHAR pass[PASS_LEN];
#define STRCPY_TO_ORA(dest, source)\
dest.len = strlen(source);\
strcpy((char *)dest.arr, (const char *)source)
#define STRCPY_FROM_ORA(dest, source)\
source.arr[source.len] = 0;\
strcpy((char *)dest,(const char *)source.arr)
/* Connecting to the database */
int db_connection(char *db_user, char *db_pass)
{
strncpy((char *) user.arr, db_user, USER_LEN);
user.len = strlen((char *) user.arr);
strncpy((char *) pass.arr, db_pass, PASS_LEN);
pass.len = strlen((char *) pass.arr);
EXEC SQL CONNECT :user IDENTIFIED BY :pass;
if (sqlca.sqlcode != 0)
{
fprintf(stdout, "Connection failed:%s\n", sqlca.sqlerrm.sqlerrmc);
return(sqlca.sqlcode);
}
fprintf(stdout, "Connected to ORACLE as user:%s\n", user.arr);
return (sqlca.sqlcode);
}
int book_not_found_function(char *path)
{
fprintf(stdout, "%s\n", __FUNCTION__);
}
int path_update_success_function(char *book_id, char *new_path)
{
fprintf(stdout, "Update book %s path to %s\n", book_id, new_path);
}
void other_function(void)
{
fprintf(stdout, "%s\n", __FUNCTION__);
}
/* Updating books path */
int books_path_updating(char *old_path, char *new_path)
{
char book_id_string[32];
EXEC SQL BEGIN DECLARE SECTION;
varchar sql_old_path[255];
varchar sql_new_path[255];
varchar sql_book_id[32];
EXEC SQL END DECLARE SECTION;
STRCPY_TO_ORA(sql_old_path, old_path);
STRCPY_TO_ORA(sql_new_path, new_path);
/* Declare a cursor for the FETCH statement. */
EXEC SQL DECLARE books_cursor CURSOR FOR
SELECT BOOK_ID
FROM BOOK_INFO
WHERE FOLDER_PATH = :sql_old_path;
if (sqlca.sqlcode != 0)
{
fprintf(stdout, "Declare cursor failed\n");
fprintf(stdout, "Oracle error %s\n", sqlca.sqlerrm.sqlerrmc);
return(sqlca.sqlcode);
}
EXEC SQL OPEN books_cursor;
if (sqlca.sqlcode != 0)
{
fprintf(stdout, "Open cursor failed\n");
fprintf(stdout, "Oracle error %s\n", sqlca.sqlerrm.sqlerrmc);
return(sqlca.sqlcode);
}
for ( ;; )
{
//EXEC SQL WHENEVER NOT FOUND DO break; // I used it but still nothing
//EXEC SQL WHENEVER NOT FOUND GOTO not_found; // I used this too
//EXEC SQL WHENEVER NOT FOUND DO continue; // I used this too
/* Fetching data */
EXEC SQL FETCH books_cursor
INTO :sql_book_id;
if (sqlca.sqlcode == 1403)
{
fprintf(stdout, "No book found for this folder %s\n", old_path);
book_not_found_function(old_path);
return 0;
}
else if (sqlca.sqlcode != 0)
{
fprintf(stdout, "Oracle error %s\n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL CLOSE books_cursor;
return (sqlca.sqlcode);
}
else
{
STRCPY_FROM_ORA(book_id_string, sql_book_id);
fprintf(stdout, "BOOK_ID = %s\n", book_id_string);
/* Updating the path */
EXEC SQL UPDATE BOOK_INFO
SET FOLDER_PATH =:sql_new_path
WHERE BOOK_ID =:sql_book_id;
if (sqlca.sqlcode != 0)
{
fprintf(stdout, "Oracle error %s\n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL CLOSE books_cursor;
return (sqlca.sqlcode);
}
else
{
path_update_success_function(book_id_string, new_path);
}
}
}
EXEC SQL CLOSE books_cursor;
other_function();
EXEC SQL COMMIT WORK RELEASE;
return 0;
}
int main(int argc, char **argv)
{
db_connection("evariste", "123456");
books_path_updating("/home/user1/unix_programming_books", "/home/user1/UNIX_PROGRAMMING_BOOKS");
books_path_updating("/non_existing_path", "/non_existing_path");
return 0;
}
This program produce the output :
Connected to ORACLE as user:evariste
BOOK_ID = BOOK1
Update book BOOK1 path to /home/user1/UNIX_PROGRAMMING_BOOKS
BOOK_ID = BOOK2
Update book BOOK2 path to /home/user1/UNIX_PROGRAMMING_BOOKS
No book found for this folder /home/user1/unix_programming_books // WHEY THIS?
book_not_found_function // WHY THIS
Declare cursor failed // WHY THIS
Oracle error ORA-01403: no data found // WHY THIS
The table is not updated and the functions path_update_success_function and other_function are never executed! Why this?
Thanks for your help.
Connected to ORACLE as user:evariste
BOOK_ID = BOOK1
Update book BOOK1 path to /home/user1/UNIX_PROGRAMMING_BOOKS
BOOK_ID = BOOK2
Update book BOOK2 path to /home/user1/UNIX_PROGRAMMING_BOOKS
No book found for this folder /home/user1/unix_programming_books // WHEY THIS?
You've fetched past the end of the resultset. Two rows match the cursor this time, so the first two fetches succeed; the third gets no data. This is expected and you shouldn't treat this as an error - just break the loop, which will also cause other_function to be called.
book_not_found_function // WHY THIS
Because you treat 1403 as an error. If you want to call this function when there are no matches, you'll need to count in the loop, and call it afterwards if needed.
Declare cursor failed   // WHY THIS
Oracle error ORA-01403: no data found // WHY THIS
sqlca.sqlcode seems to still be set from the earlier fetch, so this is misleading.
As far as I can remember you'd normally declare the cursor once in the file, not each time the function is called; not sure if Pro*C just ignores the redefinition. You can look at the generated file to see how it deals with it. You also won't get a runtime error from this, if it's wrong it won't (pre-)compile.
The table is not updated and the functions
path_update_success_function and other_function are never executed!
Why this?
path_update_success is called for the first two fetches, but not for the third which fails, and not for the second path because the function returns due to the apparent declare cursor before it gets near it. other_function isn't called because for both calls you return from the function before you can reach it. Similarly the table seems to not be updated because you return before you commit. Unlike SQL*Plus, Pro*C doesn't automatically commit on exit, so there is an implicit rollback. Note also that if you did get to the commit, the release disconnects you, so the second time you'd get a not-connected-to-Oracle error. You should decide to commit/rollback once really, probably right at the end of main.
Untested modification:
int books_path_updating(char *old_path, char *new_path)
{
char book_id_string[32];
int books_found;
EXEC SQL BEGIN DECLARE SECTION;
varchar sql_old_path[255];
varchar sql_new_path[255];
varchar sql_book_id[32];
EXEC SQL END DECLARE SECTION;
STRCPY_TO_ORA(sql_old_path, old_path);
STRCPY_TO_ORA(sql_new_path, new_path);
/* Declare a cursor for the FETCH statement */
EXEC SQL DECLARE books_cursor CURSOR FOR
SELECT BOOK_ID
FROM BOOK_INFO
WHERE FOLDER_PATH = :sql_old_path;
EXEC SQL OPEN books_cursor;
if (sqlca.sqlcode != 0)
{
fprintf(stdout, "Open cursor failed\n");
}
books_found = 0;
while (sqlca.sqlcode == 0)
{
/* Fetching data */
EXEC SQL FETCH books_cursor
INTO :sql_book_id;
if (sqlca.sqlcode != 0)
{
break;
}
STRCPY_FROM_ORA(book_id_string, sql_book_id);
fprintf(stdout, "BOOK_ID = %s\n", book_id_string);
/* Updating the path */
EXEC SQL UPDATE BOOK_INFO
SET FOLDER_PATH = :sql_new_path
WHERE BOOK_ID = :sql_book_id;
if (sqlca.sqlcode != 0)
{
break;
}
/* Track how many books we found, though we only really care later that
* this is non-zero */
books_found++;
path_update_success_function(book_id_string, new_path);
}
EXEC SQL CLOSE books_cursor;
/* Check for and display error, but ignore 1403 as this just indicates the
* end of the result set */
if ((sqlca.sqlcode != 0) && (sqlca.sqlcode != 1403))
{
fprintf(stdout, "Oracle error %s\n", sqlca.sqlerrm.sqlerrmc);
return 1;
}
if (books_found == 0)
{
fprintf(stdout, "No book found for this folder %s\n", old_path);
book_not_found_function(old_path);
return 0;
}
other_function();
return 0;
}
int main(int argc, char **argv)
{
int rc;
rc = db_connection("evariste", "123456");
/* Only do the first path if we didn't get an error connecting */
if (rc == 0)
{
rc == books_path_updating("/home/user1/unix_programming_books",
"/home/user1/UNIX_PROGRAMMING_BOOKS");
}
/* Only do the next path if we didn't get an error from the previous one */
if (rc == 0)
{
rc = books_path_updating("/non_existing_path",
"/non_existing_path");
}
/* Decide whether to rollback or commit; this assumes you don't want to
* keep any changes if there are any errors */
if (rc != 0)
{
EXEC SQL ROLLBACK WORK RELEASE;
return 1;
}
EXEC SQL COMMIT WORK RELEASE;
return 0;
}
Seems DECLARE CURSOR does not set sqlca.sqlcode.
The error you've got was the previous one when you achieved the end of your fetch.
So you should not test errors after a DECLARE CURSOR, but after the OPEN CURSOR only.