![](/media/cache/profiles/DG_Mugshot.png.50x50_q85.jpg)
Dependencies: EthernetNetIf mbed MySQLClient
MySQLClientExample.cpp@2:2711bbad1623, 2010-07-09 (annotated)
- Committer:
- donatien
- Date:
- Fri Jul 09 14:49:01 2010 +0000
- Revision:
- 2:2711bbad1623
- Parent:
- 0:82effba6633e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:82effba6633e | 1 | #include "mbed.h" |
donatien | 0:82effba6633e | 2 | #include "EthernetNetIf.h" |
donatien | 0:82effba6633e | 3 | #include "MySQLClient.h" |
donatien | 0:82effba6633e | 4 | |
donatien | 0:82effba6633e | 5 | #define SQL_SERVER "" |
donatien | 0:82effba6633e | 6 | #define SQL_USER "" |
donatien | 0:82effba6633e | 7 | #define SQL_PASSWORD "" |
donatien | 0:82effba6633e | 8 | #define SQL_DB "" |
donatien | 0:82effba6633e | 9 | |
donatien | 0:82effba6633e | 10 | EthernetNetIf eth; |
donatien | 0:82effba6633e | 11 | MySQLClient sql; |
donatien | 0:82effba6633e | 12 | |
donatien | 0:82effba6633e | 13 | MySQLResult sqlLastResult; |
donatien | 0:82effba6633e | 14 | void onMySQLResult(MySQLResult r) |
donatien | 0:82effba6633e | 15 | { |
donatien | 0:82effba6633e | 16 | sqlLastResult = r; |
donatien | 0:82effba6633e | 17 | } |
donatien | 0:82effba6633e | 18 | |
donatien | 0:82effba6633e | 19 | int main() |
donatien | 0:82effba6633e | 20 | { |
donatien | 0:82effba6633e | 21 | printf("Start\n"); |
donatien | 0:82effba6633e | 22 | |
donatien | 0:82effba6633e | 23 | printf("Setting up...\n"); |
donatien | 0:82effba6633e | 24 | EthernetErr ethErr = eth.setup(); |
donatien | 0:82effba6633e | 25 | if(ethErr) |
donatien | 0:82effba6633e | 26 | { |
donatien | 0:82effba6633e | 27 | printf("Error %d in setup.\n", ethErr); |
donatien | 0:82effba6633e | 28 | return -1; |
donatien | 0:82effba6633e | 29 | } |
donatien | 0:82effba6633e | 30 | printf("Setup OK\n"); |
donatien | 0:82effba6633e | 31 | |
donatien | 0:82effba6633e | 32 | Host host(IpAddr(), 3306, SQL_SERVER); |
donatien | 0:82effba6633e | 33 | |
donatien | 0:82effba6633e | 34 | //Connect |
donatien | 0:82effba6633e | 35 | sqlLastResult = sql.open(host, SQL_USER, SQL_PASSWORD, SQL_DB, onMySQLResult); |
donatien | 0:82effba6633e | 36 | while(sqlLastResult == MYSQL_PROCESSING) |
donatien | 0:82effba6633e | 37 | { |
donatien | 0:82effba6633e | 38 | Net::poll(); |
donatien | 0:82effba6633e | 39 | } |
donatien | 0:82effba6633e | 40 | if(sqlLastResult != MYSQL_OK) |
donatien | 0:82effba6633e | 41 | { |
donatien | 0:82effba6633e | 42 | printf("Error %d during connection\n", sqlLastResult); |
donatien | 0:82effba6633e | 43 | } |
donatien | 0:82effba6633e | 44 | |
donatien | 0:82effba6633e | 45 | //SQL Command |
donatien | 0:82effba6633e | 46 | //Make command |
donatien | 0:82effba6633e | 47 | char cmd[128] = {0}; |
donatien | 0:82effba6633e | 48 | const char* msg="Hello World!"; |
donatien | 0:82effba6633e | 49 | sprintf(cmd, "INSERT INTO Test (Test) VALUES('%s')", msg); |
donatien | 0:82effba6633e | 50 | |
donatien | 0:82effba6633e | 51 | //INSERT INTO DB |
donatien | 0:82effba6633e | 52 | string cmdStr = string(cmd); |
donatien | 0:82effba6633e | 53 | sqlLastResult = sql.sql(cmdStr); |
donatien | 0:82effba6633e | 54 | while(sqlLastResult == MYSQL_PROCESSING) |
donatien | 0:82effba6633e | 55 | { |
donatien | 0:82effba6633e | 56 | Net::poll(); |
donatien | 0:82effba6633e | 57 | } |
donatien | 0:82effba6633e | 58 | if(sqlLastResult != MYSQL_OK) |
donatien | 0:82effba6633e | 59 | { |
donatien | 0:82effba6633e | 60 | printf("Error %d during SQL Command\n", sqlLastResult); |
donatien | 0:82effba6633e | 61 | } |
donatien | 0:82effba6633e | 62 | |
donatien | 0:82effba6633e | 63 | sql.exit(); |
donatien | 0:82effba6633e | 64 | |
donatien | 0:82effba6633e | 65 | while(1) |
donatien | 0:82effba6633e | 66 | { |
donatien | 0:82effba6633e | 67 | |
donatien | 0:82effba6633e | 68 | } |
donatien | 0:82effba6633e | 69 | |
donatien | 0:82effba6633e | 70 | return 0; |
donatien | 0:82effba6633e | 71 | } |