SQLtie adds and deletes the query

Create table, add data, update data, delete data, delete table.

First introduce three core methods

1, openDatabase: This method uses an existing database or creates a new database to create a database object.

2, transaction: This method allows us to control the transaction commit or rollback according to the situation.

3. executeSql: This method is used to execute real SQL queries.

Step 1: Open the connection and create the database

copy code
code show as below:

var dataBase = openDatabase("student", "1.0", "student table", 1024 * 1024, function () { });
if (!dataBase) {
alert("Database creation failed!");
} else {
alert( "Database created successfully!");
}



Explain that the openDatabase method opens an existing database, and if the database does not exist, it can also create the database. The meanings of several parameters are:
1. Database name.
2. The version number is currently 1.0, regardless of him, it is OK to write to death.
3, the description of the database.
4. Set the size of the data.
5. Callback function (can be omitted).
The database is created when the first call is made, and the connection is established later.
The created database exists locally, and the path is as follows:
C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default\databases\http_localhost_4987 .
Created is a sqllite database, you can open the file with SQLiteSpy, you can see the data inside. SQLiteSpy is a green software, you can download it from Baidu or the official download of SQLiteSpy: SQLiteSpy .

 
Step 2: Create the data table

copy code
code show as below:

this.createTable=function() {
dataBase.transaction( function(tx) {
tx.executeSql(
"create table if not exists stu (id REAL UNIQUE, name TEXT)",
[],
function(tx,result){ alert('创建stu表成功'); },
function(tx, error){ alert('创建stu表失败:' + error.message);
});
});
}



Explain, the
executeSql function has four parameters, and their meanings are:
1) A string representing the query, and the SQL language used is SQLite 3.6.19.
2) String data inserted into the query where the question mark is.
3) The callback function that is executed on success. Returns two parameters: tx and the result of the execution.
4) A callback function that is executed on failure. Returns two parameters: tx and the failure error message.

 

Step 3: Execute additions, deletions, changes, and inquiries

1) Add data:

copy code
code show as below:

this.insert = function () {
dataBase.transaction(function (tx) {
tx.executeSql(
"insert into stu (id, name) values(?, ?)",
[id, 'Xu Mingxiang'],
function () { alert('Added data successfully'); },
function (tx, error) { alert('Added data failed: ' + error.message);
} );
});



 
2) Query data

copy code
code show as below:

this.query = function () {
dataBase.transaction(function (tx) {
tx.executeSql(
"select * from stu", [],
function (tx, result) { //Callback function for successful execution
//here result do what you want....
},
function (tx, error) {
alert('Query failed: ' + error.message);
} );
});
}



Explain that
the successful callback function in the above code has a parameter result.

result: The queried dataset. Its data type is SQLResultSet, just like DataTable in C#.
SQLResultSet is defined as:

copy code
code show as below:

interface SQLResultSet {
readonly attribute long insertId;
readonly attribute long rowsAffected;
readonly attribute SQLResultSetRowList rows;
};


One of the most important properties—rows of type SQLResultSetRowList—is the "rows" of the dataset.
rows has two properties: length, item.
Therefore, get the value of a certain row and a certain column of the query result: result.rows[i].item[fieldname].

3) Update data


copy code
code show as below:

this.update = function (id, name) {
dataBase.transaction(function (tx) {
tx.executeSql(
"update stu set name = ? where id= ?",
[name, id],
function (tx, result) {
},
function (tx, error) {
alert('更新失败: ' + error.message);
});
});
}



4) Delete data


copy code
code show as below:

this.del = function (id) {
dataBase.transaction(function (tx) {
tx.executeSql(
"delete from stu where id= ?",
[id],
function (tx, result) {
},
function (tx, error) {
alert('删除失败: ' + error.message);
});
});
}




5) Delete the data table

copy code
code show as below:


this.dropTable = function () {
dataBase.transaction(function (tx) {
tx.executeSql('drop table stu');
});
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325182291&siteId=291194637