» »

[C] MySQL

[C] MySQL

SodVina ::

A kdo ve, kako se priklopiti na mysql bazo v c-ju (linux). poskusil sem ze mysql++ in mysql C api, pa dobim error: undefined refference za vse funkcije, ki jih klicem iz mysql.h

potreboval bi enostaven priklop na bazo s standardnimi c funkcijami, saj potrebujem le priklop in izvrsitev enega query-ja

ce kdo ve odgovor, mu bom zanj hvalezen,
cav
  • spremenilo: snow ()

Vesoljc ::

headerji ponavadi rabijo ali source (v *.cpp) ali pa že skompajlano kljižnico *.lib ali *.dll. si jih povezal zraven?
Abnormal behavior of abnormal brain makes me normal...

SodVina ::

dobil sem rpm, s headerji in knjiznjicami.
pa vseeno...
knjiznice pa majo koncnico a

vse naj bi bilo na pravih imenikih, poskusil sem tudi dodati knjiznice na isti imenik, kot so headerji, pa tudi ni pomagalo

BigWhale ::

Banzai!

Tako fscking tezko je malo ampak res samo MALO prebrati dokumentacijo...

Link!


PS:
slabe volje sem...

edit moderator: Sedaj sem tudi jaz.

Zgodovina sprememb…

  • spremenilo: CCfly ()

SodVina ::

