sqlite3_iphone

前端之家收集整理的这篇文章主要介绍了sqlite3_iphone前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_3@ 1. 预备知识

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

@H_403_3@ 2. 创建数据库

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

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

@H_403_3@ cd /Users/Smitty/Documents

@H_403_3@ mkdir sqliteDemo

@H_403_3@ cd sqliteDemo

@H_403_3@ sqlite3 PersonDatabase.sql

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

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

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

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

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

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

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

@H_403_3@ 3. 创建项目

@H_403_3@ 现在让我门创建Xcode项目。

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

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

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

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

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

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

@H_403_3@

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

@H_403_3@ 5. 开始编程

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

@H_403_3@ 修改Classes/Person.h为:

@H_403_3@ @interface Person : NSObject {

@H_403_3@ NSString *name;

@H_403_3@ NSString *title;

@H_403_3@ NSString *iconURL;

@H_403_3@ }

@H_403_3@ @property (nonatomic,retain) NSString *name;

@H_403_3@ @property (nonatomic,retain) NSString *title;

@H_403_3@ @property (nonatomic,retain) NSString *iconURL;

@H_403_3@ -(id)initWithName:(NSString *)n title:(NSString *)t url:(NSString *)u;

@H_403_3@ @end

@H_403_3@ 修改Classes/Person.m为:

@H_403_3@ #import "Person.h"

@H_403_3@ @implementation Person

@H_403_3@ @synthesize name,iconURL;

@H_403_3@ -(id)initWithName:(NSString *)n title:(NSString *)t url:(NSString *)u {

@H_403_3@ self.name=n;

@H_403_3@ self.title=t;

@H_403_3@ self.iconURL=u;

@H_403_3@ return self;

@H_403_3@ }

@H_403_3@ @end

@H_403_3@ 修改Other Sources/main.m为:

@H_403_3@ #import

@H_403_3@ #import "Person.h"

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

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

@H_403_3@ //setup database name.

@H_403_3@ NSString *databaseName = @"PersonDatabase.sql";

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

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

@H_403_3@ NSString *documentsDir = [documentPaths objectAtIndex:0];

@H_403_3@ NSString *databasePath = [documentsDirstringByAppendingPathComponent:databaseName];

@H_403_3@

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

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

@H_403_3@ NSFileManager *fileManager = [NSFileManager defaultManager];

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

@H_403_3@ BOOL success = [fileManager fileExistsAtPath:databasePath];

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

@H_403_3@ if(!success){

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

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

@H_403_3@ NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

@H_403_3@ // Copy the database from the package to the users filesystem

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

@H_403_3@ }

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

@H_403_3@ sqlite3 *database;

@H_403_3@ // Init the animals Array

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

@H_403_3@ // Open the database from the users filessytem

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

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

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

@H_403_3@ sqlite3_stmt *compiledStatement;

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

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

@H_403_3@ while(sqlite3_step(compiledStatement) == sqlITE_ROW) {

@H_403_3@ // Read the data from the result row

@H_403_3@ NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,1)];

@H_403_3@ NSString *aTitle = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,2)];

@H_403_3@

@H_403_3@ NSString *aIconUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,3)];

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

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

@H_403_3@ // Add the animal object to the animals Array

@H_403_3@ [personArr addObject:person];

@H_403_3@ [person release];

@H_403_3@ }

@H_403_3@ }

@H_403_3@ // Release the compiled statement from memory

@H_403_3@ sqlite3_finalize(compiledStatement);

@H_403_3@ }

@H_403_3@ sqlite3_close(database);

@H_403_3@ for (Person *tmpPerson in personArr) {

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

@H_403_3@ [tmpPerson release];

@H_403_3@ }

@H_403_3@ NSLog(@"Done.");

@H_403_3@ [personArr release];

@H_403_3@ [fileManager release];

@H_403_3@ [pool release];

@H_403_3@ return (0);

@H_403_3@ }

@H_403_3@ 6. 编译执行

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

@H_403_3@ 参考:

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

猜你在找的Sqlite相关文章