0

I'm trying to learn sqlite using c and I have a table called 'cells'. There is a possibility that the cell values might be identical, and so instead of inserting another row for every identical value, I want to instead increase a column called rayHits by 1 for every identical row. I've figured out how to create the table using c:

#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int main()
{
    char* err;
    sqlite3* db;
    sqlite3_stmt* stmt;
    sqlite3_open("DB_test.db", &db);
    int rc = sqlite3_exec(db,"CREATE TABLE IF NOT EXISTS cell(id INTEGER PRIMARY KEY AUTOINCREMENT, xCell REAL, yCell REAL, zCell REAL, volume REAL, rayHits INTEGER);", NULL, NULL,&err);

    if(rc != SQLITE_OK){
        printf("error1: %s", err);
    }
    for(int i = 0; i<10; i++){
        char query[]= "INSERT INTO cell (xCell, yCell, zCell, volume, rayHits) VALUES (1.3,1.003,2.885,0.004,1);";
        printf("%s\n",query);
        rc = sqlite3_exec(db,query,NULL,NULL,&err);
        printf("%sTest",rc);
        if(rc != SQLITE_OK){
            printf("error2: %s", err);
        }
    }

    return 0;
}

But I do not know how to increase the number of rayHits for every identical row. Here's a visual:

What is currently happening:

What I need to happen:

Note how instead of 4 identical rows, it became just 1 row with the column rayHits increasing to 4.

I tried creating a table and creating an input of identical values. I expected to increase the number of rayHits (column) by 1 for every identical row, but I don't know how to increase the number of rayHits by 1.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
eevs
  • 1
  • 3

0 Answers0