Res je!!
pozabil (sem (ne vem kako) parameter -lm.
zdaj zadeva dela!!
hvala, ker si me spomnil, kaksen bedak sem!!
upam da ti bom lahko kdaj vrnil
lp

BigWhale ::

-lm nima nobene veze s MySQLom...

fx ::

A je možno s C++ v Win pisati podatke v MySQL. Bazo imam narejeno sedaj bi samo rad samo vnašal podatke v mysql bazo. Vem da bi lahko uporabil php samo php koliko jaz vem ne omogoča brati iz com porta ali pa iz omrežne kartice, če se motim glede php mi povejte.

Lp,

CWIZO ::

Da se.
Oboje.
hancic.info
I can't uninstall it, there seems to be some kind of "Uninstall Shield"...

fx ::

Odločil sem se da bom uporabil naslednjo možnost:
Com port (v prihodnisti bo LAN povezava) -> MySQL in obratno samo vse skupaj bo vodil program v C++ kot vez med prvim in drugim. A to se tudi da? Prvi del programa imam da mi program prebere iz comporta a za vpis v MySql bazo pa se mi niti ne sanja kako bi to rešo in kako bi se k temu spravo (uporabo google pa ne vem kaj je prav). Delam pa za windowse.

Hvala za vsako pomoč.

Lp,

sverde21 ::

Kaj imaš pa na COM port priključen? Računalnik. Če ga imaš, lahko v win (z Linux nism še probval) narediš "mrežo", in se potem povezuješ na port 3306 (default MySQL port) ali pa dostopaš na server z PHP, ki je povezan preko localhosta na MySQL.

Stvar sm preizkusil tudi sam sicer preko LPT kabla (na obeh compih Win XP) ampak presenetljivo deluje :P
<?php echo `w`; ?>

fx ::

Ne, naredil bom svojo elektroniko. Samo povezav je povezava in je vseeno kaj ti gor priključiš (vsaj jaz tako mislim) in če lahko uporabim za to php je toliko boljše. Bom prvo probla tvoje in če bo to kakor jaz želim potem OK. Hvala za nasvet.

Lp,

sid_dabster ::

Za COM port (cisto kratek povzetek - sinhrono branje in pisanje):

HANDLE com_port = CreateFile(  "COM1",  GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
DCB params;
params.BaudRate = ...;
params.ByteSize = ...;
params.Parity   = ...;
params.StopBits = ...;
params.fParity = ...;
SetCommState(com_port, &params);

#define BUF_SIZE ...pac neki

//...

//Pol pa za branje:
char buf[BUF_SIZE];
DWORD bytes_read;
ReadFile(com_port, buf, BUF_SIZE, &bytes_read, NULL);

//...

//...Pa za pisanje:
char buf[BUF_SIZE];
DWORD bytes_written;
WriteFile(com_port, buf, BUF_SIZE, &bytes_written, NULL);

//...

//...pa na koncu se zapri port
CloseHandle(com_port);


Za mysql c-api knjiznico si pa poglej link, ki ga je poslal bigwhale. Vse ostalo je samo se kuckanje. Mislim, da ti ne bo problem zloziti skupaj taksnega programcka.:D Pa kr v c-ju nared, sploh ce bos s com portom delal.
Fallen beyond all grace deeper and deeper
The sound of her own blood dripping
Like sacred tears from a bleeding rose...( Embraced, Within)

BigWhale ::

Uporabi Qt za Mysql, bo precej lazje.

fx ::

MySQLpp.cpp:1:26: error: mysql++/util.h: No such file or directory
In file included from /usr/local/include/mysql++/connection.h:40,
                 from /usr/local/include/mysql++/mysql++.h:52,
                 from MySQLpp.cpp:2:
/usr/local/include/mysql++/defs.h:34:19: error: mysql.h: No such file or directory
/usr/local/include/mysql++/defs.h:66: error: &#8216;MYSQL_FIELD&#8217; does not name a type
/usr/local/include/mysql++/connection.h:146: error: &#8216;my_bool&#8217; has not been declared
/usr/local/include/mysql++/connection.h:163: error: &#8216;my_bool&#8217; has not been declared
/usr/local/include/mysql++/connection.h:360: error: &#8216;st_mysql_options&#8217; does not name a type
/usr/local/include/mysql++/connection.h:427: error: &#8216;my_ulonglong&#8217; does not name a type
/usr/local/include/mysql++/connection.h:438: error: &#8216;my_ulonglong&#8217; does not name a type
/usr/local/include/mysql++/connection.h:488: error: &#8216;mysql_option&#8217; has not been declared
/usr/local/include/mysql++/connection.h:545: error: &#8216;MYSQL&#8217; does not name a type
/usr/local/include/mysql++/connection.h: In member function &#8216;void mysqlpp::Connection::close()&#8217;:
/usr/local/include/mysql++/connection.h:171: error: &#8216;mysql_&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h:171: error: &#8216;mysql_close&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h: In member function &#8216;const char* mysqlpp::Connection::error()&#8217;:
/usr/local/include/mysql++/connection.h:228: error: &#8216;mysql_&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h:228: error: &#8216;mysql_error&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h: In member function &#8216;int mysqlpp::Connection::errnum()&#8217;:
/usr/local/include/mysql++/connection.h:235: error: &#8216;mysql_&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h:235: error: &#8216;mysql_errno&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h: In member function &#8216;int mysqlpp::Connection::refresh(unsigned int)&#8217;:
/usr/local/include/mysql++/connection.h:247: error: &#8216;mysql_&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h:247: error: &#8216;mysql_refresh&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h: In member function &#8216;int mysqlpp::Connection::kill(long unsigned int)&#8217;:
/usr/local/include/mysql++/connection.h:270: error: &#8216;mysql_&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h:270: error: &#8216;mysql_kill&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h: In member function &#8216;std::string mysqlpp::Connection::client_info()&#8217;:
/usr/local/include/mysql++/connection.h:278: error: &#8216;mysql_get_client_info&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h: In member function &#8216;std::string mysqlpp::Connection::host_info()&#8217;:
/usr/local/include/mysql++/connection.h:289: error: &#8216;mysql_&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h:289: error: &#8216;mysql_get_host_info&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h: In member function &#8216;int mysqlpp::Connection::proto_info()&#8217;:
/usr/local/include/mysql++/connection.h:298: error: &#8216;mysql_&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h:298: error: &#8216;mysql_get_proto_info&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h: In member function &#8216;std::string mysqlpp::Connection::server_info()&#8217;:
/usr/local/include/mysql++/connection.h:306: error: &#8216;mysql_&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h:306: error: &#8216;mysql_get_server_info&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h: In member function &#8216;std::string mysqlpp::Connection::stat()&#8217;:
/usr/local/include/mysql++/connection.h:317: error: &#8216;mysql_&#8217; was not declared in this scope
/usr/local/include/mysql++/connection.h:317: error: &#8216;mysql_stat&#8217; was not declared in this scope
/usr/local/include/mysql++/fields.h: At global scope:
/usr/local/include/mysql++/fields.h:41: error: &#8216;Field&#8217; was not declared in this scope
/usr/local/include/mysql++/fields.h:41: error: template argument 2 is invalid
/usr/local/include/mysql++/fields.h:41: error: template argument 3 is invalid
/usr/local/include/mysql++/fields.h:54: error: expected &#8216;;&#8217; before &#8216;&&#8217; token
/usr/local/include/mysql++/fields.h:57: error: expected &#8216;;&#8217; before &#8216;&&#8217; token
/usr/local/include/mysql++/fields.h:62: error: expected `;' before &#8216;size_type&#8217;
/usr/local/include/mysql++/fields.h:62: error: &#8216;size_type&#8217; does not name a type
/usr/local/include/mysql++/type_info.h:142: error: expected `)' before &#8216;t&#8217;
/usr/local/include/mysql++/type_info.h:148: error: expected &#8216;,&#8217; or &#8216;...&#8217; before &#8216;&&#8217; token
/usr/local/include/mysql++/type_info.h:299: error: &#8216;enum_field_types&#8217; has not been declared
/usr/local/include/mysql++/type_info.h:340: error: expected `)' before &#8216;t&#8217;
/usr/local/include/mysql++/type_info.h:346: error: expected &#8216;,&#8217; or &#8216;...&#8217; before &#8216;&&#8217; token
/usr/local/include/mysql++/type_info.h: In constructor &#8216;mysqlpp::mysql_type_info::mysql_type_info(int)&#8217;:
/usr/local/include/mysql++/type_info.h:348: error: &#8216;f&#8217; was not declared in this scope
/usr/local/include/mysql++/type_info.h:348: error: &#8216;UNSIGNED_FLAG&#8217; was not declared in this scope
/usr/local/include/mysql++/type_info.h:349: error: &#8216;NOT_NULL_FLAG&#8217; was not declared in this scope
/usr/local/include/mysql++/row.h: At global scope:
/usr/local/include/mysql++/row.h:66: error: expected &#8216;,&#8217; or &#8216;...&#8217; before &#8216;&&#8217; token
/usr/local/include/mysql++/result.h:77: error: expected `)' before &#8216;*&#8217; token
/usr/local/include/mysql++/result.h:95: error: expected &#8216;;&#8217; before &#8216;*&#8217; token
/usr/local/include/mysql++/result.h:104: error: expected `;' before &#8216;Row&#8217;
/usr/local/include/mysql++/result.h:134: error: expected &#8216;;&#8217; before &#8216;&&#8217; token
/usr/local/include/mysql++/result.h:140: error: expected `;' before &#8216;void&#8217;
/usr/local/include/mysql++/result.h:296: error: expected &#8216;;&#8217; before &#8216;&&#8217; token
/usr/local/include/mysql++/result.h:306: error: expected `;' before &#8216;bool&#8217;
/usr/local/include/mysql++/result.h:320: error: expected &#8216;;&#8217; before &#8216;*&#8217; token
/usr/local/include/mysql++/result.h: In constructor &#8216;mysqlpp::ResUse::ResUse()&#8217;:
/usr/local/include/mysql++/result.h:68: error: class &#8216;mysqlpp::ResUse&#8217; does not have any field named &#8216;result_&#8217;
/usr/local/include/mysql++/result.h: In copy constructor &#8216;mysqlpp::ResUse::ResUse(const mysqlpp::ResUse&)&#8217;:
/usr/local/include/mysql++/result.h:85: error: &#8216;const class mysqlpp::ResUse&#8217; has no member named &#8216;result_&#8217;
/usr/local/include/mysql++/result.h: In member function &#8216;mysqlpp::Row mysqlpp::ResUse::fetch_row()&#8217;:
/usr/local/include/mysql++/result.h:106: error: &#8216;result_&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:114: error: &#8216;MYSQL_ROW&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:114: error: expected `;' before &#8216;row&#8217;
/usr/local/include/mysql++/result.h:115: error: &#8216;result_&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:115: error: &#8216;mysql_fetch_lengths&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:116: error: &#8216;row&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:124: error: &#8216;row&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h: In member function &#8216;long unsigned int* mysqlpp::ResUse::fetch_lengths() const&#8217;:
/usr/local/include/mysql++/result.h:130: error: &#8216;result_&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:130: error: &#8216;mysql_fetch_lengths&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h: In member function &#8216;void mysqlpp::ResUse::field_seek(int)&#8217;:
/usr/local/include/mysql++/result.h:142: error: &#8216;result_&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:142: error: &#8216;mysql_field_seek&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h: In member function &#8216;int mysqlpp::ResUse::num_fields() const&#8217;:
/usr/local/include/mysql++/result.h:148: error: &#8216;result_&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:148: error: &#8216;mysql_num_fields&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h: In member function &#8216;void mysqlpp::ResUse::purge()&#8217;:
/usr/local/include/mysql++/result.h:164: error: &#8216;result_&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:165: error: &#8216;mysql_free_result&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h: In member function &#8216;mysqlpp::ResUse::operator bool() const&#8217;:
/usr/local/include/mysql++/result.h:193: error: &#8216;result_&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h: In member function &#8216;bool mysqlpp::ResUse::operator==(const mysqlpp::ResUse&) const&#8217;:
/usr/local/include/mysql++/result.h:308: error: &#8216;result_&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:308: error: &#8216;const class mysqlpp::ResUse&#8217; has no member named &#8216;result_&#8217;
/usr/local/include/mysql++/result.h: In member function &#8216;bool mysqlpp::ResUse::operator!=(const mysqlpp::ResUse&) const&#8217;:
/usr/local/include/mysql++/result.h:315: error: &#8216;result_&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:315: error: &#8216;const class mysqlpp::ResUse&#8217; has no member named &#8216;result_&#8217;
/usr/local/include/mysql++/result.h: At global scope:
/usr/local/include/mysql++/result.h:356: error: expected `)' before &#8216;*&#8217; token
/usr/local/include/mysql++/result.h:401: error: &#8216;my_ulonglong&#8217; does not name a type
/usr/local/include/mysql++/result.h: In member function &#8216;const mysqlpp::Row mysqlpp::Result::fetch_row() const&#8217;:
/usr/local/include/mysql++/result.h:379: error: &#8216;result_&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:387: error: &#8216;MYSQL_ROW&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:387: error: expected `;' before &#8216;row&#8217;
/usr/local/include/mysql++/result.h:388: error: &#8216;result_&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:388: error: &#8216;mysql_fetch_lengths&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:389: error: &#8216;row&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:397: error: &#8216;row&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h: In member function &#8216;void mysqlpp::Result::data_seek(mysqlpp::uint) const&#8217;:
/usr/local/include/mysql++/result.h:412: error: &#8216;result_&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h:412: error: &#8216;mysql_data_seek&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h: In member function &#8216;virtual unsigned int mysqlpp::Result::size() const&#8217;:
/usr/local/include/mysql++/result.h:418: error: &#8216;num_rows&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h: In member function &#8216;unsigned int mysqlpp::Result::rows() const&#8217;:
/usr/local/include/mysql++/result.h:424: error: &#8216;num_rows&#8217; was not declared in this scope
/usr/local/include/mysql++/result.h: At global scope:
/usr/local/include/mysql++/result.h:458: error: &#8216;my_ulonglong&#8217; does not name a type
/usr/local/include/mysql++/result.h:459: error: &#8216;my_ulonglong&#8217; does not name a type
/usr/local/include/mysql++/result.h: In member function &#8216;mysqlpp::ResUse& mysqlpp::ResUse::operator=(const mysqlpp::ResUse&)&#8217;:
/usr/local/include/mysql++/result.h:628: error: &#8216;const class mysqlpp::ResUse&#8217; has no member named &#8216;result_&#8217;
/usr/local/include/mysql++/query.h: At global scope:
/usr/local/include/mysql++/query.h:639: error: &#8216;my_ulonglong&#8217; does not name a type
/usr/local/include/mysql++/query.h:640: error: &#8216;my_ulonglong&#8217; does not name a type
/usr/local/include/mysql++/query.h: In member function &#8216;void mysqlpp::Query::storein_sequence(Sequence&, const char*)&#8217;:
/usr/local/include/mysql++/query.h:671: error: &#8216;MYSQL_ROW&#8217; was not declared in this scope
/usr/local/include/mysql++/query.h:671: error: expected `;' before &#8216;d&#8217;
/usr/local/include/mysql++/query.h:672: error: &#8216;d&#8217; was not declared in this scope
/usr/local/include/mysql++/query.h:674: error: &#8216;d&#8217; was not declared in this scope
/usr/local/include/mysql++/query.h:674: error: &#8216;class mysqlpp::ResUse&#8217; has no member named &#8216;raw_result&#8217;
/usr/local/include/mysql++/query.h:674: error: there are no arguments to &#8216;mysql_fetch_lengths&#8217; that depend on a template parameter, so a declaration of &#8216;mysql_fetch_lengths&#8217; must be available
/usr/local/include/mysql++/query.h:674: error: (if you use &#8216;-fpermissive&#8217;, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
/usr/local/include/mysql++/query.h: In member function &#8216;void mysqlpp::Query::storein_set(Set&, const char*)&#8217;:
/usr/local/include/mysql++/query.h:696: error: &#8216;MYSQL_ROW&#8217; was not declared in this scope
/usr/local/include/mysql++/query.h:696: error: expected `;' before &#8216;d&#8217;
/usr/local/include/mysql++/query.h:697: error: &#8216;d&#8217; was not declared in this scope
/usr/local/include/mysql++/query.h:699: error: &#8216;d&#8217; was not declared in this scope
/usr/local/include/mysql++/query.h:699: error: &#8216;class mysqlpp::ResUse&#8217; has no member named &#8216;raw_result&#8217;
/usr/local/include/mysql++/query.h:699: error: there are no arguments to &#8216;mysql_fetch_lengths&#8217; that depend on a template parameter, so a declaration of &#8216;mysql_fetch_lengths&#8217; must be available


