How to handle a huge string?

This may be a beginner's question, but I want to avoid buffer overflows. I read a lot of registry data that will be loaded into the SQL database. I read the data in a loop and the data was inserted after each loop. My problem is that in this way, if I read 20 keys, and the values ​​are under (the number of keys is different on each computer), then I need to connect to the SQL database 20 times.

However, I found out that there is a way to create a stored procedure and transfer all the data, and so the SQL server will process the data, and I need to connect only once to the SQL server.

Unfortunately, I do not know how to handle such a large string to avoid any unexpected errors such as the owerflow buffer stream. So my question is: how should I declare this line?

Should I just create a type string char string[ 15000 ];and concatenate the values? Or is there an easier way to do this?

Thank!

+5
source share
3 answers

STL strings should work much better than the approach you described.

You will also need to create some threshold values. For example, if your line has grown by more than megabytes, you should consider creating different SQL connections, as your transaction will be too long.

+2
source

C, , malloc . , , , . , .

, . , , , .

, , . " , X , ", , . , / , , , , .

: char buf[15000]; , , , static. , , , . ( , .)

, :

#define BUFFER_SIZE 15000
char buf[BUFFER_SIZE];

, .

0

(, ) , . "write" . , , , . (, ) - reset "" . " , " "" , 0. , .

const int MAX_BUFFER_SIZE = 15000;
char buffer[MAX_BUFFER_SIZE];
char buffer_pos = 0; // "write" position within the buffer.

...

// Retrieve key, value pairs and push them into the buffer.
while(get_next_key_value(key, value)) {
  post(key, value);
}

// Execute stored procedure if buffer is not empty.
if(buffer_pos > 0) {
  exec_stored_procedure(buffer);
}
...

bool post(const char* key, const char* value)
{
  int len = strlen(key) + strlen(value) + <length of separators>;

  // Execute stored procedure if there is no space for new key/value pair.
  if(len + buffer_pos >= MAX_BUFFER_SIZE) {
    exec_stored_procedure(buffer);
    buffer_pos = 0;  // Reset "write" position.
  }

  // Copy key, value pair to the buffer if there is sufficient space.
  if(len + buffer_pos < MAX_BUFFER_SIZE) {  
    <copy key, value to the buffer, starting from "write" position>
    buffer_pos += len; // Adjust "write" position.
    return true;
  }
  else {
    return false;
  }
}

bool exec_stored_procedure(const char* buf)
{
  <connect to SQL database and execute stored procedure.>
}
0

All Articles