(GO) PhoneGap database detailed

Source: Internet
Author: User

Original: http://firepix.iteye.com/blog/1618343

PhoneGap Database detailed blog classification:

    • Web App
PhoneGap

Today to use phonegap some of the ideas and doubts to write, so that after their own review, interested students can also go to view, can do some reference, I also just learn, so there are divisive parts, you can directly carry out treatise, can also email me: [Email Protected] (OR:935517644QQ)

1,opendatabase

PhoneGap is clearly stated in the official documentation that if you use a database, you first create it with a Window object:

var Dbshell = window.opendatabase (name, version, display_name, size);

Parameters:

    • Name: Identify the database names
    • Version: Revision number
    • Display_name: The display name differs from name in the database table, respectively, with these two fields.
    • Size: Sizes of databases

Detailed

Before using the Factory mode, a database object was created:

function db (name,ver,dis,size) {

.......

var _db = window.opendatabase (name,ver,dis,size);

_db.transaction ();

}

When calling the function method to create a DB object, new db (), the parameters passed in can be four parameters, but I have a problem with this method, first look at the method:

var newdb = {

_db: ",

Db:function () {

if (!newdb._db) {

newdb._db = window.opendatabase ("Database", "1.0", "MyDatabase", 10000);

return new db (newdb._db);

}

}

}

Doubt: When making a call to the factory class to create the database, the passed parameter can not be given four parameters, but directly passed in a database object, and when the database object generated from the factory class, there is also a _db, two databases, Which one is called when the transaction transaction is executed?

It says so much, it's all for laying out:

The process is this:

First, the program creates a folder with the current app package name for the current app under the Data folder

When you run the Window.opendatabase method, a App_database folder is created under the folder ; Create a database.db database file that will create two tables in this database file: Database and origins;database stored in the database, fill in the parameter information, origins in the folder path

Figure 1:database Two tables in a database

Figure 2:databases Table

The Database folder path (Origin field) and file path (path field) created in the databases table are created in file__0/0000000000000001.db by default . This is the database file that we really created. You invoke database objects for database operations that are performed under this database.

When we specify a database that does not exist, it will help us create a new database, and when there is only one existing database object will be returned.

2,sqltransaction Object

The object is used to manipulate the ExecuteSQL method;

Executes the transaction method that, while accepting a SqlTransaction object, also executes the callback function in it:

Db.transaction (Populatedb, ERRORCB, SUCCESSCB);

when you call the transaction method of the database object, its callback function is called and receives a SqlTransaction object. Users can set up a database transaction by calling ExecuteSQL multiple times through the SqlTransaction object.

function Populatedb(TX) {TX.ExecuteSQL( 'DROP TABLE DEMO IF EXISTS' );TX.ExecuteSQL( 'CREATE TABLE IF not EXISTS DEMO (ID unique, data)' );TX.ExecuteSQL( 'INSERT into DEMO (ID, data) VALUES (1, "first row")' );TX.ExecuteSQL( 'INSERT into DEMO (ID, data) VALUES (2, "Second row")' );}function ERRORCB(Err) {   Alert( "Error processing SQL: " +Err);}function SUCCESSCB() {   Alert( "success! ");}var DB = window.OpenDatabase( "Database" ,  "1.0" ,  "PhoneGap Demo" , 200000);DB.Transaction(Populatedb, ERRORCB, SUCCESSCB);

3,sqlresultset Object

Executes the ExecuteSQL method, returning the object: SqlResultSet, the property in the object

    • Insertid: The row ID of the row record that the SqlResultSet object is inserted into the database through the SQL statement. [when inserting multiple rows, return the ID of the last row]
    • rowaffected: The number of record rows that were changed by the SQL statement and set to 0 if the statement did not affect any rows.
    • rows: is a Sqlresultsetrowlist object that represents the multiple records returned. If no records are returned, this object is empty.

The SqlResultSet object can be obtained in the successful callback function that executes ExecuteSQL:

Examples in the reference documentation:tx.executesql (' SELECT * from DEMO ', [], querysuccess, ERRORCB);

The first parameter is the database statement to execute. Not much different from normal manipulation of database statements.

The second parameter is an array object that holds an array of the parameters needed in the SQL statement, which is what appears in the SQL statement? The required parameters.

The third parameter is the method that is called after the successful execution, in which the result set results can be obtained . is a SqlResultSet object

The fourth parameter is the method that is called when an error is executed.

Now let's take a look at the callback method of querysuccess .

function Querysuccess (TX, results) {

// because no record is inserted, the return value is null

Console.log ("Insert ID =" + Results.insertid);

// because this is a query statement, the return value is 0

Console.log ("Rows Affected =" + results.rowaffected);

// Returns the number of record rows to query

Console.log ("Insert ID =" + results.rows.length);

}

4,sqlresultsetlist Object

Contains all the row data returned by the SQL query.

Property:

    • Length: The number of record rows returned by the SQL query.

Method:

    • Item: a JavaScript object that returns a row record based on the specified index.

Summarize:

Db.transaction (SQLTRANSACTION,ERR,SUCSS);

SqlResultSet = Sqltransaction.executesql ("SELECT * from", [],succ,err);

Sqlresultsetlist = sqlresultset.rows;

item = Sqlresultsetlist.item (i);

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.