Zakaj mi to prevajalnik vrne vse to, če sem mysql++ namestil kot sem prebral tu. V konzolo pa napišem g++ MySQLpp.cpp -o MySQLpp.o v MySQLpp.cpp pa imam samo napisano int main(){return 0;};

Lp,

fx ::

Našel sem to :

CFG=/usr/local/mysql/bin/mysql_config
sh -c "g++ -o progam_name `$CFG --cflags` progam_name.cpp `$CFG --libs`"

zadeva deluje sem jo preizkusil s to kodo:
#include <iostream>
#include <mysql.h>
int main()
{
    MYSQL mysql;
    mysql_init(&mysql);
    if (!mysql_real_connect(&mysql,"localhost","root","new_password","tuncek",0,NULL,0))
    {
        std::cout << "Napaka ! : "  << mysql_error(&mysql);
    }
    char *query = "select * from table";
    mysql_real_query(&mysql,query,strlen(query));
    MYSQL_RES *res = mysql_store_result(&mysql);
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(res)))
    {
            std::cout << row[0] << " " << row[1] << std::endl;
    }
    mysql_close(&mysql);
    return 0;
}
Samo ko poženem mi program javi naslednje: Segmentation fault

Kaj to pomeni, ker res ne vem.

Lp,

BigWhale ::

