下面是我的代码,我正在尝试从my.sqlite数据库中获取数据但是它没有执行我的if case。它总是进入其他条件。我做错了什么? 我无法执行我的if if if(sqlite3_prepare_v2(database,sqlQuerry,-1,& querryStatement,NULL)== SQLITE_OK)
sqlite3 *database;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"my.sqlite"];
const char* dbPath=[databasePath UTF8String];
if(sqlite3_open(dbPath, &database)==SQLITE_OK)
{
const char *sqlQuerry="SELECT * FROM Contacts;";
sqlite3_stmt *querryStatement;
if(sqlite3_prepare_v2(database, sqlQuerry, -1, &querryStatement, NULL)==SQLITE_OK)
{
NSLog(@"conversion successful....");
while (sqlite3_step(querryStatement)==SQLITE_ROW)
{
NSString *addressField = [[NSString alloc] initWithUTF8String:
(const char *)
sqlite3_column_text(querryStatement, 0)];
NSString *phoneField = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(querryStatement, 1)];
NSLog(@"DB ID :- %@",addressField);
NSLog(@"DB NAME :- %@",phoneField);
}
sqlite3_reset(querryStatement);
}
else {
NSLog(@"error while conversion....");
}
sqlite3_close(database);
}
/////////////////////////////////////////////// ////////////////////////////////// 我尝试使用FMDB并按照下面的方式执行: 但是我的* s和*结果对象获得了nil值而while语句没有被执行
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *path = [docsPath stringByAppendingPathComponent:@"my.sqlite"];
FMDatabase* database = [FMDatabase databaseWithPath:path];
[database open];
FMResultSet *s = [database executeQuery:@"SELECT COUNT(*) FROM Contacts"];
while ([s next])
{
int totalCount = [s intForColumnIndex:0];
NSLog(@"Count : %i", totalCount);
}
FMResultSet *results = [database executeQuery:@"select * from Contacts"];
while([结果下一个]) {
NSString *contact_id = [results stringForColumn:@"contact_id"];
NSString *name = [results stringForColumn:@"name"]; //
NSLog(@"User: %@ - %@",name, contact_id);
}
[database close];
答案 0 :(得分:1)
使用以下代码替换您的第一个六个行:
sqlite3 *database;
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"my.sqlite"];
NSLog(@"DATABASE PATH=%@",databasePath);
NSFileManager *fn=[NSFileManager defaultManager];
NSError *error;
BOOL success=[fn fileExistsAtPath:databasePath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"my.sqlite"];
success = [fn copyItemAtPath:defaultDBPath toPath:databasePath error:&error];
}
const char *dbPath=[databasePath UTF8String];
if(sqlite3_open(dbPath, &database)==SQLITE_OK)
{
//. . . . your code...//
}
答案 1 :(得分:0)
如果您遇到任何sqlite API问题,则需要使用sqlite3_errmsg()
(reference)报告错误,这样可以帮助您找出问题所在。
更改代码以报告错误并使用日志输出更新您的问题:
if (sqlite3_open(dbPath, &database) == SQLITE_OK)
{
...
}
else
{
NSLog("Failed to open database '%@': %s", databasePath, sqlite3_error(database));
}
根据sqlite3_open()
(here)的文档:
....否则返回错误代码。 sqlite3_errmsg()或 sqlite3_errmsg16()例程可用于获取英语 任何故障失败后的错误描述 sqlite3_open()例程。
答案 2 :(得分:0)
Table not found
。
实际问题是CoreData
更改了我的数据库的每个表名,其名称前面加了“z”,因此我的表名称Contacts以类似的方式更改为ZContacts和列名。当我在SQLite Manager中打开我的sqlite数据库时,我发现我错了。