我想创建一个注册函数,我想验证用户名是否已经在我的数据库中。 我不知道如何验证“SELECT”语句是否返回NULL。 这是我迄今写的:
strcat(msg, "Insert username:\n"); if(write(client, msg, 100) <= 0) { perror("Error"); exit(1); } if(read(client, name, 30) <= 0) { perror("Error"); exit(1); } sqlite3_stmt *res; char sql[100]; sprintf("SELECT * FROM users where name=\"%s\";", name); int rc = sqlite3_prepare_v2(db, sql, -1, &res, NULL); if(rc != SQLITE_OK) { perror("Error"); exit(1); } int step = sqlite3_step(res); if(step != SQLITE_DONE) { perror("Error"); exit(1); }I want to create a registration function and I want to verify if a user name is already in my database. I do not know how to verify if "SELECT" statement returns NULL. This is what i wrote so far:
strcat(msg, "Insert username:\n"); if(write(client, msg, 100) <= 0) { perror("Error"); exit(1); } if(read(client, name, 30) <= 0) { perror("Error"); exit(1); } sqlite3_stmt *res; char sql[100]; sprintf("SELECT * FROM users where name=\"%s\";", name); int rc = sqlite3_prepare_v2(db, sql, -1, &res, NULL); if(rc != SQLITE_OK) { perror("Error"); exit(1); } int step = sqlite3_step(res); if(step != SQLITE_DONE) { perror("Error"); exit(1); }最满意答案
引用文档 :
SQLITE_DONE表示语句已成功执行。 如果不先调用sqlite3_reset()将虚拟机重置为其初始状态,则不应再次在此虚拟机上调用sqlite3_step()。
如果正在执行的SQL语句返回任何数据,则每次新的数据行准备好由调用者处理时,都会返回SQLITE_ROW。 这些值可以使用列访问函数来访问。 再次调用sqlite3_step()来检索下一行数据。
我承认这不是很明显,但这对于空结果集实际上意味着sqlite3_step将直接返回SQLITE_DONE ,而不是SQLITE_ROW 。 我之前没有在C中使用过sqlite,但这就是我对文档的理解,但我认为我是对的,特别是在检查sqlite论坛之后
> Is my impression correct that when calling sqlite3_step() on a query > that returns no rows, the result will be [SQLITE_DONE]? Yes. > If that is the case, might that be added to the documentation? "SQLITE_DONE means that the statement has finished executing successfully." "If the SQL statement being executed returns any data, then SQLITE_ROW is returned each time a new row of data is ready for processing by the caller." The implication is that if SQL statement doesn't return any data, then sqlite3_step doesn't return SQLITE_ROW. Since it nevertheless finishes executing successfully, it returns SQLITE_DONE. -- Igor Tandetnik根据这些信息,你的代码的最后一部分对我来说似乎是错误的:
int step = sqlite3_step(res); if(step != SQLITE_DONE) { perror("Error"); exit(1); }如果step != SQLITE_DONE ,它可能是SQLITE_ROW ,这不是一个错误,它只是意味着查询返回了一个结果,您应该着手处理该数据。 如果您想将空结果集视为错误,那么您可以将其更改为:
int step = sqlite3_step(res); if(step != SQLITE_ROW) { if (step == SQLITE_DONE) perror("No results"); else perror("Error"); exit(1); }Quoting the documentation:
SQLITE_DONE means that the statement has finished executing successfully. sqlite3_step() should not be called again on this virtual machine without first calling sqlite3_reset() to reset the virtual machine back to its initial state.
If the SQL statement being executed returns any data, then SQLITE_ROW is returned each time a new row of data is ready for processing by the caller. The values may be accessed using the column access functions. sqlite3_step() is called again to retrieve the next row of data.
I will admit that this isn't very obvious, but what this actually means for empty result sets is that sqlite3_step will return SQLITE_DONE right off the bet, not SQLITE_ROW. I've not used sqlite in C before, but that's how I understood the docs, but I think I'm right, especially after checking the sqlite forum here
> Is my impression correct that when calling sqlite3_step() on a query > that returns no rows, the result will be [SQLITE_DONE]? Yes. > If that is the case, might that be added to the documentation? "SQLITE_DONE means that the statement has finished executing successfully." "If the SQL statement being executed returns any data, then SQLITE_ROW is returned each time a new row of data is ready for processing by the caller." The implication is that if SQL statement doesn't return any data, then sqlite3_step doesn't return SQLITE_ROW. Since it nevertheless finishes executing successfully, it returns SQLITE_DONE. -- Igor TandetnikBased on this info, the last bit of your code seems wrong to me:
int step = sqlite3_step(res); if(step != SQLITE_DONE) { perror("Error"); exit(1); }If step != SQLITE_DONE, it might be SQLITE_ROW, which isn't an error, it just means the query returned a result, and you should set about processing that data. If you want to treat empty result sets as an error, then you might change it to this:
int step = sqlite3_step(res); if(step != SQLITE_ROW) { if (step == SQLITE_DONE) perror("No results"); else perror("Error"); exit(1); }更多推荐
发布评论