Poglej moj post pred tvojim. ;)

Gundolf ::

Ne veš kaj je segmentation fault? To pomeni da ti program dostopa do nekega kosa pomnilnika, katerega si ne lasti. Vzroki so torej recimo dostopanje do podakov na katere kaže NULL pointer, prevellik ali premajhen index pri dostopanju do tabel, dostopanje do deletanih pointerjev itd.

fx ::

Kako pa se z Qt dela, ker dosedaj sem vendo uporabljaj konzolo tako za C/C++ in tudi za PHP.

Lp,

BigWhale ::

Qt je skupek knjiznjic. :) www.trolltech.com

fx ::

Program katerega pišem bo pozneje deloval s mysql v textovnem načinu, ker podatke ki jih bom vpisoval v podatkovno bazo bodo prihajali iz serijskih vrat. Potrebujem pa samo to da se podatki vpišejo v bazo in da se za vsaki dan ustvari nova podatkovna baza. To pa je vse kar potrebujem.

V linuxu in win so ti baze datoteke in me sedaj zanima če jaz lahko bi ti pisal direkto v datoteko ali tega mysql ne dovoli, ali pa je prezapleten sistem zapisavanja direktov v datoteko. Samo spršuje če kdo ve.

Lp,

BigWhale ::

Potem je Qt, kot nalasc zate! ;)

