2010-10-04 30 views
0

這是在PHP中很容易:如何在c/C++中從MySQL獲取記錄?

$con = mysql_connect("localhost:".$LOCAL_DB_PORT, $LOCAL_DB_USER, $LOCAL_DB_PASS); 
mysql_select_db("db", $con); 
mysql_query("set names utf8", $con); 

$result = mysql_query("select ..."); 
while($row = mysql_fetch_assoc($result)) 
{ 
... 
} 

但是,什麼是用C/C++在windows做到這一點最簡單的方法?

回答

0
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; 
} 

對於工作示例看看:http://www.codingfriends.com/index.php/2010/02/17/mysql-connection-example/

0

你需要獲得MySQL連接器/ C的保持++。下載和使用信息是here

如果許可限制阻止了這一點,您可以使用Connector/ODBC

+0

'c'中是否有連接器? – ollydbg 2010-10-05 03:47:04

+0

@ollydbg - 是的,請查看http://dev.mysql.com/doc/refman/5.1/en/c.html上的文檔 – 2010-10-05 10:41:13