c++ - 结合char插入数据库

标签 c++ mysql rfid

下面显示的代码在监视器上显示标签数据。我需要的是组合“t”中的所有字符并将其插入数据库。在下面的代码中发生的是,每次循环时,它都会将字符打印到 mysql 中的不同条目中。例如:如果十六进制标记长度为 10 字节长,它将在 10 个不同的条目中。我需要以某种方式组合字符并将其插入到 1 个条目中。

我试过将它变成字符串,但是 sprintf() "Query[256]"的第一个参数必须是字符声明,因此它给了我一个错误。

顺便说一句,下面的代码显示了从 RFID 阅读器读取的标签。

void CT1121Dlg::DisplayTagData(int cnt,int tag_len,int start_index)

{
MYSQL *pConnection;
MYSQL_RES *pResult=NULL; 
MYSQL_ROW Row;
char Query[256];
int a;
int z = 25; 
int fields;
pConnection = mysql_init(NULL);
mysql_real_connect(pConnection,"localhost","root","password","test",0,NULL,0); 


    CString s,s0;

    int i,j;

    unsigned char t;


for(i = 0; i < cnt; i++)
    {   
        s.Format("NO.%d: ",start_index+i+1);
        for(j = 0; j < tag_len; j++)
        {
            t = IdBuf[i].Ids[j];



            if(t < 0x10)
            {
                s0.Format("0%X ",t); // if hexa is less than 10 print 0 infront of it



            }
            else
                s0.Format("%X ",t); // else just print the 2 bit hexa decimal


            s += s0;
        **sprintf(Query, "INSERT into t(e) values (%x)",t);**

        if ( mysql_query(pConnection,Query) == 0 )
    {
        pResult = mysql_store_result( pConnection );    
        }

        }



        AddOprationInfo(s); // print string s on the screen


    }

 }

最佳答案

我认为您正在使用 MFC:只需将 Query 声明为 CString,然后使用 Format,就像您在所有其他地方已经在做的那样。

关于c++ - 结合char插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9653798/

相关文章:

C++ 用两种不同的方式初始化指针会产生两个答案

php - 嵌套的Mysqli连接

nfc - Mifare Classic 1k - 仅在 Samsung S III mini GT-i8190N 上出现错误

c++ - CMake 生成的 DLL 和 Curiously Recurring 模板 (C++) 的不正确行为

c++ - 基于自动机的 re2 替代方案

c++ - 如何以简单的方式从 C++ 中的循环中删除最后一个逗号?

php - 限制两列之和的结果

php - mysql ADDDATE问题更新记录

C++将特定数据模式写入RFID阅读器的文本文件

google-chrome - WebUSB 和 RFID 阅读器