| 1 |
private import std.string; |
|---|
| 2 |
private import std.stdio; |
|---|
| 3 |
|
|---|
| 4 |
import sdbo.odbc; |
|---|
| 5 |
|
|---|
| 6 |
void main() |
|---|
| 7 |
{ |
|---|
| 8 |
OdbcEnvironment env = new OdbcEnvironment; |
|---|
| 9 |
|
|---|
| 10 |
writefln("List of available Driver:"); |
|---|
| 11 |
foreach(char[][2] driver; env.drivers()) |
|---|
| 12 |
writefln("%s - %s", driver[0], driver[1]); |
|---|
| 13 |
|
|---|
| 14 |
writefln(); |
|---|
| 15 |
|
|---|
| 16 |
version(Windows) { |
|---|
| 17 |
char[] constr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=test1.mdb"; |
|---|
| 18 |
} |
|---|
| 19 |
else { |
|---|
| 20 |
char[] constr = "DRIVER=/usr/lib/libmyodbc3.so;HOST=localhost;USER=root"; |
|---|
| 21 |
} |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
OdbcConnection c = env.connectDriver(constr); |
|---|
| 26 |
|
|---|
| 27 |
SqlResult result; |
|---|
| 28 |
|
|---|
| 29 |
char[] createtable = "create table TestCount ( counter_id int, counter_label varchar(50), counter_date date )"; |
|---|
| 30 |
|
|---|
| 31 |
version(linux) { |
|---|
| 32 |
c.execDirect("create database if not exists _sdbo_teste"); |
|---|
| 33 |
c.execDirect("use _sdbo_teste;"); |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
try { |
|---|
| 38 |
result = c.execDirect(createtable); |
|---|
| 39 |
} catch(SqlException e) { |
|---|
| 40 |
|
|---|
| 41 |
if(e.sqlState == "42S01") { |
|---|
| 42 |
result = c.execDirect("drop table TestCount"); |
|---|
| 43 |
result.execDirect(createtable); |
|---|
| 44 |
} else { |
|---|
| 45 |
|
|---|
| 46 |
writefln(e.toString()); |
|---|
| 47 |
writefln("Can't recover. Giving up..."); |
|---|
| 48 |
|
|---|
| 49 |
delete env; |
|---|
| 50 |
return; |
|---|
| 51 |
} |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
version(Windows) { |
|---|
| 55 |
result.execDirect("insert into TestCount values (10, 'counter id is 10', date())"); |
|---|
| 56 |
} else { |
|---|
| 57 |
result.execDirect("insert into TestCount values (10, 'counter id is 10', now())"); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
result.execDirect("select * from TestCount order by counter_id"); |
|---|
| 61 |
|
|---|
| 62 |
result.bindAll = false; |
|---|
| 63 |
result.setBinding(2, result.D_CHAR); |
|---|
| 64 |
|
|---|
| 65 |
result.fetch(); |
|---|
| 66 |
|
|---|
| 67 |
writefln("display a bound column"); |
|---|
| 68 |
writefln("string counter_date: %s", result.string(2)); |
|---|
| 69 |
/* column 2 is bound */ |
|---|
| 70 |
|
|---|
| 71 |
writefln("retrieving unbound columns with longData and stringData"); |
|---|
| 72 |
|
|---|
| 73 |
writefln("long counter_id: %d", result.longData(0)); |
|---|
| 74 |
/* column 0 is unbound */ |
|---|
| 75 |
|
|---|
| 76 |
writefln("string counter_label: %s", result.stringData("counter_label")); |
|---|
| 77 |
/* column counter_label is unbound */ |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
try { |
|---|
| 81 |
/* using getdata twice or out of order might cause a SqlException or return invalid data, |
|---|
| 82 |
depending on the driver */ |
|---|
| 83 |
|
|---|
| 84 |
writefln("long counter_id: %d", result.longData(0)); |
|---|
| 85 |
} catch(SqlException e) { |
|---|
| 86 |
e.print(); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
delete env; |
|---|
| 90 |
|
|---|
| 91 |
} |
|---|