C: Iterate through SQLite records and assigning each value to a variable

Murdock L

I have table USER with columns ID, NAME and ADDRESS. e.g.

Column ID has 1,2 Column NAME has Bob, Jane Column ADDRESS has UK, US

And I wish to iterate through all records and save each column into an array. The result would be something like this:

char *NAME[2]; NAME[0] would have "Bob", NAME[1] would have "Jane" and

char *ADDRESS[2]; ADDRESS[0] would have "UK", ADDRESS[1] would have "US".

Can someone point me in the right direction instead of calling an SQL statement for each array entry?

Thanks in advance!

Magnus Reftel

If you start with the SELECT example in the tutorial you linked to, you can modify it to have a

struct results {
  size_t num_used;
  char* NAME[SOME_MAX_NUMBER_OF_ROWS_HERE];
  char* ADDRESS[SOME_MAX_NUMBER_OF_ROWS_HERE];
};

and then have one of those on your stack (with num_used set to 0) and pass a pointer to that as argument 4 of sqlite3_exec. In callback, you then cast argument 1 to a pointer to struct results, and strdup your fields to results->NAME[results->num_used] etc., and increment results->num_used at the end. When sqlite3_exec returns, you should have your NAME and ADDRESS arrays filled out.

There are of course better ways of handling the arrays, so that you don't have to anticipate the maximum number of rows etc., but that's a different issue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Assigning a value in SQLite cursor to a variable

From Dev

Iterate through 2D List, assign each value on a row in the list to a variable, repeat for each row - python

From Dev

Iterate through each value of list in order, starting at random value

From Dev

ruby - iterate through a hash containing key/array value pairs, and iterate through each value

From Dev

C# Assigning a new value to a variable

From Dev

How do I iterate through a table and create a hash for each value?

From Dev

jQuery ''mapping'': How to iterate through an array, using variable value

From Dev

c++ vector for each loop doesn't iterate through references?

From Dev

Iterate through Queue in c# while Dequeue for each iteration

From Dev

Assigning a DataTable value to a Variable

From Dev

Assigning value for a class variable

From Dev

Assigning a Variable and a Column to a Value

From Dev

condition is not assigning value to the variable

From Dev

Assigning value to global variable

From Dev

Assigning ArrayList value into a Variable

From Dev

Iterate through each li in ul

From Dev

Iterate through each of the group column?

From Dev

Custom method for console write line and assigning value to variable - C#

From Dev

Returning a dictionary value in C# without assigning it to a variable?

From Dev

Iterate through bits in C

From Dev

Oracle Forms: only iterate through selected records

From Dev

Iterate through struct by variable name

From Dev

Iterate through an array using each_with_index without a separate index variable

From Dev

Ruby Rails - Go through Array & Save Each Value As A Unique Variable

From Dev

variable declared within a loop maintains value through each iteration of the loop

From Dev

FORTRAN - Assigning an array value to variable

From Dev

Awk not assigning value of field to variable

From Dev

Assigning a value to a variable randomly, but not working

From Dev

Assigning Array Value to variable PHP

Related Related

  1. 1

    Assigning a value in SQLite cursor to a variable

  2. 2

    Iterate through 2D List, assign each value on a row in the list to a variable, repeat for each row - python

  3. 3

    Iterate through each value of list in order, starting at random value

  4. 4

    ruby - iterate through a hash containing key/array value pairs, and iterate through each value

  5. 5

    C# Assigning a new value to a variable

  6. 6

    How do I iterate through a table and create a hash for each value?

  7. 7

    jQuery ''mapping'': How to iterate through an array, using variable value

  8. 8

    c++ vector for each loop doesn't iterate through references?

  9. 9

    Iterate through Queue in c# while Dequeue for each iteration

  10. 10

    Assigning a DataTable value to a Variable

  11. 11

    Assigning value for a class variable

  12. 12

    Assigning a Variable and a Column to a Value

  13. 13

    condition is not assigning value to the variable

  14. 14

    Assigning value to global variable

  15. 15

    Assigning ArrayList value into a Variable

  16. 16

    Iterate through each li in ul

  17. 17

    Iterate through each of the group column?

  18. 18

    Custom method for console write line and assigning value to variable - C#

  19. 19

    Returning a dictionary value in C# without assigning it to a variable?

  20. 20

    Iterate through bits in C

  21. 21

    Oracle Forms: only iterate through selected records

  22. 22

    Iterate through struct by variable name

  23. 23

    Iterate through an array using each_with_index without a separate index variable

  24. 24

    Ruby Rails - Go through Array & Save Each Value As A Unique Variable

  25. 25

    variable declared within a loop maintains value through each iteration of the loop

  26. 26

    FORTRAN - Assigning an array value to variable

  27. 27

    Awk not assigning value of field to variable

  28. 28

    Assigning a value to a variable randomly, but not working

  29. 29

    Assigning Array Value to variable PHP

HotTag

Archive