65,207
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
#define OTL_ODBC_MYSQL
#include <otlv4.h>
otl_connect db;
int main()
{
char* constr="Driver={MySQL ODBC 5.1 Driver};Server=localhost; Database=test; Uid=****; Pwd=****; CharSet=UTF8; ";
otl_connect::otl_initialize();
try
{
cerr<<constr<<endl;
db.rlogon(constr);
cerr<<"已连接数据库"<<endl;
db.logoff();
}
catch(otl_exception& p)
{
cerr<<p.msg<<endl;
cerr<<p.stm_text<<endl;
cerr<<p.code<<endl;
cerr<<p.sqlstate<<endl;
cerr<<p.var_info<<endl;
}
cout << "Hello world!" << endl;
return 0;
}
#include <stdio.h>
#include <winsock.h>
#include "mysql.h"
int main(int argc, char* argv[])
{
unsigned short Port = 3306;
char* IPAddress = "localhost";
char* UserName = "root";
char* Password = "123456";
char* DBName = "test";
unsigned long i;
printf("Start... \n");
MYSQL* ssock;
MYSQL_RES* res;
MYSQL_ROW row;
ssock = (MYSQL*)malloc(sizeof(MYSQL));
mysql_init(ssock);
if (ssock == NULL) {
printf("EROR: MySQL ssock init error. \n");
return FALSE;
}
printf("MySQL ssock init OK. \n");
//连接到指定的数据库
ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0);
if (!ssock) {
printf("conn fail... \n");
unsigned int mtint = mysql_errno(ssock);
return FALSE;
}
printf("MySQL connnect OK... \n");
if (mysql_select_db(ssock, DBName) != 0) {
printf("select db error. \n");
return FALSE;
}
printf("select db OK. \n");
printf("version=%d \n", mysql_get_server_version(ssock));
//SQL查询语句
if (mysql_query(ssock, "select * from testuser where id")) {
printf("mysql_query() Error, %s\n", mysql_error(ssock));
}
if (!(res = mysql_store_result(ssock))) {
printf("mysql_store_result() Error, %s\n", mysql_error(ssock));
}
while ((row = mysql_fetch_row(res))) {
for (i = 0 ; i < mysql_num_fields(res); i++) {
printf("%s ", row[i]);
}
printf("\n");
}
mysql_free_result(res);
mysql_close(ssock);
printf("End... \n");
return TRUE;
}