首页 \ 问答 \ 如何通过iphone编程插入多行数据到sqlite数据库(How to insert multiple row of data to sqlite database by iphone programming)

如何通过iphone编程插入多行数据到sqlite数据库(How to insert multiple row of data to sqlite database by iphone programming)

我是iphone开发的新手。我想插入多个值到我的sqlite3数据库并在tableview中显示内容。我能够将单行数据插入到我的数据库并检索并显示数据,但我无法与插入多行数据有关。这里是我的代码...

-(void)initializeTableData{
sqlite3 *db=[DatabaseTestAppDelegate getNewDBConnection];
sqlite3_stmt *statement=nil;
sqlite3_stmt *statement1=nil;

if (insert_MyObj_statement == nil)
{
    const char *sql2="DELETE FROM user";
    sqlite3_prepare_v2(db, sql2, -1, &statement1, NULL);
    sqlite3_step(statement1);
    //const char *sql1 = "INSERT INTO user (id,name) VALUES ('0','ash'),('3','karna'),('2','kumar'),('5','siva')";
const char *sql1 = "INSERT INTO user (id,name) VALUES ('0','xxx')";
 int result=sqlite3_prepare_v2(db, sql1, -1, &insert_MyObj_statement, NULL);
    NSAssert1(result == SQLITE_OK, @"addMyObjectIntoDatabase: failed to prepare statement with err '%s'", sqlite3_errmsg(db));
}
 sqlite3_step(insert_MyObj_statement);

const char *sql="select * from user";
if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL)!=SQLITE_OK)
    NSAssert1(0,@"error in preparing staement",sqlite3_errmsg(db));
else{
    while(sqlite3_step(statement)==SQLITE_ROW)
        [tableData addObject:[NSString stringWithFormat:@"%s",(char*)sqlite3_column_text(statement,1)]];
}
    sqlite3_finalize(statement);


}

有没有其他方法可以将多行数据插入到我的表中,请帮助我。谢谢。


I am new to iphone development.I want to insert multiple values into my sqlite3 database and display the content in the tableview.I am able to insert single row of data in to my database and retrieve it and display the data but i am not able to do with inserting multiple row of data.Here is my code...

-(void)initializeTableData
{
    sqlite3 *db=[DatabaseTestAppDelegate getNewDBConnection];
    sqlite3_stmt *statement=nil;
    sqlite3_stmt *statement1=nil;

    if (insert_MyObj_statement == nil)
    {
        const char sql2[] = "DELETE FROM user";
        sqlite3_prepare_v2(db, sql2, -1, &statement1, NULL);
        sqlite3_step(statement1);
        const char sql1[] = "INSERT INTO user (id,name) VALUES ('0','xxx')";
        int result=sqlite3_prepare_v2(db, sql1, -1, &insert_MyObj_statement, NULL);
    }
    sqlite3_step(insert_MyObj_statement);

    const char sql[] = "select * from user";
    if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL)!=SQLITE_OK)
    {
        NSAssert1(0,@"error in preparing staement",sqlite3_errmsg(db));
    }
    else
    {
        while(sqlite3_step(statement)==SQLITE_ROW)
            [tableData addObject:[NSString stringWithFormat:@"%s",(char*)sqlite3_column_text(statement,1)]];
    }
    sqlite3_finalize(statement);
}

Is there any other way to insert multiple row of data in to my table .Please help me out.Thanks.


原文:https://stackoverflow.com/questions/2200828
更新时间:2024-03-05 07:03

最满意答案

为什么这是输出?

tbl[0]->name =

这是因为

DF_XML_Table xmlTbl;

超出范围,并将在每次循环迭代中被销毁。 您将留下一个悬挂指针,稍后访问它为未定义行为


您应该选择一个合适的动态内存管理智能指针来存储在std::vector<> ,而不是使用原始指针:

vector<std::unique_ptr<DF_XML_Table>> tblVec;
for (int i = 1; i <= 5; i++) {
    DF_XML_Table* xmlTbl = ;
    tblVec.push_back(std::unique_ptr<DF_XML_Table>(new DF_XML_Table()));
    tblVec.back()->name = "Name";
}
cout << "tbl[0]->name = " << tblVec[0]->name << endl;

上面的代码将确保动态分配的对象的所有权被转移到包含的vector<std::unique_ptr<DF_XML_Table>> ,并且被正确处理以减少它们的寿命,而不是早于tblVec超出范围。


Why is this the output?

tbl[0]->name =

It's because

DF_XML_Table xmlTbl;

goes out of scope and will be destroyed on each of the loops iterations. You'll have a dangling pointer left, and accessing it later is Undefined Behaviour.


