Storage in phonegap

Source: Internet
Author: User
Tags sql error

Storage-related objects in phonegap include
Database objects
SQLTransaction object
SQLResultSet SQL result object
SQLResultSetList
SQLError SQL error object
LocalStorage local storage objects

Database objects:
Obtain the object using the openDatabase method.
For example:
Window. openDatabase (database_name, database_version, database_displayname, database_size );

Var dbOne = window. openDatabase ("test1", "1.0", "Test DB", 1000000 );

Database_name: Database Name,
Database_version: database version
Database_displayname: Display name
Database_size: database size

After the database is created, the created database file is found in/data/package name/app_database/dbOne. db. You can view the file by using DDMS.

SQLTransaction object
Phonegap does not provide a method to directly obtain the object. Instead, it uses the Database object transaction method to pass the object to a callback.
Function, such
DbOne. transaction (createATable, errorCreateTable, successCreateTable );

CreateATable is a callback function that transfers the object in the form of parameters. The createATable Function

CreateATable (trans ){
}
The trans here are the objects passed in. With the object, you can use the executeSql method of the object to execute SQL statements.

For example
CreateATable (trans ){
Trans.exe cuteSql ('create table if not exists MyTab (id unique, data )');
Tx.exe cuteSql ('insert INTO MyTab (id, data) VALUES (1, "First row ")');
Tx.exe cuteSql ('insert into demo (id, data) VALUES (2, "Second row ")');

}
In this way, the MyTab table is created and two pieces of data are inserted using the transaction object of the database.

SQLError error object
The SQLError object is also passed to a callback function as a parameter.
Method above
DbOne. transaction (createATable, errorCreateTable, successCreateTable );
ErrorCreateTable is a callback function. If dbOne. transaction fails to be executed, the callback function errorCreateTable is called.
At the same time, the SQLError object is passed in
For example
Function errorCreateTable (err)
{
Alert ("err code:" + err. code + "err message:" + err. message ');
}

Code and message are two attributes of the SQLError object.

SQLResultSet object


This object is passed to the callback function by the executeSql method of the transaction object. In the callback function, operations are performed on the structure set object, such
Tx.exe cuteSql ('select * FROM mytab', [], querySuccess, errorCB );

QuerySuccess is the callback function after successful execution,

Function querySuccess (trans, results ){
Alert ("Returned rows =" + results. rows. length );

If (! ResultSet. rowsAffected ){
Alert ('no rows affected! ');
Return false;
}
This function accepts two parameters: the transaction object and the result set object SQLResultSet. SQLResultSet contains three attributes.
The row ID of the inserted data row of the insertId function.
Number of data rows changed by rowsAffected
Rows: rows is a SQLResultSetList object that represents all data rows returned during SQL queries.

SQLResultSetList
This object contains an attribute length (number of returned data rows) and a method item (this method returns a specific data row 0

Example
Function querySuccess (trans, results ){
Var len = results. rows. length;
Console. log ("MyTab table:" + len + "rows found .");
For (var I = 0; I <len; I ++ ){
Console. log ("Row =" + I + "ID =" + results. rows. item (I ). id + "Data =" + results. rows. item (I ). data );
}
}

LocalStorage local storage objects

This object has nothing to do with the storage on the mobile phone device, but is an interface provided by the local storage in html5

A complete example

Html section

[Html]
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> new document </title>
</Head>
<Body>
<Button onclick = "pgapStorage ()"> pgapStorage </button>
</Body>
<Script type = "text/javascript" src = "cordova-2.1.0.js"> </script>
<Script type = "text/javascript" src = "pgapstorage. js"> </script>
</Html>

Js Section

[Html]
Function pgapStorage (){
// Create or open a database. The size is 100000 bytes.
Var dbOne = window. openDatabase ("myDataBase", "1.0", "phonegapdatabase", 100000 );

// Pass the SQLTransaction object to the createATable function. If an error occurs, send the SQLError object to the errorCreateTable function.
DbOne. transaction (createATable, errorCreateTableTrans, successCreateTableTrans );
}
// Execute the callback function createATable to create the MyTab table and insert data.
Function createATable (trans ){
Trans.exe cuteSql ('drop table if exists mytab ');
Trans.exe cuteSql ('create table if not exists MyTab (id unique, data )');
Trans.exe cuteSql ('insert INTO MyTab (id, data) VALUES (1, "First row ")');
Trans.exe cuteSql ('insert INTO MyTab (id, data) VALUES (2, "Second row ")');
}
// Run dbOne. transaction () after the execution fails and pass the SQLError object
Function errorCreateTableTrans (err ){
Alert ("err code:" + err. code + "err message:" + err. message );
}
 
// After dbOne. transaction () is successfully executed
Function successCreateTableTrans (){
Alert ("successfully ");
Var dbOne = window. openDatabase ("myDataBase", "1.0", "phonegapdatabase", 100000 );
// Create a new object and pass it to the callback function
DbOne. transaction (queryMyTab, successQueryMyTabTrans, errorQueryMyTabTrans );
}
 
Function successQueryMyTabTrans (){
Alert ("successQueryMyTabTrans ");
}
Function errorQueryMyTabTrans (){
Alert ("err code:" + err. code + "err message:" + err. message );
}
// Query
Function queryMyTab (trans ){
// After the query is successful, run the successQueryMyTab callback function. The errorQueryMyTab function fails to be executed.
Trans.exe cuteSql ('select * FROM mytab', [], successQueryMyTab, errorQueryMyTab );
}
// Execute the executeSql statement after it is executed successfully, and pass the SQLTransaction and SQLResultSet objects to the successQueryMyTab function.
Function successQueryMyTab (trans, results ){
Var len = results. rows. length;
Alert (len );
// Use the pop-up box to display the query results
For (var I = 0; I <len; I ++ ){
Alert ("Row =" + I + "ID =" + results. rows. item (I ). id + "Data =" + results. rows. item (I ). data );
}
}
// Execute after executeSql () fails to be executed
Function errorQueryMyTab (err ){
Alert ("err code:" + err. code + "err message:" + err. message );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.