gpt4 book ai didi

mysql - Arduino + MySql + MySqlIO 编译时出错

转载 作者:行者123 更新时间:2023-11-29 21:03:08 25 4
gpt4 key购买 nike

我正在尝试做这个project 。当我尝试编译此代码时:

#include <mysql.h>

char *host, *user, *pass, *db;
int isconnected = 0;

void setup()
{
Serial.begin(9600);
host = "localhost";
user = "root";
pass = "";
db = "arduino";
isconnected = mysql_connect(host,user,pass,db);
if(isconnected){
Serial.print("Connected to ");
Serial.println(host);
}
else{
Serial.println("Connection failed.");
}
mysql_close();
}

void loop(){}

也许问题出在库或 Arduino IDE 上。我收到这些错误和警告:

C:\Users\Mateusz\Documents\Arduino\libraries\mysql\examples\ConnectToMysql\ConnectToMysql.ino: In function 'void setup()':

C:\Users\Mateusz\Documents\Arduino\libraries\mysql\examples\ConnectToMysql\ConnectToMysql.ino:30:7: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

host = "localhost";

   ^

C:\Users\Mateusz\Documents\Arduino\libraries\mysql\examples\ConnectToMysql\ConnectToMysql.ino:31:7: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

user = "root";

   ^

C:\Users\Mateusz\Documents\Arduino\libraries\mysql\examples\ConnectToMysql\ConnectToMysql.ino:32:7: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

pass = "";

   ^

C:\Users\Mateusz\Documents\Arduino\libraries\mysql\examples\ConnectToMysql\ConnectToMysql.ino:33:5: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

db = "arduino";

 ^

C:\Users\Mateusz\Documents\Arduino\libraries\mysql\mysql.cpp: In function 'String mysql_result_query(String, String)':

C:\Users\Mateusz\Documents\Arduino\libraries\mysql\mysql.cpp:67:10: error: converting to 'String' from initializer list would use explicit constructor 'String::String(int, unsigned char)'

return 0;

      ^

C:\Users\Mateusz\Documents\Arduino\libraries\mysql\mysql.cpp:71:10: error: converting to 'String' from initializer list would use explicit constructor 'String::String(int, unsigned char)'

return 0;

      ^

exit status 1

编辑:有库mysql.cpp。

#include "mysql.h"

int mysql_connect(char *host, char *user, char *pass, char *db){
Serial.print("host=");
Serial.println(host);
Serial.print("user=");
Serial.println(user);
Serial.print("pass=");
Serial.println(pass);
Serial.print("db=");
Serial.println(db);
Serial.println("mysql_connect()");
int x = Serial.read();
if(x == '-')
return 0;
while( x <= 0){
x = Serial.read();
if(x == '-')
return 0;
}
return x-48;
}

int is_mysql(){
Serial.print("is_mysql()");
int x = Serial.read();
if(x == '-')
return 0;
while( x <= 0){
x = Serial.read();
if(x == '-')
return 0;
}
return x-48;
}

void mysql_close(){
Serial.println("mysql_close()");
}
int mysql_query(char *query){
Serial.print("query=");
Serial.println(query);
int x = Serial.read();
if(x == '-' || x == '0')
return 0;
while( x <= 0){
x = Serial.read();
if(x == '-' || x == '0')
return 0;
}
return x-12;
}

String mysql_result_query(String query, String field){
String res = "";
String q = "query=" + query + "&field=" + field;
Serial.println(q);
res = Serial.readString();
if(res == "-")
return 0;
while(res.length() <= 0){
res = Serial.readString();
if(res == "-")
return 0;
}
return res;
}

还有mysql.h:

#ifndef mysql_h
#define mysql_h

#include "Arduino.h"

int mysql_connect(char *, char *, char *, char *);
void mysql_close();
int is_mysql();
int mysql_query(char *);
String mysql_result_query(String, String);


#endif

我不知道如何解决这个问题。我找不到任何解决方案。抱歉我的英语:)

最佳答案

您需要在开始时初始化变量。试试这个:

#include <mysql.h>

char *host = "localhost", *user="root", *pass="", *db="arduino";
int isconnected = 0;

void setup()
{
Serial.begin(9600);
isconnected = mysql_connect(host,user,pass,db);
if(isconnected){
Serial.print("Connected to ");
Serial.println(host);
}
else{
Serial.println("Connection failed.");
}
mysql_close();
}

void loop(){}

错误来自您的mysql_result_query函数。对于返回 String 的函数,您将返回 0

试试这个。

String mysql_result_query(String query, String field){
String res = "";
String q = "query=" + query + "&field=" + field;
Serial.println(q);
res = Serial.readString();
if(res == "-")
return "";
while(res.length() <= 0){
res = Serial.readString();
if(res == "-")
return "";
}
return res;
}

这里我返回一个空字符串。您可以稍后使用 length() 方法进行检查。

关于mysql - Arduino + MySql + MySqlIO 编译时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37040443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com