这是数据库字段值。
以下是我用于从sqlite数据库中获取数据的代码
-(void)readDataFromRestaurantTable
{
[self openDataBase];
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, & database) == SQLITE_OK)
{
sqlite3_stmt *statement = NULL;
NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM RestaurantDB"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW)
{
strRestaurantName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
strRestaurantAddress = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
strRestaurantPhone = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
strRestaurantLatitude = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)];
strRestaurantLongitude = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)];
NSLog(@"strRestaurantName :--> %@",strRestaurantName);
}
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
}
在上一段代码中,应用程序没有进入
if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
}
这里有 statement = 0x00000000 &我认为由于这个原因,它只是没有进入if块。
崩溃时出错
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithUTF8String:]: NULL cString'
First throw call stack:
(0x18dc012 0x12a1e7e 0x18dbdeb 0xcbdce6 0x78e4 0x7bdf 0x2ca817 0x2ca882 0x2ebed9 0x2ebd14 0x2ea1ea 0x2ea06c 0x2ebc57 0x12b5705 0x1ec920 0x1ec8b8 0x40f0b4 0x12b5705 0x1ec920 0x1ec8b8 0x2ad671 0x2adbcf 0x2ad6a6 0x412bb9 0x12b5705 0x1ec920 0x1ec8b8 0x2ad671 0x2adbcf 0x2acd38 0x21c33f 0x21c552 0x1fa3aa 0x1ebcf8 0x1da5df9 0x185ff3f 0x185f96f 0x1882734 0x1881f44 0x1881e1b 0x1da47e3 0x1da4668 0x1e965c 0x2712 0x2645 0x1)
libc++abi.dylib: terminate called throwing an exception
请指导我实际问题在哪里。
答案 0 :(得分:8)
这是因为尝试使用NULL
字符串创建NSString对象。这是以下其中一条:
[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, ...)];
因此,在使用sql语句的结果创建NSString之前,需要检查NULL,如下所示:
char *tmp = sqlite3_column_text(statement, 1);
if (tmp == NULL)
strRestaurantName = nil;
else
strRestaurantName = [[NSString alloc] initWithUTF8String:tmp];
答案 1 :(得分:2)
你必须检查第一个数据是否为NULL之前弯曲它如下: -
const char* date = (const char*)sqlite3_column_text(statement, 3);
NSString *enddate = date == NULL ? nil : [[NSString alloc] initWithUTF8String:date];
在您的方法中实施:
-(void)readDataFromRestaurantTable {
[self openDataBase];
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "SELECT * FROM RestaurantDB";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
strRestaurantName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
strRestaurantAddress = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
strRestaurantPhone = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
strRestaurantLatitude = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)];
strRestaurantLongitude = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)];
}
}
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
可能会帮助你:)
答案 2 :(得分:1)
在sqlite3_finalize(statement);
条件中写if
:
if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
strRestaurantName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
strRestaurantAddress = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
strRestaurantPhone = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
strRestaurantLatitude = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)];
strRestaurantLongitude = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)];
NSLog(@"strRestaurantName :--> %@",strRestaurantName);
}
sqlite3_finalize(statement);
}
答案 3 :(得分:1)
我遇到了同样的错误 我们可以修改SQL查询以返回NOT NULL / BLANK值,而不是检查NULL或空值。
在下面的Query中,餐馆名称在数据库级别验证空白/空值。
注意:请原谅语法错误(如果有的话)。只考虑1列。
例如:
const char *sql = "SELECT * FROM RestaurantDB WHERE <restuarant_Name column> != '' AND <restuarant_Name column> IS NOT NULL";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
strRestaurantName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
}
}
答案 4 :(得分:1)
在我的情况下,当我跳过db表中的第一列&#39; regno&#39; 时出现同样的错误。但是,当我添加第一列标题&#39; regno&#39;并在方法中声明为NSString,清除了错误。
`-(NSArray*)loadData {
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK) {
NSString *querySQL = [NSString stringWithFormat:@"select regno, firstname, middlename, lastname, gender, dob, bloodgroup, mobileno, emailid from personalDetails"];
NSMutableArray *resultArray = [[NSMutableArray alloc]init];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK )
{
if (sqlite3_step(statement) == SQLITE_ROW) {
***NSString *regNo = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
[resultArray addObject:regNo];***
NSString *firstname = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 1)];
[resultArray addObject:firstname];
NSString *middlename = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 2)];
[resultArray addObject:middlename];
NSString *lastname = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 3)];
[resultArray addObject:lastname];
NSString *gender = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 4)];
[resultArray addObject:gender];
NSString *dob = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 5)];
[resultArray addObject:dob];
NSString *bloodgroup = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 6)];
[resultArray addObject:bloodgroup];
NSString *mobileno = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 7)];
[resultArray addObject:mobileno];
NSString *emailid = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 8)];
[resultArray addObject:emailid];
return resultArray;
}
else{
NSLog(@"Not found");
return nil;
}
sqlite3_reset(statement);
}
}
return nil;
}'