- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在建立一个自动称重系统以进行质量控制。为此,我使用 MySQL Connector/Arduino图书馆。
一切准备就绪,只剩下以下步骤了:
查询数据库,该数据库是我要评估的批处理号(批处理号以 VARCHAR
形式存储在 qcInsecta_get
数据表中)。
将上面获得的值与称重传感器返回给我的重量值一起包含在 INSERT
中。
我使用过 basic_insert 的示例和 basic_select由该库的作者提供。在数据表中插入值我没有遇到任何问题(我之前用探针值完成过)。但是,当我执行 SELECT 操作时,如果数据库字段是数字(例如 INT),则检索到的值始终是数字值。但如果数据库字段是VARCHAR,则返回0。
我不是 C 代码专家,但我已经看到它包含在示例中:
head_count = atol(row->values[0]);
并且 atol
将字符串转换为其数值。我尝试过使用其他形式或转换器来进行选择并从数据库中获取值,但这对我来说是不可能的。我该如何解决这个问题?
使用 NODEMCU V3 板执行 SELECT 的代码是:
#include <ESP8266WiFi.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
char ssid[] = ""; // SSID NAME
char pass[] = ""; // SSID PASSWORD
IPAddress server_addr(xxx, xxx, x, xxx);
char user[] = "";
char password[] = "";
char query[] = "SELECT batchnumber FROM registro.qcInsecta_get ORDER BY id DESC LIMIT 1";
WiFiClient client;
MySQL_Connection conn((Client *)&client);
void setup() {
delay(500);
Serial.begin(115200);
WiFi.begin(ssid, pass);
if (conn.connect(server_addr, 3306, user, password)) {
delay(100);
Serial.println(F("Connected"));
}
else {
Serial.println();
conn.close();
if (conn.connect(server_addr, 3306, user, password)) {
delay(500);
}
else {
Serial.println("...");
}
}
}
void loop() {
delay(1000);
row_values *row = NULL;
long head_count = 0;
Serial.println("1) Demonstrating using a cursor dynamically allocated.");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(query);
// Fetch the columns (required) but we don't use them.
column_names *columns = cur_mem->get_columns();
// Read the row (we are only expecting the one)
do {
row = cur_mem->get_next_row();
if (row != NULL) {
head_count = atol(row->values[0]);
}
} while (row != NULL);
// Deleting the cursor also frees up memory used
delete cur_mem;
// Show the result
Serial.print(" Batch number = ");
Serial.println(head_count);
delay(500);
}
关于数据库:
| qcInsecta | CREATE TABLE `qcInsecta` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`batchnumber` varchar(30) NOT NULL,
`weight` float NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `batchnumber` (`batchnumber`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8mb4 |
MariaDB [registro]> SELECT * FROM qcInsecta;
+----+-------------+--------+---------------------+
| id | batchnumber | weight | created_at |
+----+-------------+--------+---------------------+
| 5 | COL-01-05 | 35 | 2019-06-06 17:04:38 |
| 16 | COL-01-01 | 22 | 2019-06-06 18:57:02 |
| 17 | COL-01-01 | 22 | 2019-06-06 18:57:02 |
| 18 | COL-01-01 | 25 | 2019-06-06 21:52:02 |
最佳答案
使用head_count = atol(row->values[0]);
,您可以将输出的每个字符转换为一个长整型数字值。因此,您应该仅对 NUMERIC 字段执行此操作。对于字符串字段,您只需要 row->values
,它是一个 C 字符串数组。
基于this row->values
包含一个数组,其大小为 SELECT
列结果。例如,如果您选择一列(如当前的选择查询),row->values
的大小将为 1,并且您的结果将位于 row->values[0]
中>.
如果您需要分隔每一列,请尝试为每一列进行选择,或者您可以按照 complex_select.ino示例并迭代列而不是行,然后分离每个列类型并将它们转换为所需的值。
#include <ESP8266WiFi.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
char ssid[] = ""; // SSID NAME
char pass[] = ""; // SSID PASSWORD
IPAddress server_addr(xxx, xxx, x, xxx);
char user[] = "";
char password[] = "";
char query[] = "SELECT batchnumber FROM registro.qcInsecta_get ORDER BY id DESC LIMIT 1";
WiFiClient client;
MySQL_Connection conn((Client *)&client);
void setup()
{
delay(500);
Serial.begin(115200);
WiFi.begin(ssid, pass);
if (conn.connect(server_addr, 3306, user, password))
{
delay(100);
Serial.println(F("Connected"));
}
else
{
Serial.println();
conn.close();
if (conn.connect(server_addr, 3306, user, password))
{
delay(500);
}
else
{
Serial.println("...");
}
}
}
void loop()
{
delay(1000);
long head_count = 0;
Serial.println("1) Demonstrating using a cursor dynamically allocated.");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(query);
// Fetch the columns (required) but we don't use them.
Serial.println("Fetching with Columns");
column_names *columns = cur_mem->get_columns();
for (int f = 0; f < columns->num_fields; f++)
{
Serial.print(columns->fields[f]->name);
if (f < columns->num_fields - 1)
{
Serial.print(',');
}
}
Serial.println("Done");
// Read the rows and print them
Serial.println("Fetching with Rows");
row_values *row = NULL;
do
{
row = cur_mem->get_next_row();
if (row != NULL)
{
//e.g convert to a float value
// float batchnumber = 0;
// batchnumber = atol(row->values[0]);
// Serial.print("float value: ");
// Serial.println(batchnumber,2);
String batchnumber_str = "";
for (int f = 0; f < columns->num_fields; f++)
{
//just print the String value
// Serial.print(row->values[f]);
// convert value to String
batchnumber_str = String(row->values[f]);
Serial.print("batchnumber_str: ");
Serial.println(batchnumber_str);
if (f < columns->num_fields - 1)
{
Serial.print(',');
}
}
Serial.println();
}
} while (row != NULL);
Serial.println("Done");
// Deleting the cursor also frees up memory used
delete cur_mem;
// Show the result
Serial.print(" Batch number = ");
Serial.println(head_count);
delay(500);
}
关于mysql - 使用 MySQL Connector/Arduino 获取 select 中的 varchar 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56517352/
我想为 python 3 使用 mysql-connector 库。我可以使用 pymysql 代替,但是 mysql-connector 已经有一个连接池实现,而 pymysql 似乎没有。所以我要
哈,我正在尝试在 asp.net 4 中将 CKFinder 与 ckeditor 一起使用,但出现此错误: Could not load type 'CKFinder.Connector.Conne
哈,我正在尝试在 asp.net 4 中将 CKFinder 与 ckeditor 一起使用,但出现此错误: Could not load type 'CKFinder.Connector.Conne
SAP Java 连接器 是否仍然是将 Java 应用程序与 SAP 连接的好方法?将来(尤其是在 ECC 6.0 中)是否会有连接器的支持和维护,或者是使用 ECC 6.0 中的“企业服务”的唯一好
我一直在使用 AzureLogicApp 中的 FTP 连接器将 FTP 服务器中的文件从源文件夹解压缩到目标文件夹。 我已将 FTP 连接器配置为在源文件夹中添加文件时触发。 我面临的问题是此处触发
我一直在使用 AzureLogicApp 中的 FTP 连接器将 FTP 服务器中的文件从源文件夹解压缩到目标文件夹。 我已将 FTP 连接器配置为在源文件夹中添加文件时触发。 我面临的问题是此处触发
1) sudo dpkg -i mysql-connector-python_1.0.12-1ubuntu12.04_all.deb 2) sudo dpkg -i mysql-utilities_1
导入 mysql.connector ModuleNotFoundError:没有名为“mysql.connector”的模块; 'mysql' 不是一个包 pip install mysql-con
我正在为我们的一位客户开发 C# 应用程序。我们已经定义了一些 IDOC 结构。所有规范都在 Excel 表格中。 所以问题是:是否可以从“IDOCTYPE_READ_COMPLETE”函数获取整个
我有这两个表:gantt_tasks 和 gantt_links,我成功将任务添加到第一个表,但如果我尝试添加链接,则会出现此错误。 java.lang.ClassCastException: com
我正在尝试在远程 tomcat 服务器(8.5.39)上部署我的 java 后台。为了使用 https,我在/conf/server.xml 上更改了这些行 我可以在远程服务器上运行t
当我安装mysql-connector-python 2.0.1-1时( http://dev.mysql.com/downloads/connector/python/ ) 在 Ubuntu 14.
为了在 SFTP 写入连接器中获取文件名,我已将文件名存储在变量中并写入文件(暂存),然后将其写入目录。从不同的流中,我需要将相同的文件移动到输出位置,两个流不是相互链接的。 Mule4 中没有 Se
我在 virtualbox 中使用 vagrant。将数据插入数据库时出现此错误。我尝试修复错误的事情: 清除所有缓存、路由和配置 确保我的 env 文件已配置(我已经尝试将 127.0.0.1
我正在准备第一次使用jdbc,并且正在为MySQL安装jdbc驱动程序。 但是,我不清楚将这些文件中的哪些移动到 Eclipse 中的 WEB_INF/lib 文件夹中。它们似乎都包含相同的内容,并一
我使用的是Eclipse Helios v3.6,每次启动时,都会显示以下对话框。但是我不使用颠覆。有人知道如何停止吗? 最佳答案 我遇到过同样的问题。要修复它,我进入了Eclipse插件目录,并搜索
我已经在mongodb服务器中安装了mongo-connector。 我通过发出命令来执行 mongo-connector -m [remote mongo server IP]:[remote mo
我使用的是 gnu/linux 系统,特别是 Fedora 21 64 位。我想通过终端系统启动我的arduino IDE,突然,这个错误出现了: Could not find agent libra
Mysql-connector-java驱动版本问题 由于我的数据库版本是5.7.28 ,在使用java连接mysql时经常出现版本问题。 com.mysql.jdbc.Driver 是
当前问题集: 包含 mysql_connector 对象的 Python 应用程序 只能使用 mysql 访问远程服务器(不能使用 ssh、rsh、telnet 等) 目标: 使用 mysql 连接器
我是一名优秀的程序员,十分优秀!