微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlite3_iphone

1. 预备知识

读者需要有一定的sqlite数据库知识,懂得基本的sql语句,掌握OSX系统terminal终端 以及 XCode编译器 的基本操作。

2. 创建数据库

打开 Applications-Utilities-Terminal,可见终端窗口。

假设当前系统用户为Smitty,可在终端窗口内键入如下命令:

cd /Users/Smitty/Documents

mkdir sqliteDemo

cd sqliteDemo

sqlite3 PersonDatabase.sql

至此,你已经在/Users/lookaflyingdonkey/Documents目录下创建了一个新的目录sqliteDemo,并使用 sqlite3命令开启了sqlite终端,准备建立数据库结构并输入数据。

举例来说,若我们要建立的数据库表person需要有如下结构:唯一的id值,名字,职位描述,头像图片地址,那么下列命令将创建这个表,并添加几个人的纪录。

CREATE TABLE person (id INTEGER PRIMARY KEY,name VARCHAR(50),title TEXT,icon VARCHAR(255) );

INSERT INTO person (name,title,icon) VALUES('Peter','handy man','http://dblog.com.au/wp-content/elephant.jpg');

INSERT INTO person (name,icon) VALUES('Susan','manager','http://dblog.com.au/wp-content/monkey.jpg');

INSERT INTO person (name,icon) VALUES('Wilson','developer','http://dblog.com.au/wp-content/kangaroo.jpg');

执行完这几步后,可运行命令"SELECT * FROM person;",检查数据库中是否已存在上面插入的数据。一旦确认一切正常,可用命令".quit"退出sqlite 命令行模式。

3. 创建项目

现在让我门创建Xcode项目。

首先在Xcode中选择创建一个新的” Navigation-Based Application“

输入项目名称"sqliteDemo",由于之前在同一位置创建了同名目录,Xcode将会询问是否覆盖已经存在的目录sqliteDemo,选则是。这将使项目目录结构如下图所示。

4. 将sqlite框架和PersonDatabas数据库导入项目

首先我们需要将sqlite库引入项目。请右键点击左部菜单中的"Frameworks"目录,选择“Add > Existing Frameworks…”,

然后在本地目录中选择“/Developer/Platforms/iPhoneOS.platform/Developer /SDKs/iPhoneOS3.0.sdk/usr/lib/”,找到“libsqlite3.0.dylib”文件并双击。

一个弹出窗口将出现,点击其中“Add”按钮将库添加入项目中。

我们也需要将PersonDatabas 数据库导入Resources目录。请右键点击“Resources”目录,选择“Add > Existing Files…”,在本地目录中选择“/Users/Smitty/Documents/sqliteDemo/”,双击PersonDatabase.sql文件,点击弹出窗口中的“Add”按钮将其导入当前项目。

5. 开始编程

首先创建一个Person类。右键点击“Classes”目录,选择“Add > New File…”,在弹出窗口中选择“iPhone OS > Cocoa Touch Class > Objective-C class > NSObject” 并点击“Next”按钮。在下一窗口中,设置File Name为“Person.m”。点击“Finish”按钮完成创建。

修改Classes/Person.h为:

@interface Person : NSObject {

Nsstring *name;

Nsstring *title;

Nsstring *iconURL;

}

@property (nonatomic,retain) Nsstring *name;

@property (nonatomic,retain) Nsstring *title;

@property (nonatomic,retain) Nsstring *iconURL;

-(id)initWithName:(Nsstring *)n title:(Nsstring *)t url:(Nsstring *)u;

@end

修改Classes/Person.m为:

#import "Person.h"

@implementation Person

@synthesize name,iconURL;

-(id)initWithName:(Nsstring *)n title:(Nsstring *)t url:(Nsstring *)u {

self.name=n;

self.title=t;

self.iconURL=u;

return self;

}

@end

修改Other Sources/main.m为:

#import

#import "Person.h"

