1 /* MDB Tools - A library for reading MS Access database file
2 * Copyright (C) 2000 Brian Bruns
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include "mdbtools.h"
20
21 void read_to_row(MdbTableDef *table, char *sargname);
22
23 int
main(int argc,char ** argv)24 main(int argc, char **argv)
25 {
26 MdbHandle *mdb;
27 MdbTableDef *table;
28 char *colname, *tabname;
29 char *colval;
30 int colnum;
31 char *sargname = NULL;
32 char *updstr = NULL;
33 char data[255];
34 int len;
35
36
37 if (argc<4) {
38 fprintf(stderr,"Usage: %s <file> <table> <sargs> <updstr>\n",argv[0]);
39 exit(1);
40 }
41
42 mdb = mdb_open(argv[1], MDB_WRITABLE);
43 tabname = argv[2];
44 sargname = argv[3];
45 updstr = g_strdup(argv[4]);
46
47 table = mdb_read_table_by_name(mdb, tabname, MDB_TABLE);
48
49 if (table) {
50 mdb_read_columns(table);
51 mdb_read_indices(table);
52 printf("updstr %s\n",updstr);
53 colname = strtok(updstr,"=");
54 colval = strtok(NULL,"=");
55 colnum = mdb_bind_column_by_name(table, colname, data, &len);
56 printf("column %d\n", colnum);
57 read_to_row(table, sargname);
58 printf("current value of %s is %s, changing to %s\n", colname, data, colval);
59 len = strlen(colval);
60 strcpy(data,colval);
61 mdb_update_row(table);
62 mdb_free_tabledef(table);
63 }
64
65 mdb_close(mdb);
66 return 0;
67 }
68
read_to_row(MdbTableDef * table,char * sargname)69 void read_to_row(MdbTableDef *table, char *sargname)
70 {
71 static MdbSargNode sarg;
72 char *sargcol, *sargop, *sargval;
73 unsigned int i;
74 MdbColumn *col;
75
76
77 if (sargname) {
78 sargcol = strtok(sargname," ");
79 for (i=0;i<table->num_cols;i++) {
80 col=g_ptr_array_index(table->columns,i);
81 if (!g_ascii_strcasecmp(col->name, (char *)sargcol)) {
82 sarg.col = col;
83 break;
84 }
85 }
86
87 sargop = strtok(NULL," ");
88 sargval = strtok(NULL," ");
89 printf("col %s op %s val %s\n",sargcol,sargop,sargval);
90 sarg.op = MDB_EQUAL; /* only support = for now, sorry */
91 sarg.value.i = atoi(sargval);
92 table->sarg_tree = &sarg;
93
94 // mdb_add_sarg_by_name(table, sargcol, &sarg);
95 }
96
97 mdb_rewind_table(table);
98 while (mdb_fetch_row(table)) {
99 printf("row found at page %d row %d\n",
100 table->cur_phys_pg, table->cur_row);
101 return;
102 }
103 }
104
105