Xcode project. The first task also illustrates adding
the SQLite library to your Xcode project. After creating the database
and loading it, you can then use the database programmatically via its C
programming interface.
If you don’t already have Firefox, download and install it. Select Add-ons from the Tools menu (Figure 1).
Select Get Add-ons, type SQLite in the search box, and install SQLite Manager. Once installed and you have restarted Firefox, select Tools | SQLite Manager. Select the New icon (the blank paper graphic), and create a new database named myDatabase.
Save the database file some place where you can easily find it later.
Note SQLite Manager automatically adds the .sqlite extension. Click Create Table and create a new table named photos. Add three columns: id, name, and photo. Make id an INTEGER and check Primary Key and Autoinc check boxes. Make name a VARCHAR and check only Allow Null. Make photo a BLOB and check only Allow Null. Click OK and the SQLite Manager generates the database table. Note
SQLite does not enforce
foreign key relationships. You must instead write triggers manually to
enforce foreign key relationships. SQLite does not support right outer
joins or full outer joins. SQLite views are read-only.
Click the Browse & Search tab, and then click the Add Record button. In the action sheet, leave id blank. Type Icon One
for the name. Notice the small paper clip beside photo. Move your mouse
over the paper clip, and the tooltip should say “Add File as a Blob” (Figure 3).
Click the paper clip and add any photo from your computer. If the photo
column doesn’t say something like BLOB (Size: 65984), the file was not
correctly added as a blob.
Click OK, and the record will be added. Add another record, selecting any other image from your computer. From
the menu, select Database | Close Database from the main menus to close
the database. You can now exit SQLite Manager and quit Firefox. Open Xcode and create a new View-based Application. Name the application MyDBProject. Expand
Frameworks in the Navigator and right-click on any one of the
frameworks (e.g., UIKit) and select Show in Finder from the pop-up menu.
This will get you to the folder with all of your frameworks on your
computer. Move up two folder levels from there to the SDK folder and
then open user/lib. Drag
and drop the libsqlite3.0.dylib framework from that folder to
Frameworks in the Navigator in Xcode. In the dialog that pops up, be
sure to uncheck “Copy items into destination group’s folder” and click
Finish. Add
the database file that you created earlier to the Resources folder. Be
sure to check the Copy Items check box so that the database file gets
copied into MyDBProject. This
task is now complete with the SQLite library added to the project and
the database file copied into the project’s resources. Do not delete the
project or database.
|
Note
Adding
binary data using SQLite Manager in Firefox seems to be buggy.
Sometimes it works, sometimes not. If after adding a record the photo
column is blank, just edit the row and add the photo file’s content
again. Updating the blob seems to be more stable in SQLite Manager.