diff --git a/examples/aggregations.nim b/examples/aggregations.nim new file mode 100644 index 0000000..7195035 --- /dev/null +++ b/examples/aggregations.nim @@ -0,0 +1,32 @@ +import adodb, random, strformat + +let + connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb" + db = newADODB(connect) + tbl = "users" + uid = "user_id" + + +var userId: int = nz(db.dMax(uid, tbl), 0) + +db.exec(sql"DELETE * FROM {tbl}") + +proc iteration(id: var int) = + id.inc + + db.exec(sql"INSERT INTO {tbl} ( {uid}, user_name, user_balance ) VALUES ({id}, 'NEW TEST USER {id}', {rand(1000.0)})") + + let + count = db.dCount(uid, tbl) + sum: float = db.dSum("user_balance", tbl) + avg: float = db.dAvg("user_balance", tbl) + + if count >= 1000: + # keep count of records to not exceed 1000 + let minID = db.dMin(uid, tbl) + db.exec(sql"DELETE * FROM {tbl} WHERE (({uid})={minID})") + + stdout.write('\r', fmt"MaxID: {id:>5}, Count: {count:>4}, Total sum: {sum:>9.2f}, Average: {avg:>6.2f}") + +while true: + iteration(userId) \ No newline at end of file diff --git a/examples/sqloperations.nim b/examples/sqloperations.nim new file mode 100644 index 0000000..9d5c881 --- /dev/null +++ b/examples/sqloperations.nim @@ -0,0 +1,28 @@ +import adodb + +let + connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb" + ado = newADODB(connect) + + prefix = "Test User" + id = 1 + + tbl = "Users" + +ado.exec(sql"DELETE * FROM {tbl}") + +ado.exec(sql"INSERT INTO {tbl} ( user_id, user_name ) VALUES ({id}, '{prefix} {id}')") +ado.exec(sql"INSERT INTO {tbl} ( user_id, user_name ) VALUES ({id + 1}, '{prefix} {id + 1}')") +ado.exec(sql"INSERT INTO {tbl} ( user_id, user_name ) VALUES ({id + 2}, '{prefix} {id + 2}')") +ado.exec(sql"INSERT INTO {tbl} ( user_id, user_name ) VALUES ({id + 3}, '{prefix} {id + 3}')") + +let updateSQL = sql"UPDATE {tbl} SET" +ado.exec(sql"{updateSQL} user_bday=#31.12.1999# WHERE ((user_id)={id})") # dd.mm.yyyy +ado.exec(sql"{updateSQL} user_bday=#{1985, 8, 22} WHERE ((user_id)={id + 1})") # mm/dd/yyyy +ado.exec(sql"{updateSQL} user_bday=#1987-11-15# WHERE ((user_id)={id + 2})") # yyyy-mm-dd + +let rst = ado.query($&"SELECT * FROM {tbl}") + +for rowIdx, row in rst: + for idx, name, fld in row.fields: + echo "Row ", rowIdx, ", Fld[", idx," | ", name, "] = ", nz(fld, "!!! Not set !!!") diff --git a/examples/test.mdb b/examples/test.mdb new file mode 100644 index 0000000..0ad6b77 Binary files /dev/null and b/examples/test.mdb differ