Nothing is extracted from the SQlite table? - sql

void max_min(sqlite3 *db)
{
//call back*********
int i, ncols;
sqlite3_stmt *stmt;
char *sql;
const char *tail;
char *zErrMsg = 0;
int rc;
//******************
//min/max variables
char min[20];
char max[20];
//we want only the min and max value of this table
sql = "SELECT MIN(Start),MAX(End)FROM GMTI;"; //doesn't extract but works in GUI tool?
//sql = "SELECT * FROM GMTI WHERE Start<16;"; //works?
rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &tail);
if(rc != SQLITE_OK){
fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
}
rc = sqlite3_step(stmt);
ncols = sqlite3_column_count(stmt);
printf("Number of columns: %d and row status: %d", ncols, rc);
while(rc == SQLITE_ROW){
for(i=0; i<ncols; i++){
if (strncmp("Start", sqlite3_column_name(stmt,i), strlen("Start")) == 0)
{
strncpy(min, sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i)));
strcpy(min + strlen(sqlite3_column_text(stmt,i)), "\0");
printf("min is: %s\n", min);
printf("<br>");
}
if (strncmp("End", sqlite3_column_name(stmt,i), strlen("End")) == 0)
{
strncpy(max, sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i)));
strcpy(max + strlen(sqlite3_column_text(stmt,i)), "\0");
printf("max: %s\n", max);
printf("<br>");
}
}//end for
fprintf(stderr, "\n");
rc = sqlite3_step(stmt);
}//end while
sqlite3_finalize(stmt);
}
When I use the sql query above with the MIN and MAX functions, nothing prints out for min and max. If I use one of the other statements commented out there, it works as expected, printing the selected query.
What is different about the max and min in the query, that it can't extract the values? Is it not in table format?
Also, I tested the problem query string in an SqLite database browser on the same database and it works, displaying the two - min and max values.
Any suggestions would be greatly appreciated.
Thanks.

Try printing out the returned column names. Maybe they don't equal "Start" and "End" as you assumed?

Maybe you should add a space between MAX(End) and FROM ?

Related

C - fprintf output is not consistent within for loop

I am printing a vary large dataset to a text file using fprintf:
FILE *ff23=fopen("parts_in_cell_dript_part.txt","wa");
if (ff23 == NULL)
error("Error opening text file");
int N=e->s->nr_parts;
fprintf (ff23,"%d\n",N);
for (int k = 0; k < N; k++) {
struct part *restrict p = &parts[k];
float xx=p->x[0];
float yy=p->x[1];
float zz=p->x[2];
int id;
id=p->id;
fprintf(ff23,"%d %f %f %f %d\n",k+1,xx,yy,zz,id);
}
fprintf(ff23,"Finished func\n");
fclose(ff23);
exit(1);
The output suffers from several problems:
lines overwrite other lines. The first column is should go consecutively from 1 to N.
more/less numbers are printed to the file than expected.
I have try to use only append but results stay the same.
EDIT:
I tried adding "t" mode, it did not help

Need an Oracle Pro*C program that queries for all of a user's tables

