简体   繁体   English

sqlite3(语言C):没有这样的列

[英]sqlite3 (language C): no such column

I want to save the data from USB into the database sqlite3. 我想将数据从USB保存到数据库sqlite3中。

Here is data from USB 这是来自USB的数据

char T0[8], T1[8], T2[8], T3[8], T4[8];

I create a table with 我创建了一个表

const char* Temprature_table = "Create table Temprature_1 (ID INTERGER PRIMARY KEY,Thermo_0 decimal(5,1),Thermo_1 decimal(5,1),Thermo_2 decimal(5,1),Thermo_3 decimal(5,1),Thermo_4 decimal(5,1),time DATETIME)";

then I insert data into the table 然后我将数据插入表中

result = sqlite3_exec (DB,"Insert  into Temprature VALUES(NULL, T0, T1, T2, T3, T4, time)",0,0,&errmsg);

or 要么

char array[256];
sprintf(array, "Insert into Temprature_1 VALUES(NULL, T0, T1, T2, T3, T4, time)");
result = sqlite3_exec (DB,array,0,0,&errmsg);

but there is a problem: "cannot insert data: no such column: T0". 但是有一个问题:“无法插入数据:没有这样的列:T0”。 I do not know why. 我不知道为什么。 Thanks. 谢谢。

Your C code and your SQL code is run in different domains and do not share variables. 您的C代码和SQL代码在不同的域中运行,不共享变量。 So you can not directly access C variables from SQL code. 所以你不能直接从SQL代码访问C变量。 From looking at the documentation this seems to be the correct solution: 从查看文档看,这似乎是正确的解决方案:

sqlite3_stmt *ppStmt;
sqlite3_prepare_v2(DB, "Insert into Temprature_1 VALUES(NULL, ?, ?, ?, ?, ?, time)", 58, &ppStmt, NULL);

sqlite3_bind_text(ppStmt, 1, T0, 8, SQLITE_TRANSIENT);
sqlite3_bind_text(ppStmt, 2, T1, 8, SQLITE_TRANSIENT);
sqlite3_bind_text(ppStmt, 3, T2, 8, SQLITE_TRANSIENT);
sqlite3_bind_text(ppStmt, 4, T3, 8, SQLITE_TRANSIENT);
sqlite3_bind_text(ppStmt, 5, T4, 8, SQLITE_TRANSIENT);

sqlite3_step(ppStmt);
sqlite3_finalize(ppStmt);

You don't include the names of C variables in the SQL query string. 您不在SQL查询字符串中包含C变量的名称

You either include a verbatim value (eg by programmatically composing the string from smaller pieces, eg using sprintf ), or you use placeholders for parameters. 您可以包含逐字值(例如,通过编程从较小的部分组成字符串,例如使用sprintf ),或者使用占位符作为参数。 The latter is more robust against SQL injection attacks, and generally preferred. 后者对SQL注入攻击更具鲁棒性,通常更受欢迎。 In the case of C as the host language, it will also save you the trouble of allocating sufficient memory for the pasted query. 在C作为宿主语言的情况下,它还可以省去为粘贴的查询分配足够内存的麻烦。

You can use placeholders by creating a prepared statement and binding parameters to it. 您可以通过为其创建预 准备语句绑定参数来使用占位符 When you execute that statement, the bound parameters will be used in the places indicated by the placeholders. 执行该语句时,绑定参数将用于占位符指示的位置。

You should use prepared statements to bind values to your query. 您应该使用预准备语句 将值绑定到查询。 A regular SQL has the form 常规 SQL具有该表单

INSERT INTO Temperature_1 (Thermo_0, Thermo_1) VALUES (23.2, 42.3)

A prepared statement looks like 准备好的陈述看起来像

INSERT INTO Temperature_1 (Thermo_0, Thermo_1) VALUES (?, ?)

which allows for security and performance enhancements. 这允许安全性和性能增强。 So your final code will look like 所以你的最终代码看起来像

sqlite3_stmt *stmt;

sqlite3_prepare_v2(
  db,
  "INSERT INTO Temperature (Thermo_0, Thermo_1) VALUES (?, ?)",
  -1,
  &stmt,
  NULL
);

sqlite3_bind_text(stmt, 1, T0, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, T1, -1, SQLITE_TRANSIENT);

sqlite3_step(stmt);

See this SQLite introduction to the C API. 请参阅SQLite对C API的介绍 Your code doesn't work because you submit the following string to the query engine 您的代码不起作用,因为您将以下字符串提交给查询引擎

INSERT INTO table (T0);

while the syntax of the INSERT statement looks like 而INSERT语句的语法看起来像

INSERT INTO <table> VALUES (<expression>[, <expression>[, ...]])

When the database engine evaluates your query, it can't know that TO happens to be the name of a variable somewhere in your code, thus issue that error. 当数据库引擎评估您的查询时,它无法知道TO恰好是代码中某处的变量名称,从而发出该错误。 The SQL is a language of its own, it doesn't share anything with the C context. SQL是它自己的语言,它不与C上下文共享任何东西。

As a final note, the correct spelling seems to be TEMPERATURE , not TEMPRATURE (you missed an E after PR) 作为最后一点,正确的拼写似乎是TEMPERATURE ,而不是TEMPRATURE(PR之后错过了E)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM