通过c++将记录从一个数据库的表复制到另一个数据库的同名表
Copy records from a table of a database to a table of same name of other database through c++
有一个名为"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"模板,并在每次迭代中将行条目作为值传递。
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 使用strcpy将char数组的元素复制到另一个数组
- 是否可以初始化不可复制类型的成员变量(或基类)
- 查询SQLite数据库中的日期
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 复制列表初始化的隐式转换的等级是多少
- 当从函数参数中的临时值调用复制构造函数时
- 有可能在Armadillo中复制MATLAB circshift方法吗
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 复制几乎为空的数组的最快方法
- 如何使用C ++将SQLite数据库中的所有表从一个数据库复制到另一个数据库
- QT Creator将SQLite复制为空数据库
- 将特定结果集从Oracle数据库复制到C 中的SQLite数据库的最佳方法
- C++将 SQLite Blob 从一个数据库复制到另一个数据库
- 如何将sql数据库文件从系统位置复制到另一个位置
- 在 sqlite 数据库 qt 之间复制表会导致错误
- 通过c++将记录从一个数据库的表复制到另一个数据库的同名表
- 开源C或c++嵌入式关系数据库(复制源代码以部署)