/* -(void)print
{
sqlite3_stmt *statement;
qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = '%d'",1];
if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
NSLog(@"hhhhhhhhh");
while (sqlite3_step(statement) == (SQLITE_ROW)){
NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
// tv.text=qsql;
}
}}*/
-(void)print
{
sqlite3_stmt *statement;
//qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = %d",1];
qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = %i",1];
//const char *sqlStatement = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
const char *qsql_stament = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
if(sqlite3_prepare_v2(database, qsql_stament, -1, &statement, NULL) == SQLITE_OK) {
NSLog(@"hhhhhhhhh");
while (sqlite3_step(statement) == (SQLITE_ROW)){
// NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
NSString *Q_NO = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%i",sqlite3_column_int(statement, 0)]];
// tv.text=qsql;
}
}
}
-(void) checkAndCreateDatabase{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success) return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
[fileManager release];
}
-(void) openDB {
databaseName = @"Lighthr.sqlite";
NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentsPaths objectAtIndex:0];
databasePath = [documentDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
if(sqlite3_open([databasePath UTF8String],&database) == SQLITE_OK){
NSLog(@"coming here???");
}
}
[1]: http://i.stack.imgur.com/lm1ZV.png
答案 0 :(得分:0)
因为rowid始终是整数,并且您尝试使用字符串进行访问 我必须尝试这个。
修改强>
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = %i",1];
const char *sqlStatement = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
if(sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK) {
NSLog(@"hhhhhhhhh");
while (sqlite3_step(statement) == (SQLITE_ROW)){
NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
// tv.text=qsql;
}
}
}
答案 1 :(得分:0)
你是否试图在没有“where”条款的情况下进行测试?
sqlite3_prepare_v2返回的错误代码是什么?
int sql_answer = sqlite3_prepare_v2(database, qsql_stament, -1, &statement, NULL);
NSLog(@" SQL answer : %d",sql_answer );
if(sql_answer == SQLITE_OK) {
...
答案 2 :(得分:0)
NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
以上stameny替换为以下声明
NSString *Q_NO = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%i",sqlite3_column_int(statement, 0)]];
和
const char *qsql_stament = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
现在用[qsql UTF8String]
替换qsql_stament
。它会正常工作。
答案 3 :(得分:0)
我认为你应该这样做。
- (无效)打印 {
sqlite3_stmt *声明; qsql = [NSString stringWithFormat:@“SELECT * from light where rowid =%d”,1];
const char *qsql_stament = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
if(sqlite3_prepare_v2(database, qsql_stament, -1, &statement, NULL) == SQLITE_OK) {
NSLog(@"hhhhhhhhh");
while (sqlite3_step(statement) == (SQLITE_ROW)){
NSString *Q_NO =[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]
}