插入失败:数据类型不匹配

时间:2014-02-14 09:45:04

标签: ios objective-c database xml-parsing sqlite

当我在NSLog中显示我的数据时,它显示正常。但是,当我解析数据&将它插入数组中,该数组显示错误的有序数据。虽然我正在等待我在 HNM_Id 中获得空值。所以我无法插入。问题在哪里?

这是我正在使用的代码:

-(void)parseResponse:(NSData*)data{
   NSString *xmlStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"parseResponse Xml string %@",xmlStr);

    [sqliteManager deleteLoginDetails];

    TBXMLEx *xml = [TBXMLEx parserWithXML:xmlStr];


      if (xml.rootElement) {


        TBXMLElementEx *returnNode = xml.rootElement;

        TBXMLElementEx *fileNode = [returnNode child:@"AllHospitalNetwork"];

        while ([fileNode next]) {
            NSMutableArray* allHospitalNetwork = [NSMutableArray new];

            TBXMLElementEx *hospitalNetworkNode = [fileNode child:@"HospitalNetwork"];

            while ([hospitalNetworkNode next]) {
                NSMutableDictionary* hospitalNetwork = [NSMutableDictionary new];

                TBXMLElementEx *HNM_IdNode = [hospitalNetworkNode child:@"HNM_ID"];

                NSLog(@"HNM is %@",HNM_IdNode);

                if (HNM_IdNode) {
                    [hospitalNetwork setObject:HNM_IdNode.value forKey:@"HNM_ID"];

                    NSLog(@"HNM1 is %@",HNM_IdNode);

                    NSLog(@"HNM2 is %@",HNM_IdNode.value);
                }

                TBXMLElementEx *HNM_Network_NameNode = [hospitalNetworkNode child:@"HNM_Network_Name"];

                if (HNM_Network_NameNode) {
                    [hospitalNetwork setObject:[NSString stringWithFormat:@"%@",HNM_Network_NameNode.value] forKey:@"HNM_Network_Name"];
                }

                TBXMLElementEx *HNM_StatusNode = [hospitalNetworkNode child:@"HNM_Status"];

                if (HNM_StatusNode) {
                    [hospitalNetwork setObject:HNM_StatusNode.value forKey:@"HNM_Status"];
                }

                TBXMLElementEx *HNM_Created_DateNode = [hospitalNetworkNode child:@"HNM_Created_Date"];

                if (HNM_Created_DateNode) {
                    [hospitalNetwork setObject:HNM_Created_DateNode.value forKey:@"HNM_Created_Date"];
                }

                TBXMLElementEx *HNM_Created_ByNode = [hospitalNetworkNode child:@"HNM_Created_By"];

                if (HNM_Created_ByNode) {
                    [hospitalNetwork setObject:HNM_Created_ByNode.value forKey:@"HNM_Created_By"];
                }

                TBXMLElementEx *HNM_Modified_DateNode = [hospitalNetworkNode child:@"HNM_Modified_Date"];

                if (HNM_Created_ByNode) {
                    [hospitalNetwork setObject:HNM_Modified_DateNode.value forKey:@"HNM_Modified_Date"];
                }

                TBXMLElementEx *HNM_Modified_ByNode = [hospitalNetworkNode child:@"HNM_Modified_By"];

                if (HNM_Modified_ByNode) {
                    [hospitalNetwork setObject:HNM_Modified_ByNode.value forKey:@"HNM_Modified_By"];
                }

                [allHospitalNetwork addObject:hospitalNetwork];


                NSLog(@"allHospitalNetwork insert is %@ ",allHospitalNetwork);

            } 
                [sqliteManager insertHospitalNetwork:allHospitalNetwork];


        }

parseResponse Xml字符串NSLog:

<?xml version="1.0"?>
<AllLoginDetails>
  <AllHospitalNetwork>
    <HospitalNetwork>
      <HNM_ID>1</HNM_ID>
      <HNM_Network_Name>Health Care</HNM_Network_Name>
      <HNM_Status>A</HNM_Status>
      <HNM_Created_Date>24/06/2013 17:46:58</HNM_Created_Date>
      <HNM_Created_By>1</HNM_Created_By>
      <HNM_Modified_Date />
      <HNM_Modified_By />
    </HospitalNetwork>
    </AllHospitalNetwork>

但数组值顺序已更改:

allHospitalNetwork insert is (
        {
        "HNM_Created_By" = 1;
        "HNM_Created_Date" = "24/06/2013 17:46:58";
        "HNM_ID" = 1;
        "HNM_Modified_By" = "";
        "HNM_Modified_Date" = "";
        "HNM_Network_Name" = "Health Care";
        "HNM_Status" = A;
    }

插入:

-(void)insertHospitals:(NSMutableArray*)hospitals{

 NSString* sqlStr = @"insert into Hospital_Master(\"HM_ID\",\"HM_HNM_ID\",\"HM_Hospital_Name\",\"HM_Status\",\"HM_Created_Date\",\"HM_Created_By\",\"HM_Modified_Date\",\"HM_Modified_By\")";


    for (NSDictionary* feedDict in hospitals) {
        if(i > 0){
            sqlStr = [sqlStr stringByAppendingString:@" UNION "];


        }


     sqlStr = [sqlStr stringByAppendingString:[NSString stringWithFormat:@" SELECT \"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\"",[feedDict objectForKey:@"HM_Id"] ,[feedDict objectForKey:@"HM_HNM_Id"],[feedDict objectForKey:@"HM_Hospital_Name"], [feedDict objectForKey:@"HM_Status"], [feedDict objectForKey:@"HM_Created_Date"],[feedDict objectForKey:@"HM_Created_By"],[feedDict objectForKey:@"HM_Modified_Date"],[feedDict objectForKey:@"HM_Modified_By"]]];


        i++;

    }

    char * sql =sqlite3_mprintf("%q",[sqlStr UTF8String]);

 sqlite3_exec(database, sql, 0, 0, 0);

    NSLog(@"Insert failed: %s", sqlite3_errmsg(database));

    sqlite3_free(sql);
}

插入失败:数据类型不匹配

2 个答案:

答案 0 :(得分:0)

你正在使用很多艰难的方法来完成这个插入任务,实际上我无法完全了解你在做什么,但我提供了一个以下模板用于插入目的..

这里在您的代码中,您没有准备查询语句,您正在直接执行查询,因此首先您应该正确地准备语句,然后您可以执行它...

这是模板,

sqlite3_stmt *statement;
NSString *insertSQL = [NSString stringWithFormat:@"insert into table_name (col_name_1, col_name_2, col_name_3) values(\"%@\",\"%d\",\"%d\")",variable_vale_1, variable_vale_2, variable_vale_3];
// Here i m assuming that first value is NSString type and other are integer type
    const char *insert_stmt = [insertSQL UTF8String];
    sqlite3_prepare_v2(database_object, insert_stmt,-1, &statement, NULL);
    if (sqlite3_step(statement) != SQLITE_DONE)
    {
        NSLog(@"Statement is Failed.....");
    }
sqlite3_finalize(statement);

不要忘记最终确定您的陈述,并在提供实体的数据类型时要小心。

只需使用此模板安排您的代码,我希望它对您有用...

答案 1 :(得分:-1)

我不知道这是哪种语言,

但是,请尝试使用以下代码,在那里创建SQL查询。

sqlStr stringByAppendingString:[NSString stringWithFormat:@" SELECT '%@','%@','%@','%@','%@','%@','%@','%@'",[feedDict objectForKey:@"HM_Id"] ,......