ā10-25-2011 03:10 AM
Hello !
I have amounts of datas to be saved automaticly,so I want to use database.I've registered ODBC data source in my computer.
As the test datas are saved automaticly,I only write a function to save the datas into database.
//write datas into database
void WriteYBdata(char* FileName)
{
char *time1;
char *date1;
char date[27];
int hstmt,id,status;
date1 = DateStr ();
time1 = TimeStr ();
Fmt(date,"%s<%d[w4p0]/%d[w2p0]/%d[w2p0]",date1,time1);
hstmt = DBActivateSQL (hdbc, "SELECT * FROM ybdata");
DBBindColInt (hstmt, 1, &id, &status);
DBBindColChar (hstmt, 2, 27, date, &status, "");
DBBindColFloat (hstmt, 3, &CH4, &status);
DBBindColFloat (hstmt, 4, &CO2, &status);
DBBindColFloat (hstmt, 5, &O2, &status);
DBBindColFloat (hstmt, 6, &CO, &status);
DBBindColFloat (hstmt, 7, &H2S, &status);
DBBindColInt (hstmt, 8, &TEM, &status);
DBBindColInt (hstmt, 9, &HUM, &status);
DBCreateRecord(hstmt);
DBPutRecord(hstmt);
DBDeactivateSQL(hstmt);
}
void WritePSMdata(char* FileName)
{
char *time2;
char *date2;
char date[27];
int hstmt,id,status;
date2 = DateStr ();
time2 = TimeStr ();
Fmt(date,"%s<%d[w4p0]/%d[w2p0]/%d[w2p0]",date2,time2);
hstmt = DBActivateSQL (hdbc, "SELECT * FROM psmdata");
DBBindColInt (hstmt, 1, &id, &status);
DBBindColChar (hstmt, 2, 27, date, &status, "");
DBBindColInt (hstmt, 3, &wy_voltage,&status);
DBBindColFloat (hstmt, 4, &zy_y1, &status);
DBBindColFloat (hstmt, 5, &zl_i1, &status);
DBBindColInt (hstmt, 6, &wx, &status);
DBBindColInt (hstmt, 7, &wg, &status);
DBBindColInt (hstmt, 8, &ws, &status);
DBCreateRecord(hstmt);
DBPutRecord(hstmt);
DBDeactivateSQL(hstmt);
}
I connect the database in the main function.
I've been debug the program and it communicates normally, but it does not save datas into the database.
The attachment is my program.It is my first time to use SQL Toolkit and I'm not familiar with it. I would appreciate it very much if you could give me some help..
Best regards.
xiepei
ā10-25-2011 03:25 AM
Hi,
I can not assist with the database as I don't have the toolkit.
However, looking at your code I was wondering about the line
Fmt(date,"%s<%d[w4p0]/%d[w2p0]/%d[w2p0]",date1,timāe1);
1) I would have expected three source parameters as there are three %d...
2) I would have expected three integer parameters
Instead you provide two character strings... I am surprised that this line does not cause an error?
ā10-25-2011 03:35 AM
Hi !
Thank you very much for your reply.
That line I want to set the time format and it could use the funtion
DBBindColChar (hstmt, 2, 27, date, &status, "");
I do not know it is right or wrong.I could not see any response in this line.
My original idea is recording the date and time when saving the datas.
Would you have any good idea according to it ?
ā10-25-2011 04:00 AM
...Well I can't tell if it's a good idea but you can use a function such as sscanf ( date1,"%d-%d-%d", &month, &day, &year ) to obtain the individual elements of your date information; in a similar way you can extract your time values; you could then use these individual values to format a combined date_time either using Fmt or sprintf.
I am very surprised that your Fmt line did not give any run-time error... Fmt typically complains if there is a mismatch in the number of arguments.
Good luck,
Wolfgang
ā10-25-2011 07:33 PM
Hi !
Thank you very much for your advice.You suggest me use a function sscanf ( date1,"%d-%d-%d", &month, &day, &year ).But I don't know how to get the the individual elements of the date information,such as "&year,&month,&day". Do you have any similar examples ?
Thank you very much !
ā10-26-2011 12:20 AM
xiepei,
the function sscanf ( date1,"%d-%d-%d", &month, &day, &year ) actually does extract the individual elements from your source string date1 - you should consult a C book to understand these ANSI C functions, e.g. the one found here
ā10-26-2011 12:58 AM
Hi !
Thank you very much !
I've debugged my program again.I saw the function "void WriteYBdata(char* FileName);" "void WritePSMdata(char* FileName); " which I write into database are not active.
So I could not see that line occur error. Therefore,I think it's the problem that the function I write into database. The two functions must be mistake.However,I don't know how to modify them. Could you give me some advice about modifying the functions about writting into database ?
Thank you very much !
Best regards.