关于Sqlite iPad那些事
关于Sqlite iPad那些事是本文要介绍的内容,主要是来学习ipad中Sqlite数据库的内容,首先添加APPLE提供的 sqlite 操作用程序库 ibsqlite3.0.dylib 到工程中。
位置如下
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS${VER}.sdk/usr/lib/libsqlite3.0.dylib
sqlite3 *database; NSArray *paths= NSSearchPathForDirectoriesInDomains(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); }
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
下面这2句都不可以执行
相关推荐
DAV数据库 2020-06-17
airfling 2020-05-31
zbcaicai 2020-05-26
beibeijia 2020-04-25
Rain 2020-04-16
Plant 2020-04-08
园搬家测试账号 2020-03-25
MFCJCK 2020-02-24
xiaoxiangyu 2020-02-23
Plant 2020-02-03
CharlesYooSky 2020-02-01
MFCJCK 2020-01-31
nxcjh 2020-01-29
wintershii 2020-01-18
zbcaicai 2020-01-03
MFCJCK 2019-12-30
airfling 2019-12-28
Plant 2019-12-27
Dlanguage 2019-12-27
Dlanguage 2019-12-25
whyname 2019-12-23