Insert Into SQL Database

I have a function that writes something in a SQL database. First time it is passing ok second time it is giving an error and I cant figure out what is causing it.I presume I am not closing something in the proper way. A clue in the right direction would be appreciated.


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
      void WriteSQL (char querystr[4096])
      {
           MYSQL*     conn;
    	   MYSQL_RES* res;
    	   MYSQL_ROW  row;

    	   const char* server   = "localhost";
    	   const char* user     = "root";
    	   const char* password = ""; /* set me first */
    	   const char* database = "PowerMonitor";

           cout <<  querystr << "\n";
           cout << "1 " << server << " " << user << " " << password << " " << database << "\n";
    	   conn = mysql_init(NULL);
    	   /* Connect to database */
    	   if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
    	   {
    	      fprintf(stderr, "%s\n", mysql_error(conn));
    	      exit(1);
    	   }
           cout << "2\n";
    	   /* send SQL query */
//	   cout << querystr << "\n";
  	   if (mysql_query(conn,querystr))
  	   {
  	      fprintf(stderr, "%s\n", mysql_error(conn));
  	      exit(1);
  	   }

    	   res = mysql_use_result(conn);
           cout << res << "\n"; 
    	   /* close connection */
    	   mysql_free_result(res);
    	   mysql_close(conn);
//    	   return 0;

      }


Output of program :

23:02:46
000300000049
INSERT INTO EKM(SerialNumber,Version,Date,Time,TotalFwdKwh,TotalRevKwh,VoltageL1,VoltageL2,VoltageL3,CurrentL1,CurrentL2,CurrentL3,PowerL1,PowerL2,PowerL3,CosPhiL1,CosPhiL2,CosPhiL3,Frequency) VALUES (300000049,4,'2013-02-17','23:02:46',1389.949951,181.869995,232.899994,231.100006,231.699997,1.800000,0.200000,2.800000,424.000000,0.000000,562.000000,0.990000,0.050000,0.990000,50.000000)
1 localhost root  PowerMonitor
2
0
2013-02-17 23:02:46 300000049 4 1571.82 1389.95 181.87 232.9 231.1 231.7 1.8 0.2 2.8 424 0 562 0.99 0.05 0.99 50 
23:02:47
23:02:48
23:02:49
000300000079
INSERT INTO EKM(SerialNumber,Version,Date,Time,TotalFwdKwh,TotalRevKwh,VoltageL1,VoltageL2,VoltageL3,CurrentL1,CurrentL2,CurrentL3,PowerL1,PowerL2,PowerL3,CosPhiL1,CosPhiL2,CosPhiL3,Frequency) VALUES (300000079,4,'2013-02-17','23:02:49',438.050018,193.029999,230.800003,230.899994,231.699997,0.200000,0.000000,2.600000,0.000000,0.000000,532.000000,0.030000,0.000000,0.990000,50.000000)
1 localhost root  PowerMonitor
2
Lost connection to MySQL server during query
Last edited on
I don't know why, but it sounds like a MySQL thing. You could searching MySQL resources.

http://dev.mysql.com/doc/refman/5.0/en/gone-away.html
After a few more days of searching I figured out what was going on. It was the signal handler from the serial IO interfering with the write to the database. I switched to polling the serial port and everything is working fine now.
Topic archived. No new replies allowed.