Instead of using raw pointers, you should choose an appropriate dynamic memory management smart pointer to be stored in the std::vector<>:

vector<std::unique_ptr<DF_XML_Table>> tblVec;
for (int i = 1; i <= 5; i++) {
    DF_XML_Table* xmlTbl = ;
    tblVec.push_back(std::unique_ptr<DF_XML_Table>(new DF_XML_Table()));
    tblVec.back()->name = "Name";
}
cout << "tbl[0]->name = " << tblVec[0]->name << endl;

The code above will ensure that ownership for the dynamically allocated object is transferred to the containing vector<std::unique_ptr<DF_XML_Table>>, and be properly handled to decease their life time no earlier, than tblVec goes out of scope.

相关问答

更多
  • Formtastic为Rails代码添加了额外的功能,但并没有消除现有的功能,因此以下内容应该适用于您: rd.object.review_criteria_id 'object'可以用于普通的Rails窗体助手来访问底层的绑定对象,而Formtastic则可以遵循这个约定。 Formtastic adds additional features to the Rails code, but doesn't take away existing functionality so the following ...
  • 好。 我终于解决了这个问题 如果Excel包含可能不同类的数据,似乎Excel无法将整个列识别为相同的数据类型。 即使您强制将单元格格式设置为工作簿上的文本,也会发生这种情况,因为当您查询数据时,它会根据收到的第一条记录将字段识别为确定类型; 这就是为什么不同的文件清空不同类型的记录的原因,文件以纯文本清空数字值,反之亦然。 我找到了一个解决方案,只需将连接字符串更改为Excel。 这是我原来的连接字符串 Provider=Microsoft.Jet.OLEDB.4.0;Data Source=pathTo ...
  • 由于它处于while块中,因此它将始终连续运行(因为始终满足条件)。 但你不能修改任何东西,因为一旦你确认消息,我会再次弹出。 请尝试使用'if'代替: add.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ if (title1.getText().length() == 0 || firstname1.getText().length() == 0 || fi ...
  • C ++ 03标准,附件B(实施量): 由于计算机是有限的,所以C ++实现必然受限于他们可以成功处理的程序的大小。 每个实施应记录已知的限制。 本文档可能存在固定限制,说明如何根据可用资源计算可变限制,或者说固定限制不存在或不明确。 限制可能会限制数量,包括下面描述的或其他数量。 建议将每个数量后面的括号中的数字作为该数量的最小值。 但是,这些数量只是指导方针,并不能确定是否符合要求。 该清单包括 物体的大小[262 144]。 数据成员在同一个班级,结构或联盟中[16 384]。 成员在一个班级申报[4 ...
  • 使用过滤 ,很容易: var anyFieldIsEmpty = $("form :input").filter(function() { return $.trim(this.value).length === 0; }).length > 0; if (anyFieldIsEmpty) { // empty fields } 演示: http : //jsfiddle.net/Lz9nY/ Use filtering, it is easy: var anyField ...
  • 首先,使用LinkedList而不仅仅是原始的LinkedList ; 编译器应该抱怨现有的代码。 至于“迭代字段”,这实际上没有意义。 只需遍历列表中的Obj对象,并使用elementf.n和elementf.c在for循环中执行某些elementf.c 。 First, use LinkedList and not just a raw LinkedList; the compiler should be complaining about the existing code. As ...
  • 在linux上有strsep 。 strsep()函数是作为strtok()的替代而引入的,因为后者无法处理空字段。 但是,strtok()符合C89 / C99,因此更便于携带。 On linux there's strsep. The strsep() function was introduced as a replacement for strtok(), since the latter cannot handle empty fields. However, strtok() conforms ...
  • 为什么这是输出? tbl[0]->name = 这是因为 DF_XML_Table xmlTbl; 超出范围,并将在每次循环迭代中被销毁。 您将留下一个悬挂指针,稍后访问它为未定义行为 。 您应该选择一个合适的动态内存管理智能指针来存储在std::vector<> ,而不是使用原始指针: vector> tblVec; for (int i = 1; i <= 5; i++) { DF_XML_Table* xmlTbl = ; ...
  • 我想你错过了代码中的一些逻辑。 此外,您应该尝试避免以这种方式使用try-catch块,并且应该尝试为控件提供有意义的名称,这将使您的代码更具可读性。 尝试这样做: // textBox4 should be txtUserName for example, it's more intuitive var q = (from c in Session.DB.Guests where c.UserName == textBox4.Text select c).SingleO ...
  • 您可以使用ko.mapping插件: var results = ko.mapping.fromJS(second, first); 见文档 You can use the ko.mapping plugin: var results = ko.mapping.fromJS(second, first); See Documentation

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)