iOS SQLite教程

SQLite数据库引擎是一个软件库,提供类似于MySQL或Oracle的关系数据库管理系统。它是“服务器少”,“自包含”,具有“零配置”并使用“交易SQL”。这些功能使其成为移动设备的完美候选者。在iOS SQLite中,轻松嵌入应用程序,因为它只是一种文件格式。它取代了“Plist”实现,以存储和处理iOS中的复杂数据。本教程侧重于iOS SQLite Crud操作。

对于有趣的iOS教程,请参阅Udemy.com的此课程。

如何在ios中使用sqlite?

框架部分中的“libsqlite3.dylib”库必须在应用程序中添加,该库是一个C ++包装器,它为SQLite命令提供API。要使用SQLite命令,必须导入“SQLite3”标题文件。

iOS SQLite创建数据库和表

创建SQLite数据库类似于创建文本文件。在iOS中,文档目录的路径用于将数据库文件保存在应用程序中,因为只有IOS应用程序访问该文件夹以获取用户数据。

要在SQLite数据库中执行任何查询,请先打开数据库以查询,为此目的,“SQLite3_open”方法与数据库的路径和“SQLite3”对象作为参数一起使用。在成功执行时,它将常量的“SQLite_ok”返回为零。

最后,在数据库中创建一个表IOS SQLite API方法'SQLite3_exec'与打开的SQLite3对象,一个SQL查询的Create表,回调函数,回调函数的第一个参数和错误消息容器作为参数。在成功执行时,它返回“SQLite_ok”或在执行失败的情况下,SQL错误和丢失的数据库“SQLite_Error”等于'1'。

以下代码段显示使用所描述的API方法来创建“员工”表中的“员工”表,即SQLite数据库。

代码片段:

#import  @interface databasemanager:nsobject {nsstring * databasepath;} +(databasemanager *)getInstance;- (BOOL)创建;@end static databasemanager * sharedinstance = nil;静态Sqlite3 *数据库= nil;静态sqlite3_stmt *语句= nil;@implementation databasemanager +(databasemanager *)getInstance {if(!sharedInstance){sharedInstance = [[超级Allocwithzone:null] init];[SharedInstance createdB];返回SharedInstance;}  - (bool)createdb {nsstring * docsdir; NSArray *dirPaths; // Get the documents directory path dirPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); docsDir = dirPaths[0]; // Build the path to the database file databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent: @"StaffManagement.db"]]; BOOL isSuccess = YES; NSFileManager *filemgr = [NSFileManager defaultManager]; if ([filemgr fileExistsAtPath: databasePath ] == NO) { const char *dbpath = [databasePath UTF8String]; //Open the database connection if (sqlite3_open(dbpath, &database) == SQLITE_OK) { char *errMsg; const char *sql_stmt = "create table if not exists EMPLOYEE (employeeID integer primary key NOT NULL, employeeName text NOT NULL, employeeImage blob, employeePhoneNumber text NOT NULL);"; //Execute the query if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) { isSuccess = NO; } sqlite3_close(database); return isSuccess; } else { isSuccess = NO; } } return isSuccess; } @end

在上面的代码段中,使用“NSObject”的单身“DatabaseManager”类子类。在初始化时,它在“CreatedB”实例方法中创建一个“员工管理”和“员工”表,如果发生任何故障,则在成功返回True返回True。

有兴趣了解更多关于iOS编程的信息吗?在Udemy.com拍摄课程。

iOS SQLite插入,更新和删除

To insert and update data, an opened database is required and then an IOS SQLite API method ‘sqlite3_prepare_v2’ is used with opened SQLite object, SQL insert query or update query, length of query string in bytes and SQLite statement ‘sqlite3_stmt’ handle as arguments. On successful execution, ‘SQLITE_OK’ is returned and the SQLite statement points to the compiled prepared statement that is executed later using API method ‘sqlite3_step’ with statement pointer as an argument and returns ‘SQLITE_DONE’ (has value 101 shows ‘sqlite3_step’ is done executing) on success.

