2
0

intro.bbdoc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <img src="database_logo.png" align="right" />
  2. <p>
  3. The Database Framework module is the core of a set of cross-platform modules to allow you to connect
  4. to a variety of different databases using a standard set of Types and Functions.<br>
  5. Using a standard framework means that you don't have to learn or know anything about the underlying database API, since the framework takes care of all the nitty-gritty for you. This way, you only have to worry about the data and the SQLs to access it.
  6. </p>
  7. <p>Most databases use, or try to be close to the SQL92 (ANSI-standard) specification, which means that generally, you can re-use the same SQL statements on different databases without worrying about it. Obviously, if you decide to use database-specific SQL, you will have to be careful if you decide you want to then use a different type of database.
  8. </p>
  9. <p>Since there are many good online resources, tutorials, and books available that discuss and teach SQL, its use won't be described in great detail in this documentation. We leave it up to you to find out what you need to know ;-)
  10. </p>
  11. <h2>Getting Started</h2>
  12. <p>To connect to a database you need one of the available Database Driver modules.<br>
  13. As of this version there are currently drivers available for :
  14. <ul>
  15. <li>mSQL - (<a href="../../dbmsql.mod/doc/commands.html"> Docs </a>)</li>
  16. <li>MySQL - (<a href="../../dbmysql.mod/doc/commands.html"> Docs </a>)</li>
  17. <li>ODBC - (<a href="../../dbodbc.mod/doc/commands.html"> Docs </a>)</li>
  18. <li>PostgreSQL - (<a href="../../dbpostgresql.mod/doc/commands.html"> Docs </a>)</li>
  19. <li>SQLite - (<a href="../../dbsqlite.mod/doc/commands.html"> Docs </a>)</li>
  20. <li>Xbase - (<a href="../../dbxbase.mod/doc/commands.html"> Docs </a>)</li>
  21. </ul>
  22. </p>
  23. <h3>Opening a Connection</h3>
  24. <p>A <a href="#TDBConnection">TDBConnection</a> object is your interface to the database.<br>
  25. To create one, you should use the <a href="#LoadDatabase">LoadDatabase</a> function, passing in the relevant parameters. The most important parameter is <i>dbType</i>, which tells the Framework which kind of driver it should load for this connection.<br>
  26. It is much like the way other "loaders" work in BlitzMax, allowing you connect to several different types of database within the same application.<br>
  27. <a href="#LoadDatabase">LoadDatabase</a> takes other parameters, each of which may or may not be applicable for a certain driver - see the driver documentation for details.
  28. </p>
  29. <p>
  30. <a href="#LoadDatabase">LoadDatabase</a> will return Null if no valid driver was found.</p>
  31. <p>If you've provided enough information, the Framework will try to open a connection
  32. to the database for you. You can check both <a href="#hasError">hasError</a> and the <a href="#isOpen">isOpen</a> method on the connection
  33. to determine whether or not it succeeded.
  34. </p>
  35. <h3>Communicating with the database</h3>
  36. <p>Once a connection is open, it's time to start working with the database.</p>
  37. <p>The Framework has two ways of performing actions on a database.<br>
  38. The first is to simply execute a query. The second is to prepare the query, and then execute it.</p>
  39. <p>The first method works like this:
  40. <pre>db.<a href="#executeQuery">executeQuery</a>("DROP TABLE if exists person")</pre>
  41. The statement is executed immediately on the database.</p>
  42. <p> The second method, prepare then execute, requires a bit more work to use, but the advantage over the first method is that although the initial prepare may be relatively slow, it allows multiple subsequent executions of the SQL without having to re-process it each time. (and is therefore more efficient over all)
  43. </p>
  44. <p>
  45. You begin by creating a <a href="#TDatabaseQuery">TDatabaseQuery</a> object,
  46. <pre>Local query:TDatabaseQuery = TDatabaseQuery.Create(db)</pre>
  47. The next step is to prepare the query,
  48. <pre>query.<a href="#prepare">prepare</a>("INSERT INTO person values (NULL, ?, ?)")</pre>
  49. </p>
  50. <p>
  51. With prepared statements/queries you can use placeholders to represent a value that you want to use when you execute it, much like a program variable. In the example above, there are two placeholders, specified by question marks. (<b>Note</b>: Check the database driver documentation for details of placeholder formats)
  52. </p>
  53. <p>
  54. Before the executing the query you need to bind each placeholder with a value. For example:
  55. <pre>
  56. For Local i:Int = 0 Until myArray.length
  57. query.<a href="#setString">setString</a>(0, myArray[i].forename)
  58. query.<a href="#setString">setString</a>(1, myArray[i].surname)
  59. query.<a href="#execute">execute</a>()
  60. Next
  61. </pre>
  62. As you can see, for each new "insertion" we bind a new piece of data to each placeholder. The execution itself is very fast because the SQL has already been prepared.<br>
  63. The <b>add&lt;dbtype&gt;()</b> methods are also available for the supported types, which adds a new bind value to the end of the bindings. (see <a href="#addString">addString</a>, <a href="#addInt">addInt</a>, <a href="#addLong">addLong</a>, <a href="#addFloat">addFloat</a> and <a href="#addDouble">addDouble</a>).
  64. </p>
  65. <p>
  66. For SELECT statements, the TDBConnection <a href="#executeQuery">executeQuery</a> method also returns a <a href="#TDatabaseQuery">TDatabaseQuery</a> object.<br>
  67. A TDatabaseQuery object can be used to process all the rows of data returned from the SELECT. For example:
  68. </p>
  69. <pre>Local query:TDatabaseQuery = db.executeQuery("SELECT * FROM person")</pre>
  70. or, for prepared queries,
  71. <pre>query.execute()</pre>
  72. <p>
  73. There are two ways to get the row data from the SELECT.
  74. <ul>
  75. <li>Use the TDatabaseQuery <a href="#nextRow">nextRow</a> method, which fetches the next row, returning True if the fetch was successful, or False if there is no more data.<br>
  76. Once a row is fetched, you can access the row record using the TDatabaseQuery <a href="#rowRecord">rowRecord</a> method.
  77. <pre>While query.<a href="#nextRow">nextRow</a>()
  78. Local record:<a href="#TQueryRecord">TQueryRecord</a> = query.<a href="#rowRecord">rowRecord</a>()
  79. ' ...
  80. Wend</pre>
  81. </li>
  82. <li>Use the &quot;EachIn&quot; support, as you would for a TList. For example:
  83. <pre>For Local record:<a href="#TQueryRecord">TQueryRecord</a> = EachIn query
  84. ' ...
  85. Next</pre>
  86. </li>
  87. </ul>
  88. </p>
  89. <p>The <a href="#rowsAffected">rowsAffected</a> method can be used to determine the number of rows affected by a delete, insert or update.
  90. </p>
  91. <h3>Transactions</h3>
  92. <p>
  93. Most modern databases support transactions of some kind.<br>
  94. A transaction is a block of work that doesn't become finalized on the database until you
  95. commit it. If at some point you want to cancel the transaction, you can "roll" it back to
  96. the state it was in before you started. This means that you won't have half-processed changes
  97. in your data if the server/connection goes down half-way through.
  98. </p>
  99. <p>To begin a transaction, you can use the <a href="#startTransaction">startTransaction</a> method.<br>
  100. Once the transaction is started, you should end it by calling either <a href="#commit">commit</a> or <a href="#rollback">rollback</a>.<br>
  101. If you close the connection before ending the transaction, the state of the transaction is undetermined - see the specific database documentation for details. It is better to end the transaction yourself :-)
  102. </p>
  103. <p>When not in a transaction, the default is for all database changing queries (like insert, delete, update, etc) to <b>auto-commit</b>. That is, the database will reflect the changes immediately.
  104. </p>
  105. <h2>Examples</h2>
  106. <p>The following examples use the DBSQLite module to demonstrate use of the Framework.
  107. <ul>
  108. <li><a href="../examples/example_01.bmx">example_01.bmx</a></li>
  109. <li><a href="../examples/example_02.bmx">example_02.bmx</a></li>
  110. <li><a href="../examples/example_03.bmx">example_03.bmx</a></li>
  111. </ul>
  112. </p>