Hi,

I’m trying to write a small program to add (historical) prices to my
gnucash file.  I’m basing my approach off of some code I found on the
gnucash-users list
(http://article.gmane.org/gmane.comp.gnome.apps.gnucash.user/25065/match=historical+prices+bond).
Essentially, my code opens up the book, tries to find a commodity
(hard coded to “GOOG” for testing) and then creates a new price for
that commodity.  I’ve attached my code to this message, as well as an
example.gnucash file that I’ve been using.

Unfortunately, I’m getting stuck — even though I’ve defined a GOOG
security in my gnucash file, I can’t seem to retrieve it from my file
using “gnc_commodity_table_lookup()”: I always get NULL for my
commodity, even though I can zless the example.gnucash file and can
see my commodity, as well as edit it in gnucash.

To wit, here’s a transcript of a session with my code:

  %make 
  gcc -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include  
-I/usr/include/gnucash -L/usr/lib/gnucash -lgnc-core-utils -lgnc-qof -std=c99 
-lgnc-backend-file-utils historical.c -g -o historical
  %gnucash-env ./historical
  Has NYSE namespace
  commodity is NULL
  %

(The example.gnucash file is in the same directory as the program.)

Am I doing something wrong?  I’m using gnucash 2.2.6 on Ubuntu 8.10.

Thanks,
        —Justin
#define HAVE_SCANF_LLD 1 // or else qof.h complains

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#include <qof.h>
#include <gnc-pricedb.h>
#include <gnc-session.h>

int main(int argc, char **argv) {

    QofSession *session;
    QofBook *book;

    GNCPrice *price;
    GNCPriceDB *price_db;

    gnc_commodity *currency;
    gnc_commodity_table *table;
    gnc_commodity *commodity;    
    gnc_numeric n;

    qof_init();
    gnc_engine_init(argc, argv);
    session = qof_session_new();

    qof_session_begin(session, "file:example.gnucash", TRUE, FALSE);

    book = qof_session_get_book(session);
    price_db = gnc_book_get_pricedb(book);

    table = gnc_commodity_table_get_table(book);
    currency = gnc_commodity_table_lookup(table, 
					  GNC_COMMODITY_NS_CURRENCY,
					  "USD");

    if (gnc_commodity_table_has_namespace(table, "NYSE")) {
	printf("Has NYSE namespace\n");
    }

    commodity = gnc_commodity_table_lookup(table,
					   GNC_COMMODITY_NS_NYSE,
					   "GOOG");
    if (!commodity) {
	printf("commodity is NULL\n");
	return 1;
    }
    
    price = gnc_price_create(book);
    gnc_price_set_commodity(price, commodity);
    gnc_price_set_currency(price, currency);
    gnc_price_set_time(price, gnc_dmy2timespec(1,11,2008));
    gnc_price_set_source(price, "jmp:historical");
    gnc_price_set_typestr(price, "last");


    string_to_gnc_numeric("7337.34", &n);
    gnc_price_set_value(price, n);
    gnc_pricedb_add_price(price_db, price);

    gnc_pricedb_print_contents(price_db, stdout);
    
    qof_session_save(session, NULL);

  return 0;
}

Attachment: example.gnucash
Description: Binary data

_______________________________________________
gnucash-devel mailing list
gnucash-devel@gnucash.org
https://lists.gnucash.org/mailman/listinfo/gnucash-devel

Reply via email to