mysql操作代码

int _tmain(int argc, _TCHAR* argv[])
{
    MYSQL MyData;

   
if (0 != mysql_library_init(0, NULL, NULL))
    {
        cout
<< "mysql_library_init 失败!" << endl;
        cout
<< mysql_error(&MyData) << endl;
       
return -1;
    }

   
if (NULL == mysql_init(&MyData))
    {
        cout
<< "mysql_ini 失败!" << endl;
        cout
<< mysql_error(&MyData) << endl;
       
return -1;
    }

   
if (0 != mysql_options(&MyData, MYSQL_SET_CHARSET_DIR, "gbk"))
    {
        cout
<< "设置gbk字符集失败!" << endl;
        cout
<< mysql_error(&MyData) << endl;
       
return -1;
    }

   
if (NULL == mysql_real_connect(&MyData, "localhost", "root", "123", "test", 6666, NULL, 0))
    {
        cout
<< "连接数据库失败!" << endl;
        cout
<< mysql_error(&MyData) << endl;
    }

   
string SqlStr = "create table bblovedd";
    SqlStr
+= "(";
    SqlStr
+= "name varchar(30),";
    SqlStr
+= "id int not null primary key";
    SqlStr
+= ")";

   
if (0 != mysql_query(&MyData, SqlStr.c_str()))
    {
        cout
<< "创建表失败!" << endl;
        cout
<< mysql_error(&MyData) << endl;
       
return -1;
    }
   
    SqlStr
= "insert into bblovedd values ('wangjb', 20090703)";
   
if (0 != mysql_query(&MyData, SqlStr.c_str()))
    {
        cout
<< "插入数据失败!" << endl;
        cout
<< mysql_error(&MyData) << endl;
       
return -1;
    }

    SqlStr
= "select * from bblovedd";
    MYSQL_RES
*result = NULL;
   
if (0 != mysql_query(&MyData, SqlStr.c_str()))
    {
        cout
<< "查询数据失败!" << endl;
        cout
<< mysql_error(&MyData) << endl;
       
return -1;
    }

    result
= mysql_store_result(&MyData);
    my_ulonglong RowCount
= mysql_num_rows(result);
    cout
<< "Row count:" << RowCount << endl;

    unsigned
int FieldCount = mysql_num_fields(result);
    MYSQL_FIELD
*field = NULL;

   
for (unsigned int i = 0; i < FieldCount; i++)
    {

        field
= mysql_fetch_field_direct(result, i);
        cout
<< field->name << '/t';
    }
    cout
<< endl;

    MYSQL_ROW row
= NULL;
    row
= mysql_fetch_row(result);
   
while (row)
    {
       
for (unsigned int i = 0; i < FieldCount; i++)
            cout
<< row[i] << '/t';
        cout
<< endl;
        row
= mysql_fetch_row(result);
    }
   
    SqlStr
= "drop table bblovedd";
   
if (0 != mysql_query(&MyData, SqlStr.c_str()))
    {
        cout
<< "删除表格失败!" << endl;
        cout
<< mysql_error(&MyData) << endl;
       
return -1;
    }

    mysql_free_result(result);
    mysql_close(
&MyData);
    mysql_server_end();

   
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值