关于Sqlite iPad那些事

移动开发 iOS
关于Sqlite iPad那些事是本文要介绍的内容,主要是来学习ipad中Sqlite数据库的内容,首先添加APPLE提供的 sqlite 操作用程序库 ibsqlite3.0.dylib 到工程中。

关于Sqlite iPad那些事是本文要介绍的内容,主要是来学习ipadSqlite数据库的内容,首先添加APPLE提供的 sqlite 操作用程序库 ibsqlite3.0.dylib 到工程中。

位置如下

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS${VER}.sdk/usr/lib/libsqlite3.0.dylib 
  • 1.
sqlite3 *database;  
NSArray *pathsNSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsDirectory = [paths objectAtIndex:0];  
NSString *strPaths =  [documentsDirectory stringByAppendingPathComponent:kFilename];  
if (sqlite3_open([strPaths UTF8String], &database) != SQLITE_OK) {  
        sqlite3_close(database);  
        NSAssert(0, @"Failed to open databse");  
    }  
NSString *createSQL = @"CREATE TABLE IF NOT EXISTS FIELDS (ROW INTEGER PRIMARY KEY, FIELD_DATA TEXT)";  
if(sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK){  
        sqlite3_close(database);  
        NSAssert1(1, @"Error create table :%s", errorMsg);  
    }  
NSString *query = @"SELECT ROW ,FIELD_DATA FROM FIELDS ORDER BY ROW";  
sqlite3_stmt *statement;  
 
if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK){  
        while (sqlite3_step(statement) == SQLITE_ROW) {  
            int row = sqlite3_column_int(statement, 0);  
            char *rowData = (char *)sqlite3_column_text(statement, 1);  
              
            NSString *fieldName = [[NSString alloc] initWithFormat:@"field%d", row];  
            NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData];  
              
            UITextField *field = [self valueForKey:fieldName];  
            field.text = fieldValue;  
            [fieldName release];  
            //[fieldName release];  
            [fieldValue release];  
        }  
        sqlite3_finalize (statement);  
    } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

sqllite存在沙盒内,所以打开的时候不需要name和password,但由于字符的格式不用,所以需要通过,[nsString, UTF8String]来转换。

sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil),这是执行sql语句的命令。statement记录状态。  
 
sqlite3_column_*(statement, 0);返回字段值  
sqlite3_finalize (statement);结束退出  
 
#import "SQLiteTutorialAppDelegate.h"  
#import "RootViewController.h"  
#import "Animal.h" // Import the animal object header  
 
@implementation SQLiteTutorialAppDelegate  
 
@synthesize window;  
@synthesize navigationController;  
@synthesize animals; // Synthesize the aminals array  
 
- (void)applicationDidFinishLaunching:(UIApplication *)application {  
// Setup some globals  
databaseName = @"AnimalDatabase.sql";  
 
// Get the path to the documents directory and append the databaseName  
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsDir = [documentPaths objectAtIndex:0];  
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];  
 
// Execute the "checkAndCreateDatabase" function  
[self checkAndCreateDatabase];  
 
// Query the database for all animal records and construct the "animals" array  
[self readAnimalsFromDatabase];  
 
// Configure and show the window  
[window addSubview:[navigationController view]];  
[window makeKeyAndVisible];  
}  
 
- (void)applicationWillTerminate:(UIApplication *)application {  
// Save data if appropriate  
}  
 
- (void)dealloc {  
[animals release];  
[navigationController release];  
[window release];  
[super dealloc];  
}  
 
-(void) checkAndCreateDatabase{  
// Check if the SQL database has already been saved to the users phone, if not then copy it over  
BOOL success;  
 
// 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  
success = [fileManager fileExistsAtPath:databasePath];  
 
// If the database already exists then return without doing anything  
if(success) return;  
 
// 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];  
 
[fileManager release];  
}  
 
