c++ mysql ctime_C++操作mysql数据库范例代码

C++操作mysql数据库范例代码

#include

#include

void TestMySQL()

{

TRACE("MySQL client version: %s\n", mysql_get_client_info());

MYSQL *conn = mysql_init(NULL);

if (conn == NULL) {

TRACE("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));

}

if (mysql_real_connect(conn, "127.0.0.1", "user", "pass", "testdb", 0, NULL, 0) == NULL) {

TRACE("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));

}

mysql_query(conn, "set names utf8");

// SELECT

mysql_query(conn, "SELECT * FROM nihongo");

MYSQL_RES *result = mysql_store_result(conn);

int num_fields = mysql_num_fields(result);

CString str = _T("");

wchar_t bufUnicode[MAX_PATH];

char bufUTF8[MAX_PATH];

MYSQL_ROW row;

while ((row = mysql_fetch_row(result))) {

for (int i = 0; i < num_fields; i++) {

if (row[i] == NULL) {

TRACE("NULL ");

continue;

}

int iLenUnicode = MultiByteToWideChar(CP_UTF8, 0, row[i], -1, NULL, 0);

if (iLenUnicode <= sizeof(bufUnicode)/sizeof(bufUnicode[0])) {

MultiByteToWideChar(CP_UTF8, 0, row[i], -1, bufUnicode, MAX_PATH);

str += bufUnicode;

str += _T(", ");

}

}

TRACE("\n");

}

mysql_free_result(result);

// INSERT

CTime now = CTime::GetCurrentTime();

CString s_now = now.Format(_T("%Y-%m-%d %H:%M:%S"));

CString insert = _T("INSERT INTO nihongo VALUES('本日は") + s_now + _T("です')");

wchar_t *p = insert.GetBuffer();

int iLenUtf8 = WideCharToMultiByte(CP_UTF8, 0, p, -1, NULL, 0, NULL, NULL);

if (iLenUtf8 <= sizeof(bufUTF8)) {

WideCharToMultiByte(CP_UTF8, 0, p, -1, bufUTF8, sizeof(bufUTF8), NULL, NULL);

mysql_query(conn, bufUTF8);

}

insert.ReleaseBuffer();

mysql_close(conn);

AfxMessageBox(str);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值