通过c++将记录从一个数据库的表复制到另一个数据库的同名表

Copy records from a table of a database to a table of same name of other database through c++

本文关键字:数据库 复制 另一个 一个 记录 c++ 通过      更新时间:2023-10-16

有一个名为"rfidacs"的数据库,该数据库中有一个名为"Alarm"的表。现在我想将Alarm表的所有记录复制到另一个名为DbDemo的数据库的同名"Alarm"表中。我已经写了这个代码。我是在linux的eclipse IDE上编程的。

int DBDEMO :: demoTest()

{
MYSQL_ROW row;
MYSQL *conn, *conn1;
conn  = mysql_init(NULL);
conn1 = mysql_init(NULL);
if(conn == NULL)
{
    fprintf(stderr,"mysql_init() failed");
    exit(1);
}
if(conn1 == NULL)
    {
        fprintf(stderr,"mysql_init() failed");
        exit(1);
    }
if( mysql_real_connect(conn1, "localhost", "root", "rgb123", "DbDemo", 0, NULL, 0)==NULL)
    {
        finish_with_error(conn1);
    }
if( mysql_real_connect(conn, "localhost", "root", "rgb123", "rfidacs", 0, NULL, 0)==NULL)
{
    finish_with_error(conn);
}
if(mysql_query(conn,"select * from Alarm"))
        {
            finish_with_error(conn);
        }
MYSQL_RES *result = mysql_store_result(conn);
if(result == NULL)
{
    finish_with_error(conn);
}
int num_fields = mysql_num_fields(result);

while((row=mysql_fetch_row(result)))
{
    char *temp;
    for(int i=0;i<num_fields;i++)
    {
    temp="Insert into Alarm values (row[i])";
    mysql_query(conn1, temp);
        if(mysql_query(conn1, "Insert into Alarm values row"))
          {
            finish_with_error(conn1);
          }
    }
}
mysql_free_result(result);
mysql_close(conn);
mysql_close(conn1);
}
return SUCCESS;

}

Thanks in advance

我认为准备好的语句是你正在寻找的。

您创建"prepared statement"模板,并在每次迭代中将行条目作为值传递。