iphone mysql数据库操作代码例子.docxVIP

  • 3
  • 0
  • 约3.41千字
  • 约 3页
  • 2021-11-14 发布于江苏
  • 举报
//database operation 打开数据库 -(bool) opendatabase{ nsarray*paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask,yes); nsstring *documentsdirectory = [pathsobjectatindex:0]; nsstring *path = [documentsdirectorystringbyappendingpathcomponent:@mydb.sql]; nsfilemanager*filemanager = [nsfilemanagerdefaultmanager]; bool find = [filemanagerfileexistsatpath:path]; //找到数据库文件 mydb.sql if (find) { nslog(@database file have already existed.); if(sqlite3_open([pathutf8string], amp;database_) !=sqlite_ok) { sqlite3_close(database_); nslog(@error: open database file.); return no; } return yes; } if(sqlite3_open([path utf8string], amp;database_) ==sqlite_ok) { //bfirstcreate_ = yes; [selfcreatechannelstable:datab在as后e_面];实//现函数 createchannelstable return yes; }else { sqlite3_close(database_); nslog(@error: open database file.); return no; } return no; } 创建表 - (bool) createchannelstable:(sqlite3*)db{ char*sql = create table reports (id integer primary key,stime text,stitle text,scal text,sru text); sqlite3_stmt *statement; if(sqlite3_prepare_v2(db, sql, -1, amp;statement,nil) !=sqlite_ok) { nslog(@error: failed to prepare statement:create reports table); return no; } int success =sqlite3_step(statement); sqlite3_finalize(statement); if ( success !=sqlite_done) { nslog(@error: failed to dehydrate:create table reports); return no; } nslog(@create table reports successed.); return yes; } 插入表 - (bool)insertonechannel:(nsstring*)msyttiimtele:(nsstring*)stmiytclael:(nsstring*)scal myruntime:(nsstring*)sruntime { sqlite3_stmt *statement; staticchar*sql = insert into reports (id,stime,stitle,scal,sruntime) values(null,?,?,?,?); //问号的个数要和(cid,title,imagedata,ima里ge面le字n)段的个数匹配,代表未知的值,将在下面将值和字段关 联。 int success =sqlite3_prepare_v2(database_, sql, -1, amp;statement,null); if (success !=sqlite_ok) { nslog(@error: failed to insert:channels); return no; } //这里的数字 1,2,3,4 代表第几个问号 //sqlite3_bind_text(statement, 1, stime, -1, sqlite_transient); char*p = [stime cstringusing

文档评论(0)

1亿VIP精品文档

相关文档