/* ============================================================================ Name : mysql_sample.c Author : Peter Version : Copyright : Description : ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include <mysql/mysql.h> struct connection_details { char *server; char *user; char *password; char *database; }; MYSQL* mysql_connection_setup(struct connection_details mysql_details) { // first of all create a mysql instance and initialize the variables within MYSQL *connection = mysql_init(NULL); // connect to the database with the details attached. if (!mysql_real_connect(connection, mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) { printf("Conection error : %s\n", mysql_error(connection)); exit(1); } return connection; } MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query) { // send the query to the database if (mysql_query(connection, sql_query)) { printf("MySQL query error : %s\n", mysql_error(connection)); exit(1); } return mysql_use_result(connection); } int main(void) { MYSQL *conn; // the connection MYSQL_RES *res; // the results MYSQL_ROW row; // the results row (line by line) struct connection_details mysqlD; mysqlD.server = "localhost"; // where the mysql database is mysqlD.user = "root"; // the root user of mysql mysqlD.password = "ds630805"; // the password of the root user in mysql mysqlD.database = "test_db"; // the databse to pick // connect to the mysql database conn = mysql_connection_setup(mysqlD); // assign the results return to the MYSQL_RES pointer res = mysql_perform_query(conn, "show tables"); printf("MySQL Tables in mysql database:\n"); while ((row = mysql_fetch_row(res)) != NULL) printf("%s\n", row[0]); printf("\n\n");; res = mysql_perform_query(conn, "select * from employee"); printf("employee data in mysql database:\n"); while ((row = mysql_fetch_row(res)) != NULL) printf("%s,%s,%s,%s \n", row[0],row[1],row[2],row[3]); // delete if (mysql_perform_query(conn,"insert into employee values(NULL,'Peter','RD','RD')")==0) printf("inert ok\n"); else printf("inert error %d: %sn",mysql_errno(conn),mysql_error(conn)); // delete if (mysql_perform_query(conn,"delete from employee where IDpk=4")==0) printf("delete ok\n"); else printf("delete error %d: %sn",mysql_errno(conn),mysql_error(conn)); //mysql_commit(conn); /* clean up the database result set */ mysql_free_result(res); /* clean up the database link */ mysql_close(conn); return EXIT_SUCCESS; }
2013年1月7日 星期一
使用 C/C++ 存取 MySQL範例
標籤:
Database,
Programming
2012年10月30日 星期二
Install NS2 in ubuntu
This post will help you in installing Network Simulator 2 version NS2.35 in Ubuntu 11.10
Instructions
Install Ubuntu
Download NS-2.35 (http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download)
Unzip or untar it to any folder (recommended is /home/loginname) using the following commands one by one
sudo apt-get update
Instructions
Install Ubuntu
Download NS-2.35 (http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download)
Unzip or untar it to any folder (recommended is /home/loginname) using the following commands one by one
sudo apt-get update
2012年9月10日 星期一
iOS 中如何禁止進入螢幕保護狀態
在 iOS 程式中,如果希望我們的程式不要進入螢幕保護狀態,例如導航軟體一樣,作法其實很簡單,只要使用
這樣程式就不會進入螢幕保護狀態了。但是如果使用者手動的方式進入 Lock Mode ,而重新解鎖之後你會發現,原本在執行的 Server 都被關閉了,這個時候你可以在下列函數中重新啟動
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
這樣程式就不會進入螢幕保護狀態了。但是如果使用者手動的方式進入 Lock Mode ,而重新解鎖之後你會發現,原本在執行的 Server 都被關閉了,這個時候你可以在下列函數中重新啟動
標籤:
iOS,
Programming
2012年7月5日 星期四
CCITT 16 Bit CRC
CRC 在程式設計中是相當重要的功能,他提供了錯誤偵測的能力(沒有修正,要有修正能力,要使用 ECC),確保我們傳送的數據是正確無誤的,這不管是在寫軟體、韌體都是經常會使用的方法。
Linux uPnP Programming (一)
在 Linux 上要如何開發 uPnP 程式呢?當然要先安裝 uPnP lib 。然後我以 lib 中所附上的 sample code 來解說,本篇我們先來解說 Control Point。下載完成之後解壓縮,再目錄中你會看到 upup/sample 目錄,我們現在來編譯看看
這個時候你應該會看到三的執行檔案,如下:
shanchieh@peter:Downloads$ cd libupnp-1.6.17/
shanchieh@peter:libupnp-1.6.17$ ./configure
shanchieh@peter:libupnp-1.6.17$ make
shanchieh@peter:libupnp-1.6.17$ cd upnp/sample/
這個時候你應該會看到三的執行檔案,如下:
訂閱:
文章 (Atom)