Error in applying strftime() method

I am facing a problem in mysql query execution.
My code is following.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 conn = mysql_init(NULL);
	mysql_real_connect(conn,"","root","root","trafficdetails",0,NULL,0);
	std::cout << "Total: " << totalcount << " vehicles" << std::endl;

	////std::cout << totalcount/14 << std::endl;

	time_t rawtime;
	struct tm * timeinfo;
	char buffer [80];
	char buffer1 [80];
	char buffer2 [80];

	time (&rawtime);
	timeinfo = localtime (&rawtime);

	////strftime (buffer,80,"%c:%A",timeinfo);
	strftime (buffer,80,"%T",timeinfo);
	strftime (buffer1,80,"%A",timeinfo);

	////strftime (buffer2,80,"%R",timeinfo);
	
	////puts (buffer);
	////printf(buffer);

	std::ostringstream query,query1,query2,query3,query4,query5,query6;
	
	////query0 <<  "Insert into details values  ( NOW(),'" << buffer << "' ,'" << totalcount << "','" << buffer1 << "' )";
	query  <<  "Insert into monday values   ('" << buffer << "','" << totalcount << "')";
	query1 <<  "Insert into tuesday values  ('" << buffer << "','" << totalcount << "')";
	query2 <<  "Insert into wednesday values('" << buffer << "','" << totalcount << "')";
	query3 <<  "Insert into thursday values ('" << buffer << "','" << totalcount << "')";
	query4 <<  "Insert into friday values   ('" << buffer << "','" << totalcount << "')";
	query5 <<  "Insert into saturday values ('" << buffer << "','" << totalcount << "')";
	query6 <<  "Insert into sunday values   ('" << buffer << "','" << totalcount << "')";
	
	/*mysql_query(conn, query1.str().c_str());
	mysql_query(conn, query2.str().c_str());
	mysql_query(conn, query3.str().c_str());
	mysql_query(conn, query4.str().c_str());
	mysql_query(conn, query5.str().c_str());
	mysql_query(conn, query6.str().c_str());*/

	//string compare

	if(buffer1=="Monday")
	{
		mysql_query(conn, query.str().c_str());
	}

	else if(buffer1=="Tuesday")
	{
		mysql_query(conn, query1.str().c_str());
	}

	else if(buffer1=="Wednesday")
	{
		mysql_query(conn, query2.str().c_str());
	}

	else if(buffer1=="Thursday")
	{
		mysql_query(conn, query3.str().c_str());
	}

	else if(buffer1=="Friday")
	{
		mysql_query(conn, query4.str().c_str());
	}

	else if(buffer1=="Saturday")
	{
		mysql_query(conn, query5.str().c_str());
	}

	else
	{
		mysql_query(conn, query6.str().c_str());
	}


Error is Expression:("Invalid format directive",0)

I couldn't understand what is the problem?

Need help.

Thanks in advance..
Possibly your compiler does not support the option "%T". Are you using visual studio?
If that is the case, you might try "%H:%M:%S" instead.
http://msdn.microsoft.com/en-us/library/fe06s4ak%28v=VS.71%29.aspx


Another error is this:
if(buffer1=="Monday")
Character arrays cannot be compared using the "==" operator like this (it will actually compare the address of the buffer with the address of the literal).

Instead, use if (!strcmp(buffer1, "Monday"))
note the "!" to invert the result, as the function returns 0 when the strings are equal.
http://www.cplusplus.com/reference/cstring/strcmp/
Or you can use

if (strcmp(buffer1, "Monday" == 0))

or even

if (0 == strcmp(buffer1, "Monday"))

Which one you use is a matter of taste.

Andy

PS I don't know what compiler you are using, but VC++ warns you about lines like if(buffer1=="Monday") at warning level 4 (/W4).

PS You appear to be more typing than you need. Maybe the if-cases will do more work, but if not then your code appears to be doing the same as the following, where:
- the setup of the SQL connection has been skipped
- it is assuming you trust strftime; should probably check its return value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    // establisg SQL connection

    time_t rawtime;
    struct tm * timeinfo;
    char timeOfDay[80];
    char dayName  [80];

    time (&rawtime);
    timeinfo = localtime (&rawtime);

#ifdef _MSC_VER
    strftime (timeOfDay,80,"%H:%M:%S",timeinfo);
    strftime (dayName,80,"%A",timeinfo);
    _strlwr(dayName); // assumes unaccented strings
#else
    strftime (timeOfDay,80,"%T",timeinfo);
    strftime (dayName,80,"%A",timeinfo);
    make_lower_case(dayName); // assumes unaccented strings
#endif

    std::ostringstream query;
    query  <<  "Insert into " << dayName << " values   ('" << timeOfDay
           << "','" << totalcount << "')";
    mysql_query(conn, query.str().c_str());


where _strlwr comes from the Microsoft CRT and make_lower_case is

1
2
3
4
5
6
7
8
#ifndef _MSC_VER
void make_lower_case(char* str) {
    while(*str) {
        *str = (char)toupper((unsigned char)*str);
        str++;
    }
}
#endif 


Also see:

strupper(), strlower()
http://www.cplusplus.com/faq/sequences/strings/case-conversion/
Last edited on
Topic archived. No new replies allowed.