Nov 19 2009

Lita: SQLite Database Administration

If you need a good (and free!) SQLite Admin, I’ve been using Lita, created by David Deraedt, for a bit and it’s been working well.
I must admit I mostly use it just for confirmation that my code did what I expected it to, but it seems pretty solid.

Post to Twitter


Apr 3 2008

Resolving gcc errors installing sqlite3 gem on Leopard

The following error kept coming up when trying to install Sqlite3 on OS X (actually, the same thing happened when trying to install Fink too). It was finally resolved by installing the latest XCode packages.
Running ‘sudo ./configure –prefix=/usr/local’
would return:

checking build system type... i386-apple-darwin9.2.2
checking host system type... i386-apple-darwin9.2.2
checking for gcc... gcc
checking for C compiler default output file name...
configure: error: C compiler cannot create executables
See `config.log' for more details.
Here’ all the important stuff from the log:
## ----------- ##
## Core tests. ##
## ----------- ##
configure:2062: checking build system type
configure:2080: result: i386-apple-darwin9.2.2
configure:2102: checking host system type
configure:2117: result: i386-apple-darwin9.2.2
configure:2187: checking for gcc
configure:2203: found /usr/bin/gcc
configure:2214: result: gcc
configure:2452: checking for C compiler version
configure:2459: gcc --version >&5
i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
configure:2462: $? = 0
configure:2469: gcc -v >&5
Using built-in specs.
Target: i686-apple-darwin8
Configured with: /private/var/tmp/gcc/gcc-5367.obj~1/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=powerpc-apple-darwin8 --with-arch=nocona --with-tune=generic --program-prefix= --host=i686-apple-darwin8 --target=i686-apple-darwin8
Thread model: posix
gcc version 4.0.1 (Apple Computer, Inc. build 5367)
configure:2472: $? = 0
configure:2479: gcc -V >&5
gcc: argument to `-V' is missing
configure:2482: $? = 1
configure:2505: checking for C compiler default output file name
configure:2532: gcc    conftest.c  >&5
/usr/bin/ld: /usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../libSystem.dylib unknown flags (type) of section 6 (__TEXT,__literal16) in load command 0
collect2: ld returned 1 exit status
configure:2535: $? = 1
configure:2573: result:
configure: failed program was:
| /* confdefs.h.  */
| #define PACKAGE_NAME "sqlite"
| #define PACKAGE_TARNAME "sqlite"
| #define PACKAGE_VERSION "3.5.7"
| #define PACKAGE_STRING "sqlite 3.5.7"
| #define PACKAGE_BUGREPORT ""
| /* end confdefs.h.  */
|
| int
| main ()
| {
|
|   ;
|   return 0;
| }
configure:2580: error: C compiler cannot create executables
See `config.log' for more details.
## ----------- ##
## confdefs.h. ##
## ----------- ##
#define PACKAGE_NAME "sqlite"
#define PACKAGE_TARNAME "sqlite"
#define PACKAGE_VERSION "3.5.7"
#define PACKAGE_STRING "sqlite 3.5.7"
#define PACKAGE_BUGREPORT ""
configure: exit 77

Post to Twitter


Feb 12 2008

Data storage and caching with SQLite databases and Adobe AIR

You may or may not know by now that the AIR runtime includes a version of SQLite engine. Being a smaller implementation of SQL, SQLite supports all your usual database transactions, a lot of the complex queries, and triggers. With it,you can create a database to store all the data for your Flex/AIR desktop applications, store data for offline use of your Flex/AIR desktop/web applications, or for caching of data. It’s all done in the same way. I was recently working on an application where I wanted the user to have an option to be logged in automatically when the app was launched, so the first time the user logged in I created a database to cache that login info. From then on when the application is launched it checks to see if the database exists, and if so it grabs the login info, and logs them in automatically.

I’ve made a simple example which is a super simple desktop application which stores it’s data in a SQLite database. The idea and implementation here is very similar to what I just described for the caching example. On launch, we first check to see if the database exists. If it does, that means it ought to contain some data so we grab the data and display it. If the database doesn’t exist, we create it and add our one default entry, then load it into the application. The user can then add, remove, and update entries. When each of these transactions sends a result of success, we reload all the data in the database. Obviously in a real-world application this wouldn’t be a great idea; that’s too much overhead. One option would be to just manipulate the dataProvider ArrayCollection after each successful transaction — but for this simple example I’m leaving it the way it is for the intent of simply demonstrating using SQLite.

At this point I think I’ll let the code speak for itself.


Basically you’ll notice the basic steps are:

1. Create a connection: connection = new SQLConnection();

2. Define the database file: dbFile = File.applicationStorageDirectory.resolvePath(dbFileString);

3. Open (or create and open ) the database: connection.open(dbFile);

4. Create an empty SQLStatement: var sql : SQLStatement = new SQLStatement();

5. Create a query: var sqlString : String = “CREATE TABLE Users (” +

” uid INTEGER PRIMARY KEY AUTOINCREMENT, ” +

” name TEXT, ” +

” phonenumber TEXT)”;

6. Attach the connection and the query to the SQLStatement:

sql.sqlConnection = connection;

sql.text = sqlString;

7. Create event listeners for success and failure:

sql.addEventListener(SQLEvent.RESULT, onDBCreateTableResult);

sql.addEventListener(SQLErrorEvent.ERROR, onDBCreateTableError);

8. Execute! : sql.execute()


 
 
 
 
 
 
 
 
 
 
 
 
 
 

Post to Twitter