Resnicno, Qt je skupek knjiznjic in omogoca tudi delo z mysql bazo podatkov. V bistvu omogoca delo s precej bazami in ima svoj abstraction layer za dostop do baz, tako, da lahko (ce malo pazis pri pisanju programov) bazo zamenjas in notime.

Pa tudi console only aplikacijo lahko napises brez nekih vecjih tezav. :)

IMO, precej hitreje naredis en tak program s Qtjem, kot, ce bi delal direktno z mysqlom.

fx ::

Pa mi je uspelo :D

Koda ime_programa.cpp
#include <iostream>
#include <cstdio>
#include <sys/time.h>
#include <mysql.h>

int main()
{
  MYSQL *conn;
  MYSQL_RES *res;
  MYSQL_ROW row;

  char *server = "localhost";
  char *user = "root";
  char *password = "novo_geslo";
  char *db = "Tuncek";

  char *sqlquery;
  conn = mysql_init(NULL);

  if (!mysql_real_connect(conn, server, user, password, db , 0, NULL, 0)) {
    std::cout << mysql_error(conn) << std::endl;
    return 0;
  }
  else{
    std::cout << "Povezan si na podatkovno bazo " << db << std::endl;
    sqlquery = "SELECT * FROM leto2006";
    if(mysql_query(conn,sqlquery)){
      std::cout << mysql_error(conn);
      return 0;
    }
    res = mysql_use_result(conn);
    while ((row = mysql_fetch_row(res)) != NULL){
      std::cout << row[0] << " " << row[1] << " " << row[3] << " " << row[4] << " " << row[5] << std::endl;
    }
  }
  mysql_free_result(res);
  mysql_close(conn);
  std::cout << "Povezava prekinjena s podatkovno bazo " << db << std::endl;
  return 0;
}


