SQLDataAccess 0.2.2

SQLDataAccess 0.2.2

测试已测试
语言语言 Obj-CObjective C
许可协议 MIT
发布上次发布2017年3月

Pat Murphy维护。



示例

要运行示例项目,克隆仓库,然后在您的Podfile所在目录中首先运行>pod install。要获取最新版本,请从您的Podfile所在目录运行>pod update

要求

您应该使用Xcode 8.0+和iOS 10.0+来运行这个CocoaPod。

安装

SQLDataAccess可以通过CocoaPods获得。要安装它,只需将以下行添加到您的Podfile中

use_frameworks!
target "YourAppTargetName" do
pod "SQLDataAccess"
end

将位于:SQLDataAccess/SQDataAccess/Resources/Example.db的数据库“Example.db”复制,并将其放入您的NSBundle目录中的任何位置。确保通过将其拖入Xcode使其在Xcode的项目导航器中可见。名称由AppConstants.h中的定义DB_FILE和DB_FILEX决定。

“Example.db”包含以下信息,以下是要创建它的SQL。

DROP TABLE IF EXISTS "AppInfo";
CREATE TABLE "AppInfo" (
 "ID" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
 "name" text,
 "value" text,
 "descrip" text
);

现在在您的AppDelegate : didFinishLaunchingWithOptions中

//Set some fake user for now
if(![SettingsModel getLoginState])
{
    [SettingsModel setUserName:@"John Smith"];
    [SettingsModel setUserId:[NSNumber numberWithInt:1]];
    [SettingsModel setCountry:@"USA"];
}
//If you want to run SQLCipher, add ENRYPT to Xcode project's :
//Apple LLVM 8.0 - Preprocessing : Preprocessor Macros
#ifdef ENCRYPT
//Set this just once! These are required for SQLCipher and encryption since the
//passwords are always used for the encryption keys plus some salt. 
//SQLDataAccess explains how to use SQLCipher instead of SQLite, you need the 
//libsqlcipher-ios.a added to your project.
if(![SettingsModel getLoginState])
{
    [SettingsModel setDBPW0:@"1260793RTgu"];
    [SettingsModel setDBPW1:@"1260793RTgu"];
}
[SettingsModel setDBCanEncrypt:YES];
#endif

//This register tells SQLDataAccess the database is Ok to access now
//If the Database can't be accessed this register will get set to NO
//And a warning will print out in the Console.
[SettingsModel setLoginState:YES];

[AppManager InitializeAppManager];
//Open the Database. Typically you wouldn't do this until a user has entered
//a password first from the login or signin views. 
//Never open the DB with Encryption until you have a password present in 
//getDBPW0, otherwise the DB will fail to open and will fail every single SQL
//query.

//Always open the DB when the App initializes, this applys the encryption keys
//if the DB is encrypted so the DB can be read, this is only done once, but if 
//it's called more then once it's no harm since it will only execute once.
//For a non-encrypted or plain-text DB this open's the DB's connection 
//SQLDataAccess : sqlite3dbConn
[[AppManager SQLDataAccess] openConnection];

//Lets read the database first to see if the table AppInfo exists and has a 
//column of 'name' which contains the value 'App', this is mostly a sanity 
//check.
BOOL status = NO;
//A very basic SQL select statement, super simple to write!
NSMutableArray *dataArray = 
[[AppManager SQLDataAccess] GetRecordsForQuery:@"select name from AppInfo ",nil];
if([dataArray count] > 0)
{
    //If you setup Example.db correctly, the App will come here
    NSString *appName = [[dataArray objectAtIndex:0] objectForKey:@"name"];
    if([appName isEqualToString:@"App"])
        status = YES;
}

if(status)
{
    //The table should exist so the App should come here and update the table
//with this info.
//See how easy the SQL is to write for an update! 
//Just add your parameters after the query, don't forget the nil, it's required!
//If the SQL is messed up, SQLDataAccess will print out an error in the console
//telling you where SQL is wrong!

    BOOL executeStatus = 
[[AppManager SQLDataAccess] ExecuteStatement:@"update AppInfo set name = ?, value = ? ,descrip = ?"
,@"App",@"1.2",@"Database works",nil];
}
else
{
    //See how easy the SQL is to write for an insert! 
//Just add your parameters after the query!
    BOOL executeStatus = 
[[AppManager SQLDataAccess] ExecuteStatement:@"insert into AppInfo (name,value,descrip) values(?,?,?)"
,@"App",@"1.2","Database works",nil];
}

//Now read the 'AppInfo' table and display it in the Console log window.
//See how easy the SQL is to write for a select!
//If you have a where clause just add it's parameters after the query!
dataArray = [[AppManager SQLDataAccess] GetRecordsForQuery:@"select * from AppInfo ",nil];

NSLog(@"AppDelegate : dataArray = %@",dataArray);

现在在您的AppDelegate : applicationDidEnterBackground中

//This closes the DB when the App goes into the background. 
//This is important and secures your database.
[[AppManager DataAccess] closeConnection];

现在在您的AppDelegate : applicationDidBecomeActive中

//This opens the DB when the App becomes active again, and if it's encrypted it 
//applies the encryption keys again.
//If you don't open the DB, the connection won't be valid, and no SQL will execute.
[[AppManager DataAccess] openConnection];

这是应用应该做的事情

1) On the first initialization, the App copies Example.db to the App's working Documents
   directory so it can access it. Remember you have to copy 'Example.db' first, and add 
   it to your Xcode project!
2) The App then accesses 'Example.db', and writes into it, 'App', '1.2', 'Database works'
3) If it works you see this in the console

AppDelegate : dataArray = (
    {
    ID = 1;
    descrip = "Database works";
    name = App;
    value = "1.2";
}

对于更复杂的示例,使用SQL事务,请参阅以下内容:

简单的SQLite数据库示例。

DBExample

或者

使用事务的更复杂的SQLite地址簿和日历数据库示例。

ABExample

使用相同的SQLDataAcess类。

作者

pmurphyjam,[email protected]

许可协议

SQLDataAccess可以在MIT许可下获得。有关更多信息,请参阅LICENSE文件。