Skip to main content

SQLite 3 Procedure & Functions


SQLite 3 Procedure & Functions

iOS - SQLite Database

SQLite can be used in iOS for handling data. It uses sqlite queries, which makes it easier for those who know SQL.

Steps Involved

Step 1. Create a simple View based application.
Step 2. Select your project file, then select targets and then add libsqlite3.dylib library in choose frameworks.
Step 3. Create a new file by selecting File-> New -> File... -> select Objective C class and click next.
Step 4. Name the class as DBManager with "sub class of" as NSObject.
Step 5. Select create.
Step 6. Update DBManager.h as follows −

SQLite 3 Functions Preview

sqlite3_open: This function is used to create and open a database file. It accepts two parameters, where the first one is the database file name, and the second a handler to the database. If the file does not exist, then it creates it first and then it opens it, otherwise it just opens it.
sqlite3_prepare_v2: The purpose of this function is to get a SQL statement (a query) in string format, and convert it to an executable format recognizable by SQLite 3.
sqlite3_step: This function actually executes a SQL statement (query) prepared with the previous function. It can be called just once for executable queries (insert, update, delete), or multiple times when retrieving data. It’s important to have in mind that it can’t be called prior to the sqlite3_preprare_v2 function.
sqlite3_column_count: This method’s name it makes it easy to understand what is about. It returns the total number of columns (fields) a contained in a table.
sqlite3_column_text: This method returns the contents of a column in text format, actually a C string (char *) value. It accepts two parameters: The first one is the query converted (compiled) to a SQLite statement, and the second one is the index of the column.
sqlite3_column_name: It returns the name of a column, and its parameters are the same to the previous function’s.
sqlite3_changes: It actually returns the number of the affected rows, after the execution of a query.
sqlite3_last_insert_rowid: It returns the last inserted row’s ID.
sqlite3_errmsg: It returns the description of a SQLite error.
sqlite3_finalize: It deletes a prepared statement from memory.
sqlite3_close: It closes an open database connection. It should be called after having finished any data exchange with the database, as it releases any reserved system resources.

Comments

Popular posts from this blog

What's the difference between enum, struct and class?

Enums An enum is considered as a structured data type that can be modified without needing to change say a String or Int multiple times within your code, for example the below shows how easy it would be to change something by accident and forget to change it somewhere else. let myString = "test" if myString == "ttest" { // Doesn't execute because "ttest" is the value assigned to "myString" } With a enum we can avoid this and never have to worry about changing the same thing more than once enum MyEnum : String { case Test = "test" } let enumValue = MyEnum . Test if enumValue == MyEnum . Test { // Will execute because we can reassign the value of "MyEnum.Test" unless we do so within "MyEnum" } Structs I'm not sure how much you know about the MVC pattern but in Swift this is a common practise, before I explain how structs are useful I'll...

What Is a Closure?

Closures are self contained chunks of code that can be passed around and used in your code. Closures can capture and store references to any constants or variables from the context in which they are defined. This is know as closing over those variables, hence the name closures. Closures are use intensively in the Cocoa frameworks – which are used to develop iOS or Mac applications. Functions are a special kind of closures. There are three kinds of closures: global functions  – they have a name and cannot capture any values nested functions  – they have a name and can capture values from their enclosing functions closure expressions  – they don’t have a name and can capture values from their context The thing to keep in mind for the moment is that you already have an intuition about closures. They are almost the same as functions but don’t necessarily have a name. // a closure that has no parameters and return a String var hello: () -> ( String ) = { ...