Sedaj kaj morate narediti je to da v konzolo vtipake tole:
g++ -c -I /usr/local/mysql/include/mysql/ ime_programa.cpp
g++ -o izhodni_program ime_programa.o -L /usr/local/mysql/lib/mysql -lmysqlclient
./izhodni_program

/usr/local/mysql/include/mysql/ spremenite po potrebi, odvisno kje se nahaja mysql.h.

in povem da dela. Podobno je za win, kar si tukaj ogledate jaz sem vso zadevo samo priredil za linux.

Pa vseeno ni težko.

Lp,

fx ::

Sedaj pa še pisanje v bazo :
#include <iostream>
#include <cstdio>
#include <sys/time.h>
#include <mysql.h>
#include <ctime>
#include <cstring>
#include <sstream>

std::string itos(int i){ //Integer preTvOri v String
  std::stringstream s;
  s << i;
  return s.str();
}

int main(){
  time_t rawtime;
  tm * cas;
  time ( &rawtime );
  cas = gmtime ( &rawtime );
  MYSQL *conn;
  MYSQL_RES *res;
  MYSQL_ROW row;
  char *server = "localhost";
  char *user = "root";
  char *password = "novo_geslo";
  char *db = "Tuncek";
  const char *sqlquery;
  conn = mysql_init(NULL);

  if (!mysql_real_connect(conn, server, user, password, db , 0, NULL, 0)) {
    std::cout << mysql_error(conn) << std::endl;
    return 0;
  }
  else{
    ura = itos(cas -> tm_hour + 2) + ":" + itos(cas -> tm_min) + ":" + itos(cas -> tm_sec);
    datum = itos(cas -> tm_mday) + "." + itos(cas -> tm_mon + 1) + "." + itos(cas -> tm_year + 1900);
    std::cin >> data1 >> data2 >> data3 >> data4;
    data = "INSERT into leto2006 (datum, ura, data1, data2, data3, data4) VALUES ('"+datum+"','"+ura+"','"+data1+"','"+data2+"','"+data3+"','"+data4+"')";
    sqlquery = data.c_str();
    if(mysql_query(conn,sqlquery)){
      std::cout << mysql_error(conn);
      return 0;
    }
  }
  mysql_free_result(res);
  mysql_close(conn);

  return 0;
}

Koda deluje uspešno na linux za win pa mislim da je potrebno dodati winshoc.h ali nekaj podobnega za oba primera.

Lp,

fx ::

Ne veš kaj je segmentation fault? To pomeni da ti program dostopa do nekega kosa pomnilnika, katerega si ne lasti. Vzroki so torej recimo dostopanje do podakov na katere kaže NULL pointer, prevellik ali premajhen index pri dostopanju do tabel, dostopanje do deletanih pointerjev itd.


