无法找到字符串文字操作员; conteudo;使用' const char [43]',' unsigned int'争论
我遇到了问题,在变量上向MySQL数据库发送数据。问题是我试图通过变量发送RFID数据。
String INSERT_SQL ="INSERT INTO database.table VALUES('0 ',"conteudo")";
它给出了这个错误。
无法找到字符串字符串运算符'Operator“ operator”“ conteudo”,带有'const char [43]','unsigned int'参数
,
这是完整的代码。
#include <WiFi.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <MySQL_Generic.h>
Servo servo;
#define SS_PIN 21
#define RST_PIN 22
MFRC522 mfrc522(SS_PIN, RST_PIN);
const char* ssid = "";
const char* password = "";
char user[] = "";
char passw[] = "";
String conteudo = "";
#define MYSQL_DEBUG_PORT Serial
#define MYSQL_LOGLEVEL 0
#define USING_HOST_NAME true
#if USING_HOST_NAME
char server[] = "";
char default_database[] = "";
char default_table[] = "";
#else
IPAddress server(192, 168, 0, 250);
#endif
uint16_t server_port = 3306;
MySQL_Connection conn((Client *)&client);
MySQL_Query *query_mem;
int led_CH1 = 14;
int pos = 0;
void setup()
{
pinMode(led_CH1, OUTPUT);
Serial.begin(115200);
while (!Serial && millis() < 5000);
SPI.begin();
iniciarWiFi();
MYSQL_DISPLAY1("Connecting to", ssid);
MYSQL_DISPLAY3("Connecting to SQL Server @", server, ", Port =", server_port);
MYSQL_DISPLAY5("User =", user, ", PW =", passw, ", DB =", default_database);
mfrc522.PCD_Init();
servo.attach(25);
servo.write(100);
Serial.println("Aproxime o seu cartao do leitor...");
Serial.println();
}
void loop()
{
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.print("UID da tag :");
byte letra;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
conteudo.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
conteudo.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Mensagem : ");
conteudo.toUpperCase();
if (conteudo.substring(1) == "B0 21 A4 39")
{
digitalWrite(led_CH1, HIGH);
Serial.println("Cartao1 - Acesso concedido!");
Serial.println();
String INSERT_SQL ="INSERT INTO database.table VALUES('0 ',"conteudo")";
if (conn.connectNonBlocking(server, server_port, user, passw) != RESULT_FAIL)
{
delay(500);
runInsert();
conn.close(); // close the connection
}
else
{
MYSQL_DISPLAY("\nConnect failed. Trying again on next iteration.");
}
delay(100);
delay(3000);
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
delay(1500);
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(led_CH1, LOW);
}
if (conteudo.substring(1) != "B0 21 A4 39")
{
Serial.println("Cartao2 - Acesso negado !!");
Serial.println();
}
delay(1000);
}
void iniciarWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("A conectar ao WiFi .");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void runInsert()
{
// Initiate the query class instance
MySQL_Query query_mem = MySQL_Query(&conn);
if (conn.connected())
{
MYSQL_DISPLAY(INSERT_SQL);
// Execute the query
// KH, check if valid before fetching
if ( !query_mem.execute(INSERT_SQL.c_str()) )
{
MYSQL_DISPLAY("Insert error");
}
else
{
MYSQL_DISPLAY("Data Inserted.");
}
}
else
{
MYSQL_DISPLAY("Disconnected from Server. Can't insert.");
}
}
Im having a problem sending data on a variable to mysql database. The problem is im trying to send RFID data with a variable.
String INSERT_SQL ="INSERT INTO database.table VALUES('0 ',"conteudo")";
It's giving this error.
unable to find string literal operator 'operator""conteudo' with 'const char [43]', 'unsigned int' arguments
Here's the full code.
#include <WiFi.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <MySQL_Generic.h>
Servo servo;
#define SS_PIN 21
#define RST_PIN 22
MFRC522 mfrc522(SS_PIN, RST_PIN);
const char* ssid = "";
const char* password = "";
char user[] = "";
char passw[] = "";
String conteudo = "";
#define MYSQL_DEBUG_PORT Serial
#define MYSQL_LOGLEVEL 0
#define USING_HOST_NAME true
#if USING_HOST_NAME
char server[] = "";
char default_database[] = "";
char default_table[] = "";
#else
IPAddress server(192, 168, 0, 250);
#endif
uint16_t server_port = 3306;
MySQL_Connection conn((Client *)&client);
MySQL_Query *query_mem;
int led_CH1 = 14;
int pos = 0;
void setup()
{
pinMode(led_CH1, OUTPUT);
Serial.begin(115200);
while (!Serial && millis() < 5000);
SPI.begin();
iniciarWiFi();
MYSQL_DISPLAY1("Connecting to", ssid);
MYSQL_DISPLAY3("Connecting to SQL Server @", server, ", Port =", server_port);
MYSQL_DISPLAY5("User =", user, ", PW =", passw, ", DB =", default_database);
mfrc522.PCD_Init();
servo.attach(25);
servo.write(100);
Serial.println("Aproxime o seu cartao do leitor...");
Serial.println();
}
void loop()
{
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.print("UID da tag :");
byte letra;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
conteudo.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
conteudo.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Mensagem : ");
conteudo.toUpperCase();
if (conteudo.substring(1) == "B0 21 A4 39")
{
digitalWrite(led_CH1, HIGH);
Serial.println("Cartao1 - Acesso concedido!");
Serial.println();
String INSERT_SQL ="INSERT INTO database.table VALUES('0 ',"conteudo")";
if (conn.connectNonBlocking(server, server_port, user, passw) != RESULT_FAIL)
{
delay(500);
runInsert();
conn.close(); // close the connection
}
else
{
MYSQL_DISPLAY("\nConnect failed. Trying again on next iteration.");
}
delay(100);
delay(3000);
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
delay(1500);
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(led_CH1, LOW);
}
if (conteudo.substring(1) != "B0 21 A4 39")
{
Serial.println("Cartao2 - Acesso negado !!");
Serial.println();
}
delay(1000);
}
void iniciarWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("A conectar ao WiFi .");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void runInsert()
{
// Initiate the query class instance
MySQL_Query query_mem = MySQL_Query(&conn);
if (conn.connected())
{
MYSQL_DISPLAY(INSERT_SQL);
// Execute the query
// KH, check if valid before fetching
if ( !query_mem.execute(INSERT_SQL.c_str()) )
{
MYSQL_DISPLAY("Insert error");
}
else
{
MYSQL_DISPLAY("Data Inserted.");
}
}
else
{
MYSQL_DISPLAY("Disconnected from Server. Can't insert.");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试将字符串嵌入另一个字符串中。这是不允许的。
如果要在字符串文字中使用
“
”,则需要这样逃脱:如果要将
conteudo
的值插入SQL查询中,则需要执行下列:You are trying to embed a string within another string. This is not allowed.
If you want to use
"
within the string literal, you need to escape them like this:If you want to insert the value of
conteudo
into SQL query, you need to do the following: