SQLite更新查询

SQLite更新查询,sqlite,Sqlite,我是新来的,对… 我想执行一个包含多个的查询,其中条件如下: UPDATE tablename SET field1 = ?, field2=? WHERE Itemid=? AND field1=? AND field2=? 这是进行更新的正确方法吗? 如果可能,执行查询的代码是什么?取出iPhone方面,尝试在SQLite管理工具中运行/测试您的查询。取出iPhone方面,尝试在SQLite管理工具中运行/测试您的查询。//如何选择和更新SQLi

我是新来的,对…
我想执行一个包含多个
的查询,其中
条件如下:

UPDATE tablename 
   SET field1 = ?, 
       field2=? 
 WHERE Itemid=? 
   AND field1=? 
   AND field2=?
这是进行更新的正确方法吗?
如果可能,执行查询的代码是什么?

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

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

//如何选择和更新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...
}

}
-(void)CreateEditableCopyOfDatabaseIf需要{
//首先,测试是否存在。
NSFileManager*fileManager=[NSFileManager defaultManager];
n错误*错误;
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*writableDBPath=[DocumentsDirectoryStringByAppendingPathComponent:@“PDAirDB”];
dbPath=writeabledbpath;
dbSuccess=[fileManager fileExistsAtPath:writeabledbpath];
if(dbSuccess)
{
返回;
}
//可写数据库不存在,因此请将默认数据库复制到适当的位置。
NSString*defaultDBPath=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“PDAirDB”];
dbSuccess=[fileManager copyItemAtPath:defaultDBPath-toPath:writeabledbpath-error:&error];
if(!dbSuccess){
NSAssert1(0,@“无法创建带有消息“@.”,[错误本地化描述])的可写数据库文件;
}
}
////////////////在一般信息表中/////////////////////////////////////
-(void)insertGenralInfo:(int)ID:(NSString*)CUSTOMERNAME:(NSString*)LINENAME:(NSString*)SITENAME:(NSString*)REPORTEDBY:(NSString*)AFE:(NSString*)LINENUMBER:(NSString*)JOININSPECTED:(NSString*)REPORTINGDATE
{
//ID=ID+1;
//数据库存储在应用程序包中。
[根据需要自行创建可编辑的数据库副本];
NSString*IsSyncLog=@“否”;
NSString*query=[NSString stringWithFormat:@“插入到通用信息(PDAIRID、客户名称、行名称、站点名称、报告人、AFE、行号、检查的连接、报告日期、IsSyncLog)值(\%d\”、\“%@\”、\“%@\”、\“%@\”、“%@\”、“%@\”、“%@\”、“%@\”、“%@\”、“%\”、“%\”、“%\”),ID,CUSTOMERNAME,LINENAME,SITENAME,REPORTEDBY,AFE,LINENUMBER,JOININSPECTED,REPORTINGDATE,IsSyncLog];
//打开数据库。数据库是在应用程序外部准备的。
if((sqlite3_open([dbPath UTF8String],&database)==SQLITE_OK)&&(dbSuccess==TRUE)){
const char*sql=[query UTF8String];
sqlite3_stmt*语句;
//准备语句将SQL查询编译为SQLite库中的字节码程序。
//第三个参数是SQL字符串的长度,或者是读取到第一个空终止符的-1。
if(sqlite3\u prepare\u v2(数据库,sql,-1,&语句,NULL)==SQLITE\u OK)
{
//我们“一步一步”浏览结果——每行一次。
sqlite3_步骤(语句);
}
//“Finalize”语句-释放与该语句关联的资源。
sqlite3_最终确定(声明);
}否则
{
//即使打开失败,也可以调用close来正确清理资源。
sqlite3_关闭(数据库);
NSAssert1(0,@“无法打开带有消息“%s”的数据库”,sqlite3_errmsg(数据库));
//其他错误处理,视情况而定。。。
}
}
-(NSMutableArray*)选择GenralInformation:(int)projectid
{
NSMutableArray*projectName=[[NSMutableArray alloc]init];
//数据库存储在应用程序包中。
[根据需要自行创建可编辑的数据库副本];
NSString*查询;
query=@“选择g.ID,p.Name作为项目名称,g.CustomerName,g.LineName,g.SiteName,g.ReportedBy,g.AFE,g.LineNumber,g.JointsInspected,g.ReportingDate,g.IsSyncLog从通用信息g内部连接PDAIR ON(g.PDAIR ID=PDAIR.ID)内部连接项目站点ps ON(PDAIR.ProjectSiteID=ps.ID)内部连接项目p ON(ps.ProjectID)”;
//打开数据库。数据库是在应用程序外部准备的。
if((sqlite3_open([dbPath UTF8String],&database)==SQLITE_OK)&&(dbSuccess==TRUE)){
const char*sql=[query UTF8String];
sqlite3_stmt*语句;
//准备语句将SQL查询编译为SQLite库中的字节码程序。
//第三个参数是SQL字符串的长度,或者是读取到第一个空终止符的-1。
if(sqlite3\u prepare\u v2(数据库,sql,-1,&语句,NULL)==SQLITE\u OK)
{
//我们“一步一步”浏览结果——每行一次。
while(sqlite3\u步骤(语句)=SQLITE\u行)
{

for(int i=0;i//如何选择和更新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...
}

}
-(void)CreateEditableCopyOfDatabaseIf需要{
//首先,测试是否存在。
NSFileManager*fileManager=[NSFileManager defaultManager];
n错误*错误;
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*writableDBPath=[DocumentsDirectoryStringByAppendingPathComponent:@“PDAirDB”];
dbPath=writeabledbpath;
dbSuccess=[fileManager fileExistsAtPath:writeabledbpath];
if(dbSuccess)
{
返回;
}
//可写数据库不存在,因此请将默认数据库复制到适当的位置。
NSString*defaultDBPath=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“PDAirDB”];
dbSuccess=[fileManager copyItemAtPath:defaultDBPath-toPath:writeabledbpath-error:&error];
if(!dbSuccess){
NSAssert1(0,@“无法创建带有消息“@.”,[错误本地化描述])的可写数据库文件;
}
}
////////////////在一般信息表中/////////////////////////////////////
-(void)insertGenralInfo:(int)ID:(NSString*)CUSTOMERNAME:(NSString*)LINENAME:(NSString*)SITENAME:(NSString*)REPORTEDBY:(NSString*)AFE:(NSString*)LINENUMBER:(NSString*)JOININSPECTED:(NSString*)REPORTINGDATE
{
//ID=ID+1;
//数据库存储在应用程序包中。
[根据需要自行创建可编辑的数据库副本];
NSStr