看起来很简单,查询15条记录并将它们放入数组中。 代码编译并运行,但数组会丢失输入的值。 我打印输入到数组中的值,它看起来是正确的。虽然在for循环中,第一行是正确的但在此之后不正确。 代码:
printf("SQL returned %d rows\n", mysql_num_rows(res)) ;
i = 0 ;
while ((row = mysql_fetch_row(res)) != NULL)
{
sscanf(row[0], "%lf", &data[i][0]) ;
sscanf(row[1], "%lf", &data[i][1]) ; // bid will always be below ask
sscanf(row[2], "%lf", &data[i][2]) ; // offer = ask in this DB
sscanf(row[3], "%lf", &data[i][3]) ; // mid = average of all bid/ask for each minute
data[i][4] = 0 ; // MA5
data[i][5] = 0 ; // MA15
printf("%.0lf %lf %lf %lf %lf %lf\n", data[i][0], data[i][1], data[i][2], data[i][3], data[i][4], data[i][5]) ;
i++ ;
}
printf("\n") ;
for (j=0; j<15; j++)
printf("%.0lf %lf %lf %lf %lf %lf\n", data[j][0], data[j][1], data[j][2], data[j][3], data[j][4], data[j][5]) ;
前15行与数据库中的行匹配,但只有for循环的第一行才能打印正确的值。
SQL returned 15 rows
1506594851 150.912000 150.932000 150.909265 0.000000 0.000000
1506594792 150.921000 150.941000 150.925761 0.000000 0.000000
1506594731 150.940000 150.963000 150.959714 0.000000 0.000000
1506594674 150.949000 150.971000 150.969609 0.000000 0.000000
1506594611 150.954000 150.976000 150.957382 0.000000 0.000000
1506594551 150.940000 150.964000 150.935012 0.000000 0.000000
1506594492 150.906000 150.927000 150.910357 0.000000 0.000000
1506594431 150.881000 150.906000 150.865748 0.000000 0.000000
1506594371 150.822000 150.846000 150.821062 0.000000 0.000000
1506594851 150.912000 150.932000 150.909265 0.000000 0.000000
1506594251 150.768000 150.794000 150.751332 0.000000 0.000000
1506594194 150.701000 150.724000 150.715379 0.000000 0.000000
1506594134 150.740000 150.757000 150.754935 0.000000 0.000000
1506594076 150.749000 150.772000 150.758302 0.000000 0.000000
1506594011 150.755000 150.774000 150.753550 0.000000 0.000000
1506594851 150.912000 150.932000 150.909265 0.000000 0.000000
1506594251 150.768000 150.794000 150.751332 0.000000 0.000000
1506594194 150.701000 150.724000 150.715379 0.000000 0.000000
1506594134 150.740000 150.757000 150.754935 0.000000 0.000000
1506594076 150.749000 150.772000 150.758302 0.000000 0.000000
1506594011 150.755000 150.774000 150.753550 0.000000 0.000000
1506594492 150.906000 150.927000 150.910357 0.000000 0.000000
1506594431 150.881000 150.906000 150.865748 0.000000 0.000000
1506594371 150.822000 150.846000 150.821062 0.000000 0.000000
1506594312 150.798000 150.824000 150.798255 0.000000 0.000000
0 0.000000 97795656476117789550858635641226156856160388037177544822378925818587375507523129063032745851654482135492440234643161088.000000 270721760232452916408621777331997134952922302290494666767651053623661202019325513025428600003963936853860599692342289934319616.000000 0.000000 0.000000
0 0.000000 0.000000 0.000000 0.000000 0.000000
0 0.000000 0.000000 0.000000 0.000000 0.000000
0 0.000000 0.000000 0.000000 0.000000 0.000000
0 0.000000 0.000000 0.000000 0.000000 0.000000
我无法弄清楚为什么数组正在改变,尽管print语句是相同的,只有一个在中而另一个在中用于循环。 提前致谢
#include <stdio.h>
#include <stdlib.h>
#include "/usr/include/mysql/mysql.h"
#include <string.h> // strncpy
int main(void)
{
int i = 0 ;
int j = 0 ;
int n = 0 ;
int position = 0 ;
double pairsid = 8 ;
char sql[300] ;
double tmpdbl = 0 ;
double data[6][15] ; // Stamp, bid, ask, mid, SMA5, SMA15
char *server = "localhost" ;
char *user = "x" ;
char *password = "x" ; // set me first
char *database = "x";
MYSQL *con1 ;
MYSQL_RES *res ;
MYSQL_ROW row ;
con1 = mysql_init(NULL) ;
// Connect to database
if (!mysql_real_connect(con1, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "DB Connect Error %s\n", mysql_error(con1)) ;
exit(1);
}
sprintf(sql, "(SELECT stamp, bid, ask, mid FROM oanda WHERE pairsid = %.0lf ORDER BY stamp LIMIT %d) ORDER BY stamp DESC", pairsid, 15) ;
printf("%s\n", sql) ;
if (mysql_query(con1, sql))
{
fprintf(stderr, "Error running query %s\n", mysql_error(con1)) ;
exit(1) ;
}
res = mysql_store_result(con1) ;
printf("SQL returned %d rows\n", mysql_num_rows(res)) ;
i = 0 ;
while ((row = mysql_fetch_row(res)) != NULL)
{
sscanf(row[0], "%lf", &data[i][0]) ;
sscanf(row[1], "%lf", &data[i][1]) ; // bid will always be below ask
sscanf(row[2], "%lf", &data[i][2]) ; // offer = ask in this DB
sscanf(row[3], "%lf", &data[i][3]) ; // mid = average of all bid/ask for each minute
data[i][4] = 0 ; // MA5
data[i][5] = 0 ; // MA15
printf("%.0lf %lf %lf %lf %lf %lf\n", data[i][0], data[i][1], data[i][2], data[i][3], data[i][4], data[i][5]) ;
i++ ;
}
printf("\n") ;
for (j=0; j<15; j++)
printf("%.0lf %lf %lf %lf %lf %lf\n", data[j][0], data[j][1], data[j][2], data[j][3], data[j][4], data[j][5]) ;
printf("\n") ;
mysql_free_result(res) ;
mysql_close(con1) ;
}
好的,现在就开始工作了。谢谢大家
while ((row = mysql_fetch_row(res)) != NULL)
{
sscanf(row[0], "%lf", &data[0][i]) ;
sscanf(row[1], "%lf", &data[1][i]) ; // bid will always be below ask
sscanf(row[2], "%lf", &data[2][i]) ; // offer = ask in this DB
sscanf(row[3], "%lf", &data[3][i]) ; // mid = average of all bid/ask for each minute
data[4][i] = 0 ; // MA5
data[5][i] = 0 ; // MA15
printf("Row %d %.0lf %lf %lf %lf %lf %lf\n", i, data[0][i], data[1][i], data[2][i], data[3][i], data[4][i], data[5][i]) ;
i++ ;
}
printf("\n") ;
for (j=0; j<15; j++)
printf("%.0lf %lf %lf %lf %lf %lf\n", data[0][j], data[1][j], data[2][j], data[3][j], data[4][j], data[5][j]) ;
给了我
SQL returned 15 rows
Row 0 1506594851 150.912000 150.932000 150.909265 0.000000 0.000000
Row 1 1506594792 150.921000 150.941000 150.925761 0.000000 0.000000
Row 2 1506594731 150.940000 150.963000 150.959714 0.000000 0.000000
Row 3 1506594674 150.949000 150.971000 150.969609 0.000000 0.000000
Row 4 1506594611 150.954000 150.976000 150.957382 0.000000 0.000000
Row 5 1506594551 150.940000 150.964000 150.935012 0.000000 0.000000
Row 6 1506594492 150.906000 150.927000 150.910357 0.000000 0.000000
Row 7 1506594431 150.881000 150.906000 150.865748 0.000000 0.000000
Row 8 1506594371 150.822000 150.846000 150.821062 0.000000 0.000000
Row 9 1506594312 150.798000 150.824000 150.798255 0.000000 0.000000
Row 10 1506594251 150.768000 150.794000 150.751332 0.000000 0.000000
Row 11 1506594194 150.701000 150.724000 150.715379 0.000000 0.000000
Row 12 1506594134 150.740000 150.757000 150.754935 0.000000 0.000000
Row 13 1506594076 150.749000 150.772000 150.758302 0.000000 0.000000
Row 14 1506594011 150.755000 150.774000 150.753550 0.000000 0.000000
1506594851 150.912000 150.932000 150.909265 0.000000 0.000000
1506594792 150.921000 150.941000 150.925761 0.000000 0.000000
1506594731 150.940000 150.963000 150.959714 0.000000 0.000000
1506594674 150.949000 150.971000 150.969609 0.000000 0.000000
1506594611 150.954000 150.976000 150.957382 0.000000 0.000000
1506594551 150.940000 150.964000 150.935012 0.000000 0.000000
1506594492 150.906000 150.927000 150.910357 0.000000 0.000000
1506594431 150.881000 150.906000 150.865748 0.000000 0.000000
1506594371 150.822000 150.846000 150.821062 0.000000 0.000000
1506594312 150.798000 150.824000 150.798255 0.000000 0.000000
1506594251 150.768000 150.794000 150.751332 0.000000 0.000000
1506594194 150.701000 150.724000 150.715379 0.000000 0.000000
1506594134 150.740000 150.757000 150.754935 0.000000 0.000000
1506594076 150.749000 150.772000 150.758302 0.000000 0.000000
1506594011 150.755000 150.774000 150.753550 0.000000 0.000000