SQLite更新查询

时间:2009-12-31 12:06:34

标签: sqlite

我是iPhone的新用户,遇到问题SQLite ...
我想执行一个包含多个where条件的查询,如:

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

这是进行更新的正确方法吗? 如果可能,执行查询的代码是什么?

2 个答案:

答案 0 :(得分:1)

取出iPhone方面,尝试在SQLite管理工具中运行/测试您的查询。

答案 1 :(得分: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 *IsSyncLog=@"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 =@"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...
}

}