SeriesArticleNavigation
- Using flex in Adobe AIR to connect to the SQLite database (1) (creating databases and tables)
- Use flex in Adobe AIR to connect to the SQLite database (2) (add, delete, modify, and statement parameters)
- Connecting to the SQLite database using flex in Adobe AIR (3) (query)
- Connecting to the SQLite database using flex in Adobe AIR (4) (Transaction)
- Index of flex and fms3 articles
- Fms3 and flex create online multi-person video conferences and video chats (with the original code)
- Free beauty video chat, multi-person video conferencing feature enhanced version (fms3 and flex development (with source code ))
This chapter mainly summarizes the relational database engine (SQLite), synchronous and asynchronous execution modes, and creates databases and tables.
Adobe AIR includes an SQL-based Relational Database Engine (SQLite) that runs during runtime and stores data locally in the running AIR application.ProgramIn the database file on the computer (for example, on the hard drive of the computer ). Because database operations and data file storage are both performed locally, the air application can use the database regardless of whether the network connection is available.
A single Adobe AIR local SQL database is stored as a single file in a computer's file system. The runtime includes the SQL database engine, which manages the creation and structuring of database files, as well as operations and retrieval of data in database files. The mode or location of storing database data on the file system is not specified during runtime. On the contrary, each database is completely stored in a single file. You specify the location where the database files are stored in the file system. A single AIR application can access one or more independent databases (that is, individual database files ). Because each database is stored as a single file in the file system at runtime, you can find your database according to the application design and file access constraints of the operating system as needed. Each user can have a separate database file with specific data, or the database file can be accessed by all application users who share data on a single computer. Because data is local to a single computer, users on different computers do not automatically share data. The local SQL database engine does not provide any function to execute SQL statements on remote databases or server-based databases.
About SQLite
Features of SQLite:
1. Acid transactions
2. Zero Configuration-no installation or configuration management required
3. A complete database stored in a single disk file
4. database files can be freely shared among machines with different byte sequences.
5. Support database size to 2 TB
6. Small enough, roughly 30 thousand rows of CCode, 250 K
7. faster operations than some popular databases in most common databases
8. Simple and Easy APIs
9. Includes TCL binding and supports binding in other languages through wrapper.
10. Well notedSource codeAnd has a test coverage rate of more than 90%.
11. Independence: no additional dependencies
12. Source is completely open. You can use it for any purpose, including selling it.
13. Support for multiple development languages: C, PHP, Perl, Java, ASP. NET, and Python
For more details, refer to the following sites:
Http://www.sqlite.org/
Http://www.sqlite.com.cn/
Usage of local SQL database
The air local SQL database function can be used to store application data on your local computer for any purpose. Adobe AIR includes several local data storage mechanisms, each of which has different advantages. The following are some possible uses of the local SQL database in the air application:
1. for data-oriented applications (such as Address Book), databases can be used to store master application data.
2. For document-oriented applications (users create documents to be saved and possibly shared), you can save each document as a database file at a specified location. (However, note that any AIR application can open database files. Therefore, we recommend that you use a separate encryption mechanism for sensitive documents .)
3. for network-supported applications, the database can be used to store the local cache of application data or temporarily store data when the network connection is unavailable. You can create a mechanism to synchronize local databases with network data storage.
4. For any application, the database can be used to store individual user application settings, such as user options or application information (such as window size and location ).
Synchronous and asynchronous execution modes
1. When you write code to process a local SQL database, database operations are performed in either of the two modes: asynchronous or synchronous. In general, the sample code shows how to perform each operation in these two ways, so that you can use the example that best suits your needs.
2. In asynchronous execution mode, a command is provided for the runtime, and an event is scheduled when the requested operation is completed or fails. First, notify the database engine to perform the operation. The database engine works in the background while the application continues to run. Finally, when the operation is completed (or fails), the database engine schedules the event. The Code triggered by the event performs subsequent operations. This method has an important advantage: the database operation is performed in the background during the runtime, and the main application code continues to be executed. If the database operation takes a lot of time, the application continues to run. Most importantly, users can continue to interact with them without freezing the screen. However, writing asynchronous code may be more complex than other code. This complexity usually occurs when multiple related operations must be assigned to each event listener method.
3. Conceptually, it is easier to encode an operation as a single step sequence (a set of synchronization operations, rather than a group of operations in several event listener methods. In addition to asynchronous database operations, Adobe AIR also allows you to perform database operations synchronously. In synchronous execution mode, operations are not run in the background. Instead, they run in the same execution sequence as all other application code. Notifies the database engine to perform the operation. Then, the code is paused when the database engine is working. After the operation, continue to execute the next line of code.
4. the asynchronous or synchronous operation is set at the sqlconnection level. When a single database connection is used, some operations or statements cannot be executed synchronously and other operations or statements can be executed asynchronously. By calling the sqlconnection method to open a database, you can specify whether the sqlconnection operates in synchronous or asynchronous mode. If sqlconnection. open () is called, the connection operates in synchronous execution mode. If sqlconnection. openasync () is called, the connection operates in asynchronous execution mode. Use open () or openasync () to connect the sqlconnection instance to the database. The instance is fixed to synchronous or asynchronous execution mode unless the connection is closed and re-opened to the database.
Create a database
To create a database file, first create a sqlconnection instance. Call its open () method to open it in synchronous execution mode, or call its openasync () method to open it in asynchronous execution mode. The open () and openasync () methods are used to open the connection to the database. If the passed file instance references a non-existent file location of the reference parameter (the first parameter), the open () or openasync () method creates a database file in the file location, and open the connection to the new database.
Whether you call the open () method or openasync () method to create a database, the database file name can be any valid file name with any file extension. If you call the open () or openasync () method whose reference parameter is null,A new in-memory database will be created, instead of creating a database file on the disk..
The following code list describes how to useAsynchronous executionMode: creates a database file (new database). The database file is stored in the storage directory of the application. The file name is "mytestdb. DB ":
Import MX. Controls. Alert;
Private var con: sqlconnection;
Private function initapp (): void
{
Var file: file = file. applicationstoragedirectory. resolvepath ("mytestdb. DB ")
Con = new sqlconnection ();
// Scheduling when the openasync () method call operation is successful
Con. addeventlistener (sqlevent. Open, openhandler );
// Scheduling error caused by Asynchronous Operation of the sqlconnection object
Con. addeventlistener (sqlerrorevent. error, errorhandler );
Con. openasync (File );
}
Private function openhandler (EVT: sqlevent): void
{
Alert. Show ("completed successfully ");
}
Private function errorhandler (EVT: sqlerrorevent): void
{
Alert. Show ("failed ");
Alert. Show (EVT. Error. Message );
Alert. Show (EVT. Error. Details );
}
YesSynchronous executionWhen you use a sqlconnection instance to open a database connection, call the open () method. The following code creates and enables a sqlconnection instance for Synchronous execution of its operations:
Import MX. Controls. Alert;
Private var con: sqlconnection;
Private function initapp (): void
{
Var file: file = file. applicationstoragedirectory. resolvepath ("mytestdb. DB ")
Con = new sqlconnection ();
Try
{
Con. Open (File );
Alert. Show ("completed successfully ");
}
Catch (error: sqlerror)
{
Alert. Show (error. Message );
Alert. Show (error. Details );
}
}
Create a database table
UseAsynchronous executionCreate a table named "EMP" in the existing database file.
Import MX. Controls. Alert;
Private var con: sqlconnection;
Private var createstmt: sqlstatement;
Private function initapp (): void
{
Var file: file = file. applicationstoragedirectory. resolvepath ("mytestdb. DB ")
Con = new sqlconnection ();
// Scheduling when the openasync () method call operation is successful
Con. addeventlistener (sqlevent. Open, openhandler );
// Scheduling error caused by Asynchronous Operation of the sqlconnection object
Con. addeventlistener (sqlerrorevent. error, errorhandler );
Con. openasync (File );
}
Private function createresult (Event: sqlevent): void
{
Alert. Show ("Table creation ");
}
Private function createerror (Event: sqlerrorevent): void
{
Alert. Show ("error message:", event. Error. Message );
Alert. Show ("details:", event. Error. Details );
}
private function openhandler (EVT: sqlevent): void
{< br> alert. show ("succeeded");
createstmt = new sqlstatement ();
createstmt. sqlconnection = con;
var SQL: String =
"CREATE TABLE if not exists EMP (" +
"empid integer primary key autoincrement, "+
" firstname text, "+
" lastname text, "+
" salary numeric check (salary> 0) "+
") ";
createstmt. TEXT = SQL;
createstmt. addeventlistener (sqlevent. result, createresult);
createstmt. addeventlistener (sqlerrorevent. error, createerror);
createstmt.exe cute ();
}
Private function errorhandler (EVT: sqlerrorevent): void
{
Alert. Show ("failed ");
Alert. Show (EVT. Error. Message );
Alert. Show (EVT. Error. Details );
}
Use the following codeSynchronous executionCreate a table named "EMP" in the existing database file.
Import MX. Controls. Alert;
Private var con: sqlconnection;
Private var createstmt: sqlstatement;
Private function initapp (): void
{
Var file: file = file. applicationstoragedirectory. resolvepath ("mytestdb. DB ")
Con = new sqlconnection ();
Createstmt = new sqlstatement ();
Try
{
Con. Open (File );
Createstmt. sqlconnection = con;
VaR SQL: String =
"Create table if not exists EMP (" +
"Empid integer primary key autoincrement," +
"Firstname text," +
"Lastname text," +
"Salary numeric check (salary> 0)" +
")";
Createstmt. Text = SQL;
Createstmt.exe cute ();
Alert. Show ("completed successfully ");
}
Catch (error: sqlerror)
{
Alert. Show (error. Message );
Alert. Show (error. Details );
}
}
Download Code:
Http://files.cnblogs.com/aierong/Air_Test_SQLite.rar
Favorites and sharing
Add QQ bookmarks to Baidu souzang and Yahoo favorites
RSS subscribe to me What is RSS?