How to read data in SQL database
This article introduces the knowledge of "how to read data from SQL database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Perform query operation
To perform the operation, use the database.transaction () function:
Var db = openDatabase ('mydb',' 1.0, 'Test DB', 2 * 1024 * 1024)
Db.transaction (function (tx) {
Tx.executeSql ('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)')
});
The above statement, when executed, creates a table named LOGS in the 'mydb' database.
Insert data
After executing the above create table statement, we can insert some data:
Var db = openDatabase ('mydb',' 1.0, 'Test DB', 2 * 1024 * 1024)
Db.transaction (function (tx) {
Tx.executeSql ('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)')
Tx.executeSql ('INSERT INTO LOGS (id, log) VALUES (1, "Rookie tutorial"))
Tx.executeSql ('INSERT INTO LOGS (id, log) VALUES (2, "www.runoob.com"))
});
We can also use dynamic values to insert data:
Var db = openDatabase ('mydb',' 1.0, 'Test DB', 2 * 1024 * 1024)
Db.transaction (function (tx) {
Tx.executeSql ('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)')
Tx.executeSql ('INSERT INTO LOGS (id,log) VALUES', [e_id, e_log])
});
E_id and e_log in the instance are external variables, and executeSql maps each entry in the array parameter to "?".
Read data
The following example shows how to read data that already exists in the database:
Var db = openDatabase ('mydb',' 1.0, 'Test DB', 2 * 1024 * 1024)
Db.transaction (function (tx) {
Tx.executeSql ('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)')
Tx.executeSql ('INSERT INTO LOGS (id, log) VALUES (1, "Rookie tutorial"))
Tx.executeSql ('INSERT INTO LOGS (id, log) VALUES (2, "www.runoob.com"))
});
Db.transaction (function (tx) {
Tx.executeSql ('SELECT * FROM LOGS', [], function (tx, results) {
Var len = results.rows.length, I
Msg = "
Number of query records: "+ len +"
"
Document.querySelector ('# status') [xss_clean] + = msg
For (I = 0; I < len; iTunes +) {
Alert (results.rows.item (I) .log)
}
}, null)
});
This is the end of the content of "how to read data from SQL database". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!