我有一个包含SQLite数据库的应用程序。我创建了表并将值插入到表中,但是当我从表中获取数据时,它会给出错误
near "Dhanani": syntax error
这是我的获取数据方法:
-(void)getTextFromDb
{
NSLog(@"Method Called");
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
_databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
@"QpinData.db"]];
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_myDatabase) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"SELECT QpinName,Address,Lat,Long,Type FROM QPINTABLE WHERE UserName=Ramesh Dhanani"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_myDatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
[self.qpinname removeAllObjects];
[self.qpinType removeAllObjects];
[self.address removeAllObjects];
[self.latitude removeAllObjects];
[self.longitude removeAllObjects];
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *QpinName = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 0)];
NSString *Address = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 1)];
NSString *latitude = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 2)];
NSString *longitude = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 3)];
NSString *Type = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 4)];
[self.qpinname addObject:[NSString stringWithFormat:@" %@ ", QpinName]];
[self.address addObject:[NSString stringWithFormat:@"%@ ",Address]];
NSLog(@"self.address %@",self.address);
[self.latitude addObject:[NSString stringWithFormat:@"%@",latitude]];
[self.latitude addObject:[NSString stringWithFormat:@" %@",longitude]];
[self.qpinType addObject:[NSString stringWithFormat:@" %@",Type]];
}
NSLog(@"%s",sqlite3_errmsg(_myDatabase));
sqlite3_finalize(statement);
}
NSLog(@"%s",sqlite3_errmsg(_myDatabase));
sqlite3_close(_myDatabase);
[self.personalTable reloadData];
}
}
任何帮助都将不胜感激。
答案 0 :(得分:1)
您的SQL中有一个未引用的值。所以你要么:
在用户名值周围加上引号:
NSString *querySQL = @"SELECT QpinName,Address,Lat,Long,Type FROM QPINTABLE WHERE UserName='Ramesh Dhanani'";
如果用户名可以更改,则需要在SQL中使用?
占位符,然后使用sqlite3_bind_text
绑定值:
-(void)getTextFromDb
{
NSString *docsDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *path = [docsDir stringByAppendingPathComponent:@"QpinData.db"];
const char *dbpath = [path UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_myDatabase) == SQLITE_OK) {
const char *query_stmt = "SELECT QpinName,Address,Lat,Long,Type FROM QPINTABLE WHERE UserName=?";
if (sqlite3_prepare_v2(_myDatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
if (sqlite3_bind_text(statement, 1, [self.username UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK) {
NSLog(@"problem binding value: %s", sqlite3_errmsg(_myDatabase));
}
// clear out the model objects
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *qpinName = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 0)];
NSString *address = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 1)];
NSString *latitude = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 2)];
NSString *longitude = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 3)];
NSString *type = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 4)];
// populate the model objects
}
sqlite3_finalize(statement);
}
sqlite3_close(_myDatabase);
[self.personalTable reloadData];
}
}
答案 1 :(得分:0)
你必须使用,
NSString *querySQL = [NSString stringWithFormat:@"SELECT QpinName,Address,Lat,Long,Type FROM QPINTABLE WHERE UserName='Ramesh Dhanani'"];
因为查询中的任何字符串都必须是单引号。