[More writing John Goerzen **20071106080056] { hunk ./en/book-shortcuts.xml 137 +quickQuery'"> +SqlValue"> +SqlString"> +SqlBool"> +SqlNull"> +SqlInteger"> +toSql"> +fromSql"> + + hunk ./en/ch21-databases.xml 144 + + + One popular database, MySQL, does not support transactions + with its default table type. In its default configuration, + MySQL will silently ignore calls to &commit; or &rollback; + and will commit all changes to disk immediately. The HDBC + ODBC driver has instructions for configuring MySQL to + indicate to HDBC that it does not support transactions, + which will cause &commit; and &rollback; to generate + errors. Alternatively, you can use InnoDB tables with + MySQL, which do support transactions. InnoDB tables are + recommended for use with HDBC. + + + hunk ./en/ch21-databases.xml 162 - FIXME + + Some of the simplest queries in SQL involve statements that + don't return any data. These queries can be used to create + tables, insert data, delete data, and set database parameters. + + + The most basic function for sending queries to a database is + &run;. This function takes an &IConnection;, a &String; + representing the query itself, and a list of parameters. + Let's use it to set up some things in our database. + + &query.ghci:setup; + + After connecting to the database, we first created a table + called test. Then we inserted one row of + data into the table. Finally, we committed the changes and + disconnected from the database. Note that if we hadn't called + &commit;, no final change would have been written to the + database at all. + hunk ./en/ch21-databases.xml 184 - - Prepared Queries - FIXME + + SqlValues + + Before proceeding any farther, we need to discuss a data type + introduced in HDBC: &SqlValue;. Since both Haskell and SQL + are strongly-typed systems, HDBC tries to preserve type + information as much as possible. At the same time, Haskell + and SQL types don't exactly mirror each other. Furthermore, + different databases have different ways of representing things + such as dates or special characters in strings. + + + &SqlValue; is a data type that has a number of constructors + such as &SqlString;, &SqlBool;, &SqlNull;, &SqlInteger;, and + more. This lets you represent various types of data in + argument lists to the database, and to see various types of + data in the results coming back, and still store it all in a + list. There are convenience functions &toSql; and &fromSql; + that you will normally use. If you care about the precise + representation of data, you can still manually construct + &SqlValue; data if you need to. + hunk ./en/ch21-databases.xml 208 + + Query Parameters + + + hunk ./en/ch21-databases.xml 215 + + We can get data back out of the database fairly easily as + well. HDBC provides a number of functions for doing this. To + start with, we'll use &quickQueryp;. + }