int main(int argc,char *argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//setup database name.

Nsstring *databaseName = @"PersonDatabase.sql";

// Get the path to the documents directory and append the databaseName

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

Nsstring *documentsDir = [documentPaths objectAtIndex:0];

Nsstring *databasePath = [documentsDirstringByAppendingPathComponent:databaseName];

// Create a FileManager object,we will use this to check the status

// of the database and to copy it over if required

NSFileManager *fileManager = [NSFileManager defaultManager];

// Check if the database has already been created in the users filesystem

BOOL success = [fileManager fileExistsAtPath:databasePath];

// If the database already exists then return without doing anything

if(!success){

// If not then proceed to copy the database from the application to the users filesystem

// Get the path to the database in the application package

Nsstring *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

// copy the database from the package to the users filesystem

[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

}

// Query the database for all person records and construct the "person" array

sqlite3 *database;

// Init the animals Array

NSMutableArray *personArr = [[NSMutableArray alloc] init];

// Open the database from the users filessytem

if(sqlite3_open([databasePath UTF8String],&database) == sqlITE_OK) {

// Setup the sql Statement and compile it for faster access

const char *sqlStatement = "select * from person";

sqlite3_stmt *compiledStatement;

if(sqlite3_prepare_v2(database,sqlStatement,-1,&compiledStatement,NULL) == sqlITE_OK) {

// Loop through the results and add them to the Feeds array

while(sqlite3_step(compiledStatement) == sqlITE_ROW) {

// Read the data from the result row

Nsstring *aName = [Nsstring stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,1)];

Nsstring *aTitle = [Nsstring stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,2)];

Nsstring *aIconUrl = [Nsstring stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,3)];

// Create a new animal object with the data from the database

Person *person = [[Person alloc] initWithName:aName title:aTitle url:aIconUrl];

// Add the animal object to the animals Array

[personArr addobject:person];

[person release];

}

}

// Release the compiled statement from memory

sqlite3_finalize(compiledStatement);

}

sqlite3_close(database);

for (Person *tmpPerson in personArr) {

NSLog(@"You can see %@ %@'s icon at %@",tmpPerson.title,tmpPerson.name,tmpPerson.iconURL);

[tmpPerson release];

}

NSLog(@"Done.");

[personArr release];

[fileManager release];

[pool release];

return (0);

}

6. 编译执行

完成修改后,运行“Build > Build and Run”,关掉弹出的iPhone Simulator窗口,选择"Run > Console" 打开控制台,可看到如下输出信息。

参考:

http://www.cnblogs.com/AlexLiu/archive/2010/04/21/1716740.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


SQLite架构简单,又有Json计算能力,有时会承担Json文件/RESTful的计算功能,但SQLite不能直接解析Json文件/RESTful,需要用Java代码硬写,或借助第三方类库,最后再拼成insert语句插入数据表,代码非常繁琐,这里就不展示了。参考前面的代码可知,入库的过程比较麻烦,不能只用SQL,还要借助Java或命令行。SPL是现代的数据计算语言,属于简化的面向对象的语言风格,有对象的概念,可以用点号访问属性并进行多步骤计算,但没有继承重载这些内容,不算彻底的面向对象语言。...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite 是在世界上最广泛部署的 SQL 数据库引擎。SQLite 源代码不受版权限制。本教程将告诉您如何使用 SQLite 编程,并让你迅速上手。.................................
安卓开发,利用SQLite实现登陆注册功能
相比大多数数据库而言,具有等优势,广泛应用于、等领域。
有时候,一个项目只有一个数据库,比如只有SQLite,或者MySQL数据库,那么我们只需要使用一个固定的数据库即可。但是一个项目如果写好了,有多个用户使用,但是多个用户使用不同的数据库,这个时候,我们就需要把软件设计成可以连接多个数据库的模式,用什么数据库,就配置什么数据库即可。4.Users实体类,这个实体类要和数据库一样的,形成一一对应的关系。11.Sqlite数据库,需要在代码里面创建数据库,建立表,再建立数据。8.我们开启MySQL数据库,然后进行调试,看程序的结果。2.安装SqlSugar。
基于Android的背单词软件,功能强大完整。
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统。说白了就是使用起来轻便简单,