Witam!
Mam problem z pewnym programem pisanym w C++ kompilownaym w MSVC6. Kod programu pobralem ze strony http://www.synnottsoftware.com/tutorials/mysqlbasic.php. Jest to dosc stary kod wiec musialem wprowadzic pewne zmiany, poniewaz byla niezgodnosc wersji z MySQL'em i pojawialy sie bledy. Program ten laczy sie z baza danych MySQL zapisuje w bazie, w tabeli pewien lancuch i wyswietla wyniki. Kompiluje sie poprawnie. Podczas uruchamiania go (program.exe jakis_lancuch) zwraca blad: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server for the right syntax to use near '' line 1. Program nie chce wykonac zadnego nawet prostego zapytania... Moja wersja MySQL'a to: mysql-4.1.13a-win32. Wszystko zainstalowane na jedym komputerze. Wydaje mi sie, ze kod jest poprawny... Jestem poczatkujacym programista. Byc moze to cos z wersja MySQL'a?
Oto tresc programu:
#include <winsock.h>
#include <mysql.h> /* Headers for MySQL usage */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static MYSQL demo_db;
int main(int argc, char **argv){
my_ulonglong insert_id;
char *encdata, *query;
int datasize;
MYSQL_RES *res; /* To be used to fetch information into */
MYSQL_ROW row;
if(argc<2){
printf("Please supply a string for insertion into the database\n");
exit(0);
}
if(!mysql_real_connect(&demo_db, "localhost", "root", "haslo","demo_db",3306,NULL,0)){
/* Make connection with mysql_connect(MYSQL db, char *host, char *username, char *password */
printf(mysql_error(&demo_db));
exit(1);
}
if(mysql_select_db(&demo_db, "demo_db")){ /* Select the database we want to use */
printf(mysql_error(&demo_db));
exit(1);
}
encdata=(char *) malloc(strlen(argv[1])+1); /* Create a buffer to write our slash-encoded data int; it must be at least twice the size of the input string plus one byte for the null terminator */
datasize=mysql_real_escape_string(&demo_db, encdata, argv[1], strlen(argv[1])); /* Escape any MySQL-unsafe characters */
query=(char *)malloc(datasize+255); /* Make sure we have enough space for the query */
sprintf(query, "INSERT INTO demotable(demodata) VALUES('%s')", encdata); /* Build query */
if(mysql_real_query(&demo_db, query, strlen(query)+255)){ /* Make query */
printf(mysql_error(&demo_db));
exit(1);
}
free(query);
insert_id=mysql_insert_id(&demo_db); /* Find what id that data was given */
query=(char *)malloc(255);
sprintf(query, "SELECT demodata FROM demotable WHERE id='%d'", insert_id);
if(mysql_real_query(&demo_db, query, 255)){ /* Make query */
printf(mysql_error(&demo_db));
exit(1);
}
res=mysql_store_result(&demo_db); /* Download result from server */
row=mysql_fetch_row(res); /* Get a row from the results */
printf("You inserted \"%s\".\n", row[0]);
mysql_free_result(res); /* Release memory used to store results. */
mysql_close(&demo_db);
return 0;
}
A oto jak utworzylem tabele w MySQL'u:
CREATE TABLE demotable (
id int(11) NOT NULL auto_increment,
demodata varchar(255) default NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;
Serdecznie prosze o pomoc!!!