Kako naj jaz sedaj ugotovim kje mi program dostopa do kosa pomnilinka, katerega si ne lasti, ker sedaj sem združil oba programa tako za branje iz serijskih vrat in pisanje v mysql bazo in se mi zopet to pojavi. En niz preberega ga uspešno shrani v mysql bazo nato pa ven vrže segmentation fault in se program zaključi.
Če poženem vsaki program posamezno pa zadeva deluje lepo, brez kakršnih napak.

Lp,

Gundolf ::

Očitno si jih slabo združil. Sem ti prejšnjič napisal kje se lokacije za segmentation fault (če odštejemo možnost, da se napaka zgodi v knjižnici, ki jo uporabljaš). Za začetek lahko program debugiraš tako, da greš step by step, in ugotoviš kje ti program štekne.

Če pa ne znaš nobenega debuggerja uporabljat imaš pa dve možnosti
- da se enega naučiš,
- da debugiraš ročno: na ključnih mestih v programu narediš en izpis (cout << "ccc" << endl;), pri katerem bodi pozoren da ga flushas (to ti stori endl). Napaka bo med zadnjim izpisanim in prvim neizpisanim zapisom.

fx ::

Logika programa je bila na psu zato mu ni šlo vse skupaj, sedaj pa dela kot mora.

Lp,

fx ::

Zadeva je lepo delovala pod Linux SuSE 10.1 v grafičnem načinu.

Danes pa sem namnestil Pingo Linux 2.0 naložil strežnik namestil apacha, mysql, php vse brez kakršnih večjih problemov potem sem prevedel moj program tudi uspešno in v koncolo napišem ./program

in dobim pozdrav

./program:error while loadnig shared liberaris : libmysqlclient.so.15: cannot open shared object file: No such file or directoriy

Program sem prevedel na ta način:

g++ -c -I /usr/local/mysql/include/mysql/ ime_programa.cpp
g++ -o izhodni_program ime_programa.o -L /usr/local/mysql/lib/mysql -lmysqlclient

Kaj je lahko narobe.

Lp,

Gundolf ::

Problem je da ti tega fajla ne najde: libmysqlclient.so.15
Ta fajl je dinamična knjižnjica (ekvivalent dll) od mysqla. ki jo mora program v runtimeu naložit. Zakaj je pa ti nimaš / jo program ne najde se mi pa sanja ne.

Tutankhamun ::

Ok še js eno uprašanje.

Sm tut js naložu mySQL.

Ampak ko dam #include "mysql.h" mi Visual studio 2005 ko prtisnem compile vrže vn to:
e:\program files\microsoft visual studio 8\vc\include\mysql\mysql_com.h(184) : error C2146: syntax error : missing ';' before identifier 'fd'
e:\program files\microsoft visual studio 8\vc\include\mysql\mysql_com.h(184) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
e:\program files\microsoft visual studio 8\vc\include\mysql\mysql_com.h(184) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
e:\program files\microsoft visual studio 8\vc\include\mysql\mysql_com.h(354) : error C2065: 'SOCKET' : undeclared identifier
e:\program files\microsoft visual studio 8\vc\include\mysql\mysql_com.h(354) : error C2146: syntax error : missing ')' before identifier 's'
e:\program files\microsoft visual studio 8\vc\include\mysql\mysql_com.h(355) : error C2059: syntax error : ')'

Nikol nevem kaj morem še narest :(.

Hvala za odgovore
AMD Phenom QUAD 9950 Black Edition, 8GB

Zgodovina sprememb…

Tutankhamun ::

Ok sm najdu, ni treba pomagat, sm na netu najdu enga k ma čist ista uprašanja tko da use špila.
Useen hvala
AMD Phenom QUAD 9950 Black Edition, 8GB


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Pomoč C++

Oddelek: Pomoč in nasveti
71366 (1085) samo111
»

[Code:Blocks, c++] Program ne najde knjižnice

Oddelek: Programiranje
111373 (1152) gtu
»

[C++ & Linux & G++ prevljalnik]Kako dodati knjižnico?

Oddelek: Programiranje
91329 (1329) balocom
»

problem z phpjem

Oddelek: Izdelava spletišč
304268 (3681) snow
»

Gentoo Linux & nov KDE problem!

Oddelek: Pomoč in nasveti
101249 (1189) Tarzan

Več podobnih tem