以下代码段使用iOS SQLite API方法演示在“员工”表中插入新员工数据。

代码片段(在'databasemanager'中添加方法):

- (bool)sailledepeeif(sqlite3_open(dbpath,&database)== sqlite_ok){nsstring *插入ql = [nsstring stringwithformat:@“插入员工(employeeid,employeename,employeeImage,employeeelephoneNumber)值(\”%d \“,\”%@ \“,?,?,\“%@ \”)“,[employeeid integervalue],employeename,employeephonenumber];const char *插入_stmt = [插入ql utf8string];if(sqlite3_prepare_v2(数据库,插入_stmt,-1,&state,null)!= sqlite_ok)返回否;否则{//将语句参数添加到查询。sqlite3_bind_blob(语句,1,[employeeImage字节],[employemage length],sqlite_transient);if(sqlite3_step(语句)== sqlite_done){sqlite3_reset(语句);返回是的;} else {sqlite3_reset(语句);否; } } sqlite3_close(database); } return NO;}

在上面的代码段中,使用插入查询调用“sqlite3_prepare_v2”,然后使用'sqlite3_bind_blob'ios sqlite api方法绑定employee的图像数据。此方法呈现指向语句的指针,SQL查询中的参数索引(最左边是1),要绑定到参数和SQLite析构函数类型的值。要使用“SQLite3_Step”方法执行语句。最后,调用具有准备好语句作为参数的“SQLite3_Reset”API方法,以将语句重置为初始状态。通过更改唯一查询字符串,可以使用相同的例子来更新和删除employee记录。在删除需要调用'sqlite3_exec'方法之前激活“在删除级联”中的选项。以下代码段用于启用该功能。

代码片段:

sqlite3_exec(数据库,“pragma fignder_keys =上;”,0,0,0);sqlite3_step(语句);sqlite3_reset(语句);

iOS SQLite选择

从SQLite表中选择数据还需要打开的数据库和使用SELECT查询的准备语句。然后调用'sqlite3_step'方法以执行该语句。如果从表中成功返回行,则返回'sqlite_row'常量。“sqlite3_column_count”方法返回当前行中的列数。最后,从“sqlite_column_x”方法中检索值,其中'x'表示返回的列数据类型,这是通过使用'sqlite3_column_type'方法来识别的。以下代码段用于从员工表中检索所有数据。

代码片段(在'databasemanager'中添加方法):

- (nsarray *)selectallemployee {const char * dbpath = [databasepath utf8string];if(sqlite3_open(dbpath,&database)== sqlite_ok){nsstring * querysql = [nsstring stringwithformat:@“从员工选择*”];const char * query_stmt = [querysql utf8string];nsmutableArray *结果array = [[nsmutablearray alloc] init];if(sqlite3_prepare_v2(数据库,query_stmt,-1,&stented,null)!= sqlite_ok){} else {if(sqlite3_step(语句)== sqlite_row){do {nsmutablearray * she = [nsmutablearray array];for(int i = 0; i 
    

In the above method of ‘DataBaseManager’ class, when a ‘sqlite3_step’ returns a ‘SQLITE_ROW’ constan, a specific method is called to retrieve the content of a column based on the type of column (‘SQLITE_TEXT, ‘SQLITE_BLOB’, ‘SQLITE_NULL’, etc.) returned by ‘sqlite3_column_type’ method. Finally, this retrieved data is saved in ‘NSMutableArray’ and is returned to a caller class.

iOS SQLite Xcode项目:

员工的控制器:

例1

Controller.M(插入按钮示例):例2
结果主视图:
例3

输出:

例4.

要了解有关iOS的要领,请在Udemy.com查看课程。

特色课程

使用SQLite开发Unity应用程序

最后更新于1970年1月

  • 2个小时
  • 20讲座
  • 中级水平
5. (1)

了解如何将数据保存到Unity App中的本地SQLite数据库|由Neeraj Jaiswal.

探索课程

SQLite学生还学习

赋予你的团队。引领行业。

通过Udemy为Business获取组织的在线课程和数字学习工具库的订阅。

请求演示