Etsitään yksinkertaisin ANSI C MySQL esimerkki, joku haku tietokannasta, ja tiedon tulostaminen ruudulle printf:llä
ANSI C + MySQL
noob
1158
0Äänestä
Muut lukivat myös
Odottaa vastausta
Näihin keskusteluihin ei ole vielä vastattu. Ole ensimmäinen.
Turvaa tilisi vahvistamalla puhelinnumerosi. Voit silloin palauttaa unohtuneen salasanan tekstiviestillä.
Näihin keskusteluihin ei ole vielä vastattu. Ole ensimmäinen.
Vastaukset 1
Vanhimmat
En osaa sanoa onko tuo yksinkertaisin mutta ei kovin monimutkainenkaan. Löytyy sivulta:
http://zhaofu.4y.com.cn/myweb/linux/sql/ch13_01.htm
#include
#include
#include
int main(char **args)
{
int connection, state;
m_result *result; m_row row;
/* connect to the mSQL database at athens.imaginary.com */
state = msqlConnect("athens.imaginary.com");
/* check for a connection error */
if( state == -1 )
{ /* print the error message stored in MsqlErrMsg */
printf(msqlErrMsg); return 1; }
else
{ /* the return balue from msqlConnect() is our connection handle */
connection = state; }
/* select which database to use on the server */
state = msqlSelectDB(connection, "db_test");
/* again, -1 means an error */
if( state == -1 )
{ printf(msqlErrMsg);
/* close up our connection before exiting */
msqlClose(connection);
return 1; }
state = msqlQuery(connection, "SELECT test_id, test_val FROM test");
if( state == -1 )
{ printf(msqlErrMsg); return 1; }
else
{ printf("Rows: %d\n", state); }
/* must call msqlStoreResult() before we can issue any * other Query() calls */
result = msqlStoreResult();
/* process each row in the result set */
while( ( row = msqlFetchRow(result)) != NULL )
{ printf("id: %s, val: %s\n", (row[0] ? row[0] : "NULL"), (row[1] ? row[1] : "NULL"));
} /* free the result set */
msqlFreeResult(result);
/* close the connection */
msqlClose(connection);
printf("Done.\n");
}