我正在尝试开发一个可以与数据库一起使用的登录程序.我使用了一个表emplopyee,列"Userid","Passwd"和"Name"(这三个都是'VARCHAR'类型).在我的程序中,我添加了两个文本字段"username"和"password".我得到了"用户名"和"密码"值,并将它们存储在两个全局对象中(类型为"NSString").
现在我想选择与给定的"Userid"和"Passwd"匹配的"Name".我出于上述原因使用的编码部分在这里:
sqlite3 *dB;
sqlite3_stmt *compiledStatement;
if(sqlite3_open([databasePath UTF8String], &dB)==SQLITE_OK){
const char *sql="Select Name from employee Where Userid=? , Passwd=?";
if(sqlite3_prepare_v2(dB, sql, -1, &compiledStatement, NULL)!=SQLITE_OK)
NSAssert1(0,@"Error while reading Employee Name -->%s',sqlite3_errmsg(dB));
}
sqlite3_bind_text(compiledStatement,1,[txt UTF8String],-1,SQLITE_TRANSIENT);
/*txt and txt1 are the global objects i told about they have the values of Userid and Passwd from the texxt fields respectively*/
sqlite3_bind_text(compiledStatement,2,[txt1 UTF8String],-1,SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(compiledStatement))
txt3 =[[NSString alloc] initWithUTF8String:(char*)sqlite3_column_text(compiledStatement,0)];
else
NSAssert1(0, @"Error while getting the username '%s'", sqlite3_errmsg(dB));
sqlite3_reset(compiledStatement);
Run Code Online (Sandbox Code Playgroud)
它在编译时没有错误,但是应用程序不起作用,它在查询中显示了一些错误(在运行时).如果您无法弄清楚给出的数据,我将为您提供整个代码.
我在这里添加了错误详细信息:
如果我在select查询中使用","(逗号),则会出现此错误:
***由于未捕获的异常
'NSInternalInconsistencyException' 而终止应用程序,原因:'读取员工姓名时出错 - >'接近',":语法错误''
当我在select查询中使用"AND"时会发生这种情况:
***由于未捕获的异常
'NSInternalInconsistencyException' 而终止应用程序,原因:'获取用户名时出错'不是错误''
首要问题:
在SQL WHERE子句中,如果您的条件都需要为true,则使用AND而不是逗号来组合它们:
SELECT Name FROM employee WHERE Userid=? AND Passwd=?
Run Code Online (Sandbox Code Playgroud)
如果不是这样,建议您在问题中编辑运行时错误消息详细信息.
供参考,请参阅SQLite理解的SQL.
第二期:
看起来你错误地只将函数sqlite3_step()中的返回码SQLITE_DONE视为'成功' - 消息'不是错误'是线索.也许返回的值可能是SQLITE_ROW - 表示查询成功,并且您有一些数据要读取?
| 归档时间: |
|
| 查看次数: |
1548 次 |
| 最近记录: |