-(void) readAnimalsFromDatabase {  
// Setup the database object  
sqlite3 *database;  
 
// Init the animals Array  
animals = [[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 animals";  
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 *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];  
NSString *aImageUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];  
 
// Create a new animal object with the data from the database  
Animal *animal = [[Animal alloc] initWithName:aName description:aDescription url:aImageUrl];  
// Add the animal object to the animals Array  
[animals addObject:animal];  
[animal release];  
}  
}  
// Release the compiled statement from memory  
sqlite3_finalize(compiledStatement);  
}  
sqlite3_close(database);  
}  
@end 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.

下面这2句都不可以执行

if (select count(*) from sqlite_master where table="TB_Clothing_Main")  
DROP TABLE "TB_Clothing_Main";  
 
if EXISTS (select count(*) from sqlite_master where name = 'TB_Clothing_Main')  
DROP TABLE 'TB_Clothing_Main';  
 
BEGIN;  
 
CREATE TABLE [TB_Clothing_Main] (  
[clothing_ID] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,  
[clothing_who] INTEGER NOT NULL,  
[clothing_decription] NVARCHAR(128) NULL,  
[clothing_brend] INTEGER NOT NULL,  
[clothing_buy_location_ID] INTEGER NOT NULL,  
[clothing_store_location_ID] INTEGER NOT NULL,  
[clothing_size_shoulder] FLOAT NULL,  
[clothing_size_chest] FLOAT NULL,  
[clothing_size_waist] FLOAT NULL,  
[clothing_size_hip] FLOAT NULL,  
[clothing_size_length] FLOAT NULL,  
[clothing_type] INTEGER NOT NULL,  
[clothing_price] FLOAT NULL,  
[clothing_main_picture] NVARCHAR(128) NULL  
);  
 
INSERT INTO "TB_Clothing_Main" VALUES(0, 1, 'marc jacobs blue short T', 1, 2, 1, 37.5, 45,   38,   NULL, 66, 0, NULL,   'mj01');  
INSERT INTO "TB_Clothing_Main" VALUES(1, 1, 'marc jacobs pink short T', 1, 1, 0, 37,   43.5, 36,   NULL, 64, 0, NULL,   'mj02');  
INSERT INTO "TB_Clothing_Main" VALUES(2, 2, 'nautica blue coat',        0, 0, 2, 41,   49.5, 47.2, NULL, 60, 1, NULL,   'baba01');  
INSERT INTO "TB_Clothing_Main" VALUES(3, 1, 'juicy yellow coat',        3, 1, 3, 40,   45.1, 40.2, 47  , 62, 2, 1080.2, 'juicy01');  
INSERT INTO "TB_Clothing_Main" VALUES(4, 1, 'siwy jeans',               4, 1, 3, NULL,   NULL, 78, 93  , 94, 3, 1380,   'siwy01');  
 
COMMIT; 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

小结:关于Sqlite iPad那些事的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 新浪博客
相关推荐

2012-01-02 19:30:22

iPad

2012-05-01 21:27:55

图标

2012-05-01 08:06:49

手机

2012-07-13 00:03:08

WEB前端开发WEB开发

2011-07-04 13:51:02

QT 对象 模型

2011-08-01 17:31:25

Xcode开发 Cocoa

2019-12-10 08:00:46

Kata容器Linux

2022-09-09 08:08:28

开源项目服务

2011-07-19 15:33:57

iPhone

2015-08-13 10:54:46

2015-09-14 09:28:47

2011-06-30 10:59:43

2021-03-18 16:05:20

SSD存储故障

2009-02-19 10:21:00

路由多WAN口

2024-03-18 00:00:05

Java服务JVM

2012-04-05 10:49:40

服务器SSL证书

2015-08-19 09:10:37

程序员面试

2021-05-17 08:18:35

Java内存模型JMM

2015-07-23 13:10:38

服务器虚拟化

2017-10-19 16:27:34

点赞
收藏

51CTO技术栈公众号