2009-12-31 94 views
1

我是新來的iPhone,並有一個問題,SQLite ...
我想執行一個包含多個where條件類似的查詢:SQLite的更新查詢

UPDATE tablename 
    SET field1 = ?, 
     field2=? 
WHERE Itemid=? 
    AND field1=? 
    AND field2=? 

這是正確的方式做更新? 如果可能的話執行查詢的代碼是什麼?

+0

路要走!!!!! – 2009-12-31 12:08:16

回答

1

取出iPhone方面並嘗試在SQLite管理工具中運行/測試您的查詢。

0

//如何選擇和更新SQLite數據庫

- (void)createEditableCopyOfDatabaseIfNeeded { 
// First, test for existence. 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"PDAirDB"]; 
dbPath=writableDBPath; 
dbSuccess = [fileManager fileExistsAtPath:writableDBPath]; 
if (dbSuccess) 
{ 
    return; 
} 
// The writable database does not exist, so copy the default to the appropriate location. 
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"PDAirDB"]; 
dbSuccess = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
if (!dbSuccess) { 
    NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
} 

} 


//////////////// IN GENRAL INFORMATION TABLE ///////////////////////////////////// 

-(void)insertGenralInfo :(int)ID :(NSString *)CUSTOMERNAME :(NSString *)LINENAME :( NSString *)SITENAME :(NSString *)REPORTEDBY:(NSString *)AFE:(NSString *)LINENUMBER:(NSString *)JOININSPECTED:(NSString *)REPORTINGDATE 
{ 
//ID=ID+1; 

// The database is stored in the application bundle. 

[self createEditableCopyOfDatabaseIfNeeded]; 
    NSString *[email protected]"NO"; 

    NSString *query= [NSString stringWithFormat: @"INSERT INTO GeneralInformation (PDAIRID,CustomerName, LineName,SiteName,ReportedBy,AFE,LineNumber,JointsInspected,ReportingDate,IsSyncLog) VALUES (\"%d\",\"%@\",\"%@\", \"%@\", \"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")", ID,CUSTOMERNAME,LINENAME,SITENAME,REPORTEDBY,AFE,LINENUMBER,JOININSPECTED,REPORTINGDATE,IsSyncLog]; 

// Open the database. The database was prepared outside the application. 

if ((sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) && (dbSuccess ==TRUE)) { 

    const char *sql = [query UTF8String]; 

    sqlite3_stmt *statement; 

    // Preparing a statement compiles the SQL query into a byte-code program in the SQLite library. 
    // The third parameter is either the length of the SQL string or -1 to read up to the first null terminator. 

    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) 
    { 
     // We "step" through the results - once for each row. 

     sqlite3_step(statement); 
    } 

    // "Finalize" the statement - releases the resources associated with the statement. 

    sqlite3_finalize(statement); 
} else 
{ 
    // Even though the open failed, call close to properly clean up resources. 
    sqlite3_close(database); 
    NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database)); 
    // Additional error handling, as appropriate... 
} 

} 
-(NSMutableArray *)selectGenralInformation:(int)projectid 
{ 

NSMutableArray *projectName = [[NSMutableArray alloc] init]; 

// The database is stored in the application bundle. 

[self createEditableCopyOfDatabaseIfNeeded]; 


NSString *query; 
query [email protected]"SELECT g.ID, p.Name AS ProjectName, g.CustomerName, g.LineName, g.SiteName, g.ReportedBy, g.AFE, g.LineNumber, g.JointsInspected, g.ReportingDate, g.IsSyncLog FROM GeneralInformation g INNER JOIN PDAIR ON (g.PDAIRID=PDAIR.ID) INNER JOIN ProjectSite ps ON (PDAIR.ProjectSiteID=ps.ID) INNER JOIN Project p ON (ps.ProjectID=p.ID)"; 

    // Open the database. The database was prepared outside the application. 

if ((sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) && (dbSuccess ==TRUE)) { 

    const char *sql = [query UTF8String]; 

    sqlite3_stmt *statement; 

    // Preparing a statement compiles the SQL query into a byte-code program in the SQLite library. 
    // The third parameter is either the length of the SQL string or -1 to read up to the first null terminator. 

    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) 
    { 
     // We "step" through the results - once for each row. 

     while (sqlite3_step(statement) == SQLITE_ROW) 
     { 

      for (int i=0; i<11; i++) 
      { 
       if([NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)] !=NULL) 
       { 
        [projectName addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)]]; 
       }else 
       { 
        [projectName addObject:@"NOT EXIST"]; 
       } 
       //NSLog(@"name %@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)]); 

      } 



     } 
    } 

    // "Finalize" the statement - releases the resources associated with the statement. 

    sqlite3_finalize(statement); 
} else { 
    // Even though the open failed, call close to properly clean up resources. 
    sqlite3_close(database); 
    NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database)); 
    // Additional error handling, as appropriate... 
} 

return projectName; 

} 

-(void)upDateGenralInfo :(int)ID :(NSString *)CUSTOMERNAME :(NSString *)LINENAME :(NSString *)SITENAME :(NSString *)REPORTEDBY:(NSString *)AFE:(NSString *)LINENUMBER:(NSString *)JOININSPECTED:(NSString *)REPORTINGDATE 
{ 


// The database is stored in the application bundle. 

[self createEditableCopyOfDatabaseIfNeeded]; 

// NSString *query= [NSString stringWithFormat: @"INSERT INTO GeneralInformation (ProjectID,CustomerName, LineName,SiteName,ReportedBy,AFE,LineNumber,JointsInspected,ReportingDate) VALUES (\"%d\",\"%@\",\"%@\", \"%@\", \"%@\",\"%@\",\"%@\",\"%@\",\"%@\")", ID,CUSTOMERNAME,LINENAME,SITENAME,REPORTEDBY,AFE,LINENUMBER,JOININSPECTED,REPORTINGDATE]; 

// Open the database. The database was prepared outside the application. 
NSString *query= @"UPDATE GeneralInformation SET "; 
query=[query stringByAppendingString:[NSString stringWithFormat:@"CustomerName='%@',LineName='%@',SiteName='%@',ReportedBy='%@',AFE='%@',LineNumber='%@',JointsInspected='%@',ReportingDate='%@' WHERE ID=%d",CUSTOMERNAME,LINENAME,SITENAME,REPORTEDBY,AFE,LINENUMBER,JOININSPECTED,REPORTINGDATE,ID]]; 

if ((sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) && (dbSuccess ==TRUE)) { 

    const char *sql = [query UTF8String]; 

    sqlite3_stmt *statement; 

    // Preparing a statement compiles the SQL query into a byte-code program in the SQLite library. 
    // The third parameter is either the length of the SQL string or -1 to read up to the first null terminator. 

    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) 
    { 
     // We "step" through the results - once for each row. 

     sqlite3_step(statement); 
    } 

    // "Finalize" the statement - releases the resources associated with the statement. 

    sqlite3_finalize(statement); 
} else 
{ 
    // Even though the open failed, call close to properly clean up resources. 
    sqlite3_close(database); 
    NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database)); 
    // Additional error handling, as appropriate... 
} 

}