用c++配合MySql:分割故障
use C++ with MySql : segmentation fault
我正试图使一个c++工作与MySql我测试了以下来自链接
的代码https://www.raspberrypi.org/forums/viewtopic.php?t=31394& p = 272288
#include <iostream>
#include <mysql/mysql.h> // I added include /usr/include/mysql/ to ld.so.conf which is why that works
using namespace std;
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
#define HOST "localhost" // you must keep the quotes on all four items,
#define USER "root" // the function "mysql_real_connect" is looking for a char datatype,
#define PASSWD "123" // without the quotes they're just an int.
#define DB "temps"
int main()
{
//initialize database connection
mysql_init(&mysql);
// the three zeros are: Which port to connect to, which socket to connect to
// and what client flags to use. unless you're changing the defaults you only need to put 0 here
connection = mysql_real_connect(&mysql,HOST,USER,PASSWD,DB,0,0,0);
// Report error if failed to connect to database
if (connection == NULL) {
cout << mysql_error(&mysql) << endl;
return 1;
}
//Send query to database
query_state = mysql_query(connection, "select * from temps");
// store result
result = mysql_store_result(connection);
while ( ( row = mysql_fetch_row(result)) != NULL ) {
// Print result, it prints row[column_number])
cout << row[0] << "t" << row[1] << endl;
}
return 0;
}
文件编译成功:
g++ -o sqlOut -lmysqlclient sqlTest.cpp
但是我得到了错误:分段错误当试图运行编译文件!!
任何帮助将不胜感激,谢谢
NB:我需要保存数据从我的c++文件到MySql数据库,并查看它与phpMyAdmin,这将是非常有帮助的,如果你给我另一个链接或教程的傻瓜
我使用树莓PI b+
检查row[0]
和row[1]
是否包含值(不是NULL
)。试试这个:
while ( ( row = mysql_fetch_row(result)) != NULL ) {
if(!row[0] || !row[1]){
continue;
}
cout << row[0] << "t" << row[1] << endl;
}
我已经使用"temps"为数据库和表,我已经改变了表的名称
select * from "tempdat"
这里tempps是数据库名tempdat是表名=>它现在可以工作了
希望能帮助到有同样错误的人
相关文章:
- 分割故障C++矩阵
- C ,二进制树的高度,而不是检查我的子树是否为空,而是在检查我的子树是否是叶子节点.抛出分割故障
- 我正在为此代码分割故障
- 在执行strcpy时获取分割故障错误
- 空隙指针分割故障
- Xcode会产生分割故障,而不是例外
- OPENCV C 中的分割故障误差
- 分割故障:核心倾倒C 矢量对字符串:
- C 2D数组导致分割故障(核心倾倒)
- 分割故障/错误读数变量-C 列表
- DLSYM导致分割故障
- 使用C 中的操作员删除的分割故障
- 使用Linux X64下的Libmozjs-52(SpidermonKey)进行分割故障
- 使用长变量时分割故障(11)错误
- C - 返回字符串时的分割故障
- 创建VertexArray时的分割故障
- 向量分割故障的向量
- 从std :: fileSystem ::路径对象的类中退出时的分割故障
- C 矢量分割故障
- 基于向量的数据格式的分割故障误差