Write here your abstract
1 Insert
The INSERT statement is used to insert one or more rows into a table or view.
It has the following syntax:
Copy
<insert table='table' columns='columns' prefix='prefix' > <column name='name' value='value' /> * <select /> ? </insert>
Copy
Ax.db.insert(String table, myObject);
Copy
<xsql-script> <body> <nativesql> !CREATE TABLE test_city ( codigo serial not null, ciudad char(20) not null, pais char(20) not null ); </nativesql> <set name='test_city_codigo'>0</set> <set name='test_city_ciudad'>LIMA</set> <set name='test_city_pais'>PERU</set> <println>Table before of the insert</println> <print.sql head='true'> <select> <columns>*</columns> <from table='test_city' /> </select> </print.sql> <insert table='test_city' prefix='test_city_' /> <println>Table after of the insert</println> <print.sql head='true'> <select> <columns>*</columns> <from table='test_city' /> </select> </print.sql> <nativesql> !DROP TABLE test_city; </nativesql> </body> </xsql-script>
Table before of the insert
+----------+--------------------+--------------------+
|codigo |ciudad |pais |
+----------+--------------------+--------------------+
Table after of the insert
+----------+--------------------+--------------------+
|codigo |ciudad |pais |
+----------+--------------------+--------------------+
|1 |LIMA |PERU |
+----------+--------------------+--------------------+
Copy
Ax.db.execute(` CREATE TABLE test_city ( codigo serial not null, ciudad char(20), pais char(20) )`); var mRowCity = { codigo : 0, ciudad : 'LIMA', pais : 'PERU' }; var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table before of the insert`); console.log(mRsCity); mRsCity.close(); Ax.db.insert("test_city",mRowCity) var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table after of the insert`); console.log(mRsCity); mRsCity.close(); Ax.db.execute(`DROP TABLE IF EXISTS test_city`);
Table before of the insert
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
Table after of the insert
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |LIMA |PERU |
+----------+--------------------+--------------------+
Copy
var mRowsCity = [ { codigo: 0, ciudad:'Lima', pais:'Peru' }, { codigo: 0, ciudad:'Miami', pais:'EEUU' }, { codigo: 0, ciudad:'Mexico', pais:'Mexico' } ]; Ax.db.execute(` CREATE TABLE test_city ( codigo serial not null, ciudad char(20), pais char(20) )`); var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table before of the insert`); console.log(mRsCity); mRsCity.close(); Ax.db.insert("test_city", mRowsCity); var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table after of the insert`); console.log(mRsCity); mRsCity.close(); Ax.db.execute(`DROP TABLE IF EXISTS test_city`);
Table before of the insert
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
Table after of the insert
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |Lima |Peru |
|2 |Miami |EEUU |
|3 |Mexico |Mexico |
+----------+--------------------+--------------------+
Getting the serial of the inserted row
Copy
<xsql-script> <body> <nativesql> !CREATE TABLE test_city ( codigo serial not null, ciudad char(20) not null, pais char(20) not null ); </nativesql> <set name='test_city_codigo'>0</set> <set name='test_city_ciudad'>LIMA</set> <set name='test_city_pais'>PERU</set> <insert table='test_city' prefix='test_city_' /> <println>Table after of the insert</println> <print.sql head='true'> <select> <columns>*</columns> <from table='test_city' /> </select> </print.sql> <nativesql> !DROP TABLE test_city; </nativesql> <println>Serial: <sqlca.serial /></println> </body> </xsql-script>
Table after of the insert
+----------+--------------------+--------------------+
|codigo |ciudad |pais |
+----------+--------------------+--------------------+
|1 |LIMA |PERU |
+----------+--------------------+--------------------+
Serial: 1
Copy
Ax.db.execute(` CREATE TABLE test_city ( codigo serial not null, ciudad char(20), pais char(20) )`); var mRowCity = { codigo : 0, ciudad : 'LIMA', pais : 'PERU' }; var mIntSerial = Ax.db.insert("test_city",mRowCity).getSerial(); console.log(`Table after of the insert`); var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(mRsCity); mRsCity.close(); Ax.db.execute(`DROP TABLE IF EXISTS test_city`); console.log(`Serial: ${mIntSerial}`);
Table after of the insert
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |LIMA |PERU |
+----------+--------------------+--------------------+
Serial: 1
2 Delete
Delete records from a table.
It has the following syntax:
Copy
<delete table='table'> <optimizer-directives /> * <where condition='condition' /> ? </delete>
Copy
Ax.db.delete(String table, String condition)
Copy
<xsql-script> <body> <nativesql> !CREATE TABLE test_city ( codigo serial not null, ciudad char(20) not null, pais char(20) not null ); </nativesql> <for name='m_i' start='1' end='5'> <do> <set name='mCiudad'><string>Ciudad<m_i /></string></set> <set name='mPais'><string>Pais<m_i /></string></set> <insert table='test_city'> <column name='codigo'>0</column> <column name='ciudad'><mCiudad /></column> <column name='pais'><mPais /></column> </insert> </do> </for> <println>Table before of the delete</println> <print.sql head='true'> <select> <columns>*</columns> <from table='test_city' /> </select> </print.sql> <delete table='test_city'> <where> codigo = 5 </where> </delete> <println>Table after of the delete</println> <print.sql head='true'> <select> <columns>*</columns> <from table='test_city' /> </select> </print.sql> <nativesql> !DROP TABLE test_city; </nativesql> </body> </xsql-script>
Table before of the delete
+----------+--------------------+--------------------+
|codigo |ciudad |pais |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad5 |Pais5 |
+----------+--------------------+--------------------+
Table after of the delete
+----------+--------------------+--------------------+
|codigo |ciudad |pais |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
+----------+--------------------+--------------------+
Copy
Ax.db.execute(` CREATE TABLE test_city ( codigo serial not null, ciudad char(20), pais char(20) )`); for(var i=0; i<5; i++) { var mRowCity = { codigo : 0, ciudad : `Ciudad${i + 1}`, pais : `Pais${i + 1}` }; Ax.db.insert("test_city",mRowCity); } var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table before of the delete`); console.log(mRsCity); mRsCity.close(); Ax.db.delete('test_city', { codigo : 5 } ); var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table after of the delete`); console.log(mRsCity); mRsCity.close(); Ax.db.execute(`DROP TABLE IF EXISTS test_city`);
Table before of the delete
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad5 |Pais5 |
+----------+--------------------+--------------------+
Table after of the delete
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
+----------+--------------------+--------------------+
Copy
Ax.db.execute(` CREATE TABLE test_city ( codigo serial not null, ciudad char(20), pais char(20) )`); for(var i=0; i<5; i++) { var mRowCity = { codigo : 0, ciudad : `Ciudad${i + 1}`, pais : `Pais${i + 1}` }; Ax.db.insert("test_city",mRowCity); } var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table before of the delete`); console.log(mRsCity); mRsCity.close(); Ax.db.delete("test_city", { "codigo": 1 }); Ax.db.delete("test_city", "codigo = 3"); Ax.db.delete("test_city", { "codigo": 5 }, "pais = 'Pais5'"); var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table after of the delete`); console.log(mRsCity); mRsCity.close(); Ax.db.execute(`DROP TABLE IF EXISTS test_city`);
Table before of the delete
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad5 |Pais5 |
+----------+--------------------+--------------------+
Table after of the delete
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|2 |Ciudad2 |Pais2 |
|4 |Ciudad4 |Pais4 |
+----------+--------------------+--------------------+
3 Update
The UPDATE statement allows you to modify records in a table.
It has the following syntax:
Copy
<update table='table'> <column name='name' text='text' /> + <where /> ? </update>
Copy
Ax.db.update(String table, JSON row, String condition)
Copy
<xsql-script> <body> <nativesql> !CREATE TABLE test_city ( codigo serial not null, ciudad char(20) not null, pais char(20) not null ); </nativesql> <for name='m_i' start='1' end='5'> <do> <set name='mCiudad'><string>Ciudad<m_i /></string></set> <set name='mPais'><string>Pais<m_i /></string></set> <insert table='test_city'> <column name='codigo'>0</column> <column name='ciudad'><mCiudad /></column> <column name='pais'><mPais /></column> </insert> </do> </for> <println>Table before of the update</println> <print.sql head='true'> <select> <columns>*</columns> <from table='test_city' /> </select> </print.sql> <update table='test_city'> <column name='ciudad'><string>Ciudad6</string></column> <column name='pais'><string>Pais6</string></column> <where> codigo = 5 </where> </update> <println>Table after of the update</println> <print.sql head='true'> <select> <columns>*</columns> <from table='test_city' /> </select> </print.sql> <nativesql> !DROP TABLE test_city; </nativesql> </body> </xsql-script>
Table before of the update
+----------+--------------------+--------------------+
|codigo |ciudad |pais |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad5 |Pais5 |
+----------+--------------------+--------------------+
Table after of the update
+----------+--------------------+--------------------+
|codigo |ciudad |pais |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad6 |Pais6 |
+----------+--------------------+--------------------+
Copy
Ax.db.execute(` CREATE TABLE test_city ( codigo serial not null, ciudad char(20), pais char(20) )`); for(var i=0; i<5; i++) { var mRowCity = { codigo : 0, ciudad : `Ciudad${i + 1}`, pais : `Pais${i + 1}` }; Ax.db.insert("test_city",mRowCity); } var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table before of the update`); console.log(mRsCity); mRsCity.close(); Ax.db.update('test_city', { ciudad : 'Ciudad6', pais : 'Pais6' }, { codigo : 5 } ); var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table after of the update`); console.log(mRsCity); mRsCity.close(); Ax.db.execute(`DROP TABLE IF EXISTS test_city`);
Table before of the update
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad5 |Pais5 |
+----------+--------------------+--------------------+
Table after of the update
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad6 |Pais6 |
+----------+--------------------+--------------------+
Copy
Ax.db.execute(` CREATE TABLE test_city ( codigo serial not null, ciudad char(20), pais char(20) )`); for(var i=0; i<5; i++) { var mRowCity = { codigo : 0, ciudad : `Ciudad${i + 1}`, pais : `Pais${i + 1}` }; Ax.db.insert("test_city",mRowCity); } var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table before of the update`); console.log(mRsCity); mRsCity.close(); Ax.db.update("test_city", { "ciudad" : "Ciudad6" , "codigo": 1 }); Ax.db.update("test_city", { "ciudad" : "Ciudad7", "pais" : "Pais7" }, "codigo = 2"); Ax.db.update("test_city", { "ciudad" : "Ciudad8", "pais" : "Pais8" }, { "codigo" : 3} ); Ax.db.update("test_city", { "ciudad" : "Ciudad9", "pais" : "Pais9" }, { "codigo": 4 }, "pais = 'Pais4'"); var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table after of the update`); console.log(mRsCity); mRsCity.close(); Ax.db.execute(`DROP TABLE IF EXISTS test_city`);
Table before of the update
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad5 |Pais5 |
+----------+--------------------+--------------------+
Table after of the update
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |Ciudad6 |Pais1 |
|2 |Ciudad7 |Pais7 |
|3 |Ciudad8 |Pais8 |
|4 |Ciudad9 |Pais9 |
|5 |Ciudad5 |Pais5 |
+----------+--------------------+--------------------+
4 Update or Insert statements
Allows you to modify a record of a table, and if it does not exist, it inserts it.
Copy
<update-or-insert> <update /> + <insert /> + </update-or-insert>
Copy
Ax.db.updateOrInsert(String table, JSON row);
When the row exists
Copy
<xsql-script> <body> <nativesql> !CREATE TABLE test_city ( codigo serial not null, ciudad char(20) not null, pais char(20) not null ); </nativesql> <for name='m_i' start='1' end='5'> <do> <set name='mCiudad'><string>Ciudad<m_i /></string></set> <set name='mPais'><string>Pais<m_i /></string></set> <insert table='test_city'> <column name='codigo'>0</column> <column name='ciudad'><mCiudad /></column> <column name='pais'><mPais /></column> </insert> </do> </for> <println>Table before of the update-or-insert</println> <print.sql head='true'> <select> <columns>*</columns> <from table='test_city' /> </select> </print.sql> <update-or-insert> <update table="test_city"> <column name="ciudad">Ciudad6</column> <column name="pais">Pais6</column> <where> codigo = '5' </where> </update> <insert table="test_city"> <column name="ciudad">Ciudad6</column> <column name="pais">Pais6</column> </insert> </update-or-insert> <println>Table after of the update-or-insert</println> <print.sql head='true'> <select> <columns>*</columns> <from table='test_city' /> </select> </print.sql> <nativesql> !DROP TABLE test_city; </nativesql> </body> </xsql-script>
Table before of the update-or-insert
+----------+--------------------+--------------------+
|codigo |ciudad |pais |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad5 |Pais5 |
+----------+--------------------+--------------------+
Table after of the update-or-insert
+----------+--------------------+--------------------+
|codigo |ciudad |pais |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad6 |Pais6 |
+----------+--------------------+--------------------+
Copy
Ax.db.execute(` CREATE TABLE test_city ( codigo serial not null, ciudad char(20), pais char(20) )`); for(var i=0; i<5; i++) { var mRowCity = { codigo : 0, ciudad : `Ciudad${i + 1}`, pais : `Pais${i + 1}` }; Ax.db.insert("test_city",mRowCity); } var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table before of the update-or-insert`); console.log(mRsCity); mRsCity.close(); var rowCity={ codigo : 5, ciudad : "Ciudad6", pais : "Pais6" }; Ax.db.updateOrInsert('test_city', rowCity); var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table after of the update-or-insert`); console.log(mRsCity); mRsCity.close(); Ax.db.execute(`DROP TABLE IF EXISTS test_city`);
Table before of the update-or-insert
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad5 |Pais5 |
+----------+--------------------+--------------------+
Table after of the update-or-insert
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad6 |Pais6 |
+----------+--------------------+--------------------+
When the row no exists
Copy
<xsql-script> <body> <nativesql> !CREATE TABLE test_city ( codigo serial not null, ciudad char(20) not null, pais char(20) not null ); </nativesql> <for name='m_i' start='1' end='5'> <do> <set name='mCiudad'><string>Ciudad<m_i /></string></set> <set name='mPais'><string>Pais<m_i /></string></set> <insert table='test_city'> <column name='codigo'>0</column> <column name='ciudad'><mCiudad /></column> <column name='pais'><mPais /></column> </insert> </do> </for> <println>Table before of the update-or-insert</println> <print.sql head='true'> <select> <columns>*</columns> <from table='test_city' /> </select> </print.sql> <update-or-insert> <update table="test_city"> <column name="ciudad">Ciudad6</column> <column name="pais">Pais6</column> <where> codigo = '6' </where> </update> <insert table="test_city"> <column name="ciudad">Ciudad6</column> <column name="pais">Pais6</column> </insert> </update-or-insert> <println>Table after of the update-or-insert</println> <print.sql head='true'> <select> <columns>*</columns> <from table='test_city' /> </select> </print.sql> <nativesql> !DROP TABLE test_city; </nativesql> </body> </xsql-script>
Table before of the update-or-insert
+----------+--------------------+--------------------+
|codigo |ciudad |pais |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad5 |Pais5 |
+----------+--------------------+--------------------+
Table after of the update-or-insert
+----------+--------------------+--------------------+
|codigo |ciudad |pais |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad5 |Pais5 |
|6 |Ciudad6 |Pais6 |
+----------+--------------------+--------------------+
Copy
Ax.db.execute(` CREATE TABLE test_city ( codigo serial not null, ciudad char(20), pais char(20) )`); for(var i=0; i<5; i++) { var mRowCity = { codigo : 0, ciudad : `Ciudad${i + 1}`, pais : `Pais${i + 1}` }; Ax.db.insert("test_city",mRowCity); } var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table before of the update-or-insert`); console.log(mRsCity); mRsCity.close(); var rowCity={ codigo : 6, ciudad : "Ciudad6", pais : "Pais6" }; Ax.db.updateOrInsert('test_city', rowCity); var mRsCity = Ax.db.executeQuery(`SELECT * FROM test_city`); console.log(`Table after of the update-or-insert`); console.log(mRsCity); mRsCity.close(); Ax.db.execute(`DROP TABLE IF EXISTS test_city`);
Table before of the update-or-insert
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad5 |Pais5 |
+----------+--------------------+--------------------+
Table after of the update-or-insert
+----------+--------------------+--------------------+
|test_city |test_city |test_city |
|codigo |ciudad |pais |
|serial |char(20) |char(20) |
+----------+--------------------+--------------------+
|1 |Ciudad1 |Pais1 |
|2 |Ciudad2 |Pais2 |
|3 |Ciudad3 |Pais3 |
|4 |Ciudad4 |Pais4 |
|5 |Ciudad5 |Pais5 |
|6 |Ciudad6 |Pais6 |
+----------+--------------------+--------------------+