I'm new to Pro-C and Oracle (but not databases) so I probably need some fundamental help on what I'm doing wrong. I wrote the following Pro*C program to query for all the tables owned by a specific user:
#include <stdio.h>
EXEC SQL INCLUDE SQLCA;
int main() {
int i;
char oraCreds[10] = "user/pass";
char tables[50][50], parts[50][50];
exec sql connect :oraCreds;
if (sqlca.sqlcode != 0) {
printf("ERROR abbreviated\n");
return(0);
}
EXEC SQL SELECT TABLE_NAME
INTO :tables
FROM USER_TABLES;
if (sqlca.sqlcode != 0) {
printf("ERROR: Could not query database for user tables\n");
printf("\tError Code: %d\n", sqlca.sqlcode);
printf("\t%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
} else {
for (i = 0; i < 50; i++) {
printf("\nTable %d: %s\n", i+1, tables[i]);
}
}
// Code below inserted here
return(0);
}
This produces the following error:
ERROR: Could not query database for user tables
Error Code: 1403
ORA-01403: no data found
The problem is that there are records in user_tables and in sqlplus I can do a typical SELECT TABLE_NAME FROM USER_TABLES; in order to verify that.
In an attempt to verify some of the basic functionality of above program, I inserted the following query at the comment.
EXEC SQL SELECT NAME, PART_NO
INTO :tables, :parts
FROM ITEMS;
if (sqlca.sqlcode != 0) {
printf("ERROR: Could not query database for user tables\n");
printf("\tError Code: %d\n", sqlca.sqlcode);
printf("\t%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
} else {
for (i = 0; i < 50; i++) {
printf("\nTable %d: %s\n", i+1, tables[i]);
}
}
This second query gives the expected results and prints out one line for each item's name.
If I understand correctly, the table, user_tables, is a standard part of Oracle, so is there something special about it that would be throwing off my query? I've started reading some alternate ways to query for the user's tables, but I would love to know why the above method does not work.

unable to add columns to a existing table using C

The following program tries to insert a new row and a new column to an already existing database with a table "people" containing four cols (id, lastname, firstname, phonenumber). While the row gets inserted successfully, the column is not getting added.
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
#include <string.h>
int main()
{
PGconn *conn;
PGresult *res;
int rec_count;
int row;
int col;
conn = PQconnectdb("dbname=test host=localhost user=abc1 password=xyz1");
if(PQstatus(conn) == CONNECTION_BAD) {
puts("We were unable to connect to the database");
exit(0);
}
res = PQexec(conn,"INSERT INTO people VALUES (5, 'XXX', 'YYY', '7633839276');");
if(PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "Insertion Failed1: %s", PQerrorMessage(conn));
PQclear(res);
}
else
printf("Successfully inserted value in Table..... \n");
res = PQexec(conn,"update people set phonenumber=\'5055559999\' where id=3");
if(PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "Insertion Failed2: %s", PQerrorMessage(conn));
PQclear(res);
}
res = PQexec(conn, "ALTER TABLE people ADD comment VARCHAR(100) DEFAULT 'TRUE'");
if(PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "Insertion Failed3: %s", PQerrorMessage(conn));
PQclear(res);
}
rec_count = PQntuples(res);
printf("We received %d records.\n", rec_count);
puts("==========================");
for(row=0; row<rec_count; row++) {
for(col=0; col<3; col++) {
printf("%s\t", PQgetvalue(res, row, col));
}
puts("");
}
puts("==========================");
PQclear(res);
PQfinish(conn);
return 0;
}
The program after being compiled and linked gives the following output:
$ ./test
Successfully inserted value in Table.....
We received 0 records.
==========================
==========================
In the postgresql environment, the table "people" is updated with an extra row and a column containing "TRUE".
This is my first program with C embedded with postgresql. Please help!!
This line:
res = PQexec(conn, "EXEC('UPDATE people SET comment = ''TRUE'';');");
should be:
res = PQexec(conn, "UPDATE people SET comment = 'TRUE'");
The EXEC syntax is part of embedded SQL in C with ECPG. It's not to be combined with PQexec from the C libpq library, which is clearly what you're using given the rest of the source code.
Also the result of an UPDATE with no RETURNING clause has PQresultStatus(res)==PGRES_COMMAND_OK, not PGRES_TUPLES_OK
PGRES_COMMAND_OK
Successful completion of a command returning no data.
See Command Execution Functions
You also want to test any PGresult returned by PQexec, not only the result of your last query, because any query may fail.
How can you retrieve data without doing a SELECT statement ?
Never used PQexec, but i guess the code to add might be like this:
res = PQexec(conn, "ALTER TABLE people ADD comment VARCHAR(100) DEFAULT 'TRUE'");
if(PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "Insertion Failed3: %s", PQerrorMessage(conn));
PQclear(res);
}
//Add PQexec and select statement here.
//Something "like" this.
res = PQexec(conn, "SELECT * FROM people;");
if(PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "Select Failed: %s", PQerrorMessage(conn));
PQclear(res);
}
rec_count = PQntuples(res);
printf("We received %d records.\n", rec_count);

Scanf in Objective C Crashes Xcode And the Program

I have removed scanf from the code and the rest of the program runs with no issue. When the code reaches scanf, and after I type a number, xcode 'loses connection' and displays the error "Program ended with exit code: -1". I have also tried making 'input' an int, changed the name of variable input in case there was a conflict there, and tried it without fflush in the code. I am running Mountain Lion on Oracle VM Virtualbox, and my computer is on Windows 7, if that's relevant.
What am I doing wrong?
#import Foundation/Foundation.h
#include stdio.h
int main(int argc, const char * argv[])
{
#autoreleasepool {
float input = 1;
int i = 0;
float total = 0;
int max = 0;
int min = 1000;
while (input != 0){
NSLog(#"Please put in a number. \n");
scanf("%f", &input);
fflush(stdin);
if(input > max){
max = input;
}
if(input < min){
min = input;
}
total = total + input;
i++;
}
printf("The number of entered numbers was %i \n", i);
printf("The sum of the entered numbers is %f\n", total);
total = total/i;
printf("The average of all the numbers is %f\n", total);
printf("The highest number entered is %i\n", max);
printf("The lowest number entered is %i\n", min);
}
return 0;
}
fflush(stdin);
You probably want
fflush(stdout)
I'm not sure what fflush() will do with an FILE* opened for input.
This may or may not be the cause of your issues.
Return and Enter key have a different meaning inside Xcode. You go to the product menu and select build build for running and execute it in the terminal it will work fine.
Inside Xcode the Enter key in the numpad stops the program with a -1 return code. I am not sure whether this is bug or a feature, but I could not find the key in the keybinding for Xcode.
Please check here

SQLite3 - select query fails with Unicode characters?

My sqlite3 SELECT query fails if the query contains any Unicode characters. ie, the below query returns 0 instead of the actual count. I am generating the select query using C++/Visual Studio 2010.
SELECT COUNT() FROM mytable where path = "C:\\sølvgut.txt";
When I execute the above query using SQLite Expert editor it working as expected. But in my code I am using a wrapper class CppSQLite3.cpp to execute the query which is causing the issue. In the below code, it getting inside the code else if (nRet == SQLITE_ROW)
CppSQLite3Query CppSQLite3DB::execQuery(const char* szSQL)
{
checkDB();
sqlite3_stmt* pVM = compile(szSQL);
int nRet = sqlite3_step(pVM);
if (nRet == SQLITE_DONE)
{
// no rows
return CppSQLite3Query(mpDB, pVM, true/*eof*/);
}
else if (nRet == SQLITE_ROW)
{
// at least 1 row
return CppSQLite3Query(mpDB, pVM, false/*eof*/);
}
else
{
nRet = sqlite3_finalize(pVM);
const char* szError= sqlite3_errmsg(mpDB);
throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
}
}