Home
MySQL++ User Manual
Contents
1. MySQL User Manual Queries That Return Data Dynamic Method The easiest way to retrieve data from MySQL uses a Result object which includes one or more Row objects Be cause these classes are std vector like containers you can treat the result set as a two dimensional array For example you can get the 5th item on the 2nd row by simply saying result 1 4 You can also access row ele ments by field name like this result 2 lookup_by_name price An alternate way of accessing your query results is through a ResUse object This class acts more like an STL input iterator than a container you walk through your result set one item at a time always going forward You can t seek around in the result set and you can t know how many results are in the set until you find the end This method is more efficient when there can be arbitrarily many results which could pose a memory allocation problem with the previous technique Queries That Return Data Static Method The Specialized SQL Structures SSQLS feature method above defines C structures that match the table struc tures in your database schema We call it the static method because the table structure is fixed at compile time Indeed some schema changes re quire that you update your SSQLS definitions and recompile or else the program could crash or throw bad conver sion exceptions when MySQL tries to stuff the new data into an outdated data structure No
2. Basic form template lt class Manip gt stock_value_list lt Manip gt value_list cchar d ry anip m mysqlpp quote const template lt class Manip gt stock_field_list lt Manip gt field_list cchar d anip m mysqlpp do_nothing const template lt class Manip gt stock_equal_list lt Manip gt equal_list cchar d cchar e Manip m mysqlpp quote const Boolean argument form template lt class Manip gt stock_cus_value_list lt Manip gt value_list cchar d Manip m bool il bool i2 false bool i5 false const List form template lt class Manip gt stock_cus_value_list lt Manip gt value_list cchar d Manip m 28 MySQL User Manual stock_enum il stock_enum 12 stock_NULL stock_enum i5 stock_NULL const Vector form template lt class Manip gt stock_cus_value_list lt Manip gt value_list cchar d vector lt bool gt i const Manip m Plus the obvious equivalents for field_list and equal_list Rather than try to learn what all of these methods do at once let s ease into the subject Consider this code stock s Dinner Rolls 75 0 95 0 97 1998 05 25 cout lt lt Value list lt lt s comma_list lt lt endl cout lt lt Field list lt lt s field_list lt lt endl cout lt lt Equal list lt lt s equal_list lt lt endl That would produce someth
3. con real_connect MY_DATABASE MY_HOST MY_USER MY_PASSWORD 3306 0 60 NULL Query query con query ostringstream strbuf ifstream In argv 1 ios in ios binary catch struct stat for_len if In rdbuf gt is_open if stat argv 1 amp for_len 1 return 1 unsigned int blen for_len st_size if blen return 1 char read_buffer new char blen In read read_buffer blen string fill read_buffer blen strbuf lt lt INSERT INTO lt lt MY_TABLE lt lt lt lt MY_FIELD lt lt VALUES lt lt mysqlpp escape lt lt fill lt lt lt lt ends query exec strbuf str delete read_buffer else cerr lt lt Your binary file lt lt argv 1 lt lt could not be open errno lt lt errno return 0 BadQuery amp er Handle any connection or query errors cerr lt lt Error lt lt er what lt lt lt lt con errnum lt lt endl return 1 catch BadConversion amp er Handle bad conversions cerr lt lt Error lt lt er what lt lt lt lt endl lt lt retrieved data size lt lt er retrieved lt lt actual data size lt lt er actual_size lt lt endl return 1 catch Catch all for any other standard C exceptions cerr lt lt except ion amp er Error lt lt er what lt lt endl return 1 1
4. there s no point in storing the result in an STL container We can store the first row directly into a stock structure because one of an SSQLS s constructors takes a Row object stock row res 0 Create a copy so that the replace query knows what the original values are stock orig_row row Change the stock object s item to use only 7 bit ASCII and to deliberately be wider than normal column widths printed by print_stock_table row item Nuerenberger Bratwurst Form the query to replace the row in the stock table query update orig_row row Show the query about to be executed cout lt lt Query lt lt query preview lt lt endl Run the query with execute since UPDATE doesn t return a result set query execute Print the new table contents print_stock_table query catch BadQuery amp er Handle any connection or query errors cerr lt lt Error lt lt er what lt lt endl return 1 14 MySQL User Manual catch BadConversion amp er Handle bad conversions cerr lt lt Error lt lt er what lt lt lt lt endl lt lt retrieved data size lt lt er retrieved lt lt actual data size lt lt er actual_size lt lt endl return 1 catch exception er Catch all for any other standard C exceptions cerr lt lt Error lt lt er what lt l
5. pric mo r EParms string like so 24 MySQL User Manual Result res query store 1 25 This would throw SQLQueryNEParms because the wheref is not specified In theory this exception should never be thrown If the exception is thrown it probably a logic error in your pro gram 25 MySQL User Manual 5 Specialized SQL Structures The Specialized SQL Structure SSQLS feature lets you easily define C structures that match the form of your SQL tables Because of the extra functionality that this feature builds into these structures MySQL can populate them automatically when retrieving data from the database with queries returning many records you can ask MySQL to populate an STL container of your SSQLS records with the results When updating the database MySQL can use SSQLS structures to match existing data and it can insert SSQLS structures directly into the database You define an SSQLS using one of several macros These are in the file custom h and in the file that it includes custom macros h There are a bunch of different macros for different purposes The following sections will discuss each macro type separately beginning with the easiest and most generally useful 5 1 sql_create This is the most basic sort of SSQLS declaration sql_create_5 stock 0 0 string item int num double weight double price mysqlpp Date date This creates a C structure calle
6. lt lt endl return 1 return 0 3 3 Quoting and Escaping SQL syntax often requires certain data to be quoted Consider this query SELECT FROM stock WHERE item Hotdog Buns Because the string Hotdog Buns contains a space it must be quoted With MySQL you don t have to add these quote marks manually string s Hotdog Buns Query q conn query q lt lt SELECT FROM stock WHERE item lt lt quote_only lt lt s 10 MySQL User Manual That code produces the same query string as in the previous example We used the MySQL quote_only manip ulator which causes single quotes to be added around the next item inserted into the stream This works for various string types for any type of data that can be converted to MySQL s ColData type and for Specialized SQL Struc tures The next section introduces the SSQLS feature Quoting is pretty simple but SQL syntax also often requires that certain characters be escaped Imagine if the string in the previous example was Frank s Brand Hotdog Buns instead The resulting query would be SELECT FROM stock WHERE item Frank s Brand Hotdog Buns That s not valid SQL syntax The correct syntax is SELECT FROM stock WHERE item Frank s Brand Hotdog Buns As you might expect MySQL provides that feature too through its escape mani
7. Getting info about the fields The following example demonstrates how to get some basic information about the fields including the name of the field and the SQL type This is examples fieldinf1 cpp tinclude util h include lt mysql h gt include lt iostream gt include lt iomanip gt using namespace std using namespace mysqlpp int main int argc char argv try Connection con use_exceptions if connect_to_db argc argv con return 1 Query query con query query lt lt select from stock cout lt lt Query lt lt query preview lt lt endl Result res query store cout lt lt Records Found lt lt res size lt lt endl lt lt endl cout lt lt Query Info n cout setf ios left for unsigned int i 0 i lt res names size i cout lt lt setw 2 lt lt i this is the name of the field lt lt setw 15 lt lt res names i c_str this is the SOL identifier name MySQL User Manual Result types unsigned int returns a mysql_type_info which in many ways is like type_info except that it has additional sql type information in it with one of the methods being sql_name lt lt setw 15 lt lt res types i sql_name this is the C identifier name which most closely resembles the sql name its is implementation defined and often not very readable lt lt setw 20 lt lt res typ
8. MySQL User Manual Kevin Atkinson Sinisa Milivojevic Monty Widenius Warren Young Copyright 1998 2001 2005 Kevin AtkinsonMySQL ABEducational Technology Resources Date 2005 05 26 07 28 32 0600 Thu 26 May 2005 Table of Contents TAINO AUCH OM 52 sss O ea gbeb ENEY 2 LIA Brief History of MySQL O A A gee eee 2 1 2 If You Have Q estions derne iii 2 DEON CLVIEW SSG aide esa ee A o a 3 21 The Connection Object issostni a tete ees Roe note wears Hawa ito dra 3 2 2 The Query Object isis s cc cthesctediice er eest E E EEE AEE cling TE sheeted pace edie OE ee awed 3 2 3 A 02 asesags disses bathe ea gctes sa OSM Aes deat bath ceapstas eas MERE a bata steaatea E masse bes 3 2 4 EXCUSA ted 4 O geese EA 5 3 1 Running the Examples vespide creadas 5 3 25 The Basics comedies a ths aacag e aa tia Mibay yas E a das seoag Vas EESE saab 5 3 3 Quoting and Escaping csser irene er eves EKET EES EI SEEEN RESELE EnS 10 3 4 Specialized SQL Structtites di rl recrea 11 3 3 Handling SQE NULS siii pain aeeeeeekss 16 3 6 Which Query Typeto Use id da da Licda 17 371 Tets Do Somethinp Useful reseppie stants speed obey euoeeod ph testes susedsheSedgbehswunesenieress 17 4 Template Queries a A A ai Ns 21 4 1 Setting up template queries venom ladito ioAd 22 4 2 Setting the parameters at execution time ooocccoccnnconnconnconccnnonnnnnnncnncnnconnconnccnnccnnconnconncinnss 23 4 3 Using A ssustecececd sn sess Sota Sosa EE EA RERE Someta
9. See the top of lib type_info cpp for the list Template instantiations are first class types in the C language on par with any other type You can use nullable MySQL types anywhere you d use the plain version of that type you can assign plain values to a nullable object and vice versa etc There s a secondary distinction between SQL null and anything available in the standard C type system SQL null is a distinct value equal to nothing else C s NULL is ambiguous being equal to O in integer context so MySQL has a global null object which which you can assign to any nullable object to get a SQL null By default if you try to convert a SQL null to a plain C data type MySQL will throw a BadNullConversion exception to enforce this distinction If you insert a SQL null into a C stream you get NULL The NullisNull behavior type encapsulates these two rules it is the default for one of template Nul1 s parameters To relax this distinction you can instantiate the Nul1 template with a different behavior type NullisZero or NullisBlank As you might guess from their names SQL nulls using these behaviors get converted to O or a blank C string respectively 3 6 Which Query Type to Use There are three major ways to execute a query in MySQL Query execute Query store and Query use Which should you use and why execute is for queries that do not return data per se For instance CREATE INDEX You d
10. query lt lt select 2 fieldl 3 field2 from stock where Sl wheref SqO what query parse The format of the placeholder is 22 MySQL User Manual modifier name Where Modifier can be any one of the following Print an actual rete Don t quote or escape no matter what q This will quote and escape the item us ing the MySQL C API function mysql_escape_string if it is a string or char or another MySQL specific type that needs to be quoted Q Quote but don t escape based on the same rules as for q This can save a bit of processing time if you know the strings will never need quoting r Always quote and escape even if it is a number R Always quote but don t escape even if it is a number represents a number up to two digits It is the order of parameters given to a SQLQueryParms object starting from 0 name is for an optional name which aids in filling SQLQueryParms Name can contain any alpha numeric charac ters or the underscore If you add this it must start with a letter If this is not the case add a colon after the name If you need to represent an actual colon after the name follow the name with two colons The first one will end the name and the second one won t be processed 4 2 Setting the parameters at execution time To specify the parameters when you want to execute a query simply use Query store const SQLString amp parm0
11. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE THERE IS NO WARRANTY FOR THE LIBRARY TO THE EXTENT PERMITTED BY APPLICABLE LAW EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND OR OTHER PARTIES PROVIDE THE LIBRARY AS IS WITHOUT WARRANTY OF ANY KIND EITHER EXPRESSED OR IMPLIED INCLUDING BUT NOT LIM ITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU SHOULD THE LIBRARY PROVE DEFECTIVE YOU ASSUME THE COST OF ALL NECESSARY SERVICING REPAIR OR CORRECTION 16 INNO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER OR ANY OTHER PARTY WHO MAY MODIFY AND OR REDISTRIBUTE THE LIB RARY AS PERMITTED ABOVE BE LIABLE TO YOU FOR DAMAGES INCLUDING ANY GENERAL SPE CIAL INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Libraries If you develop a new library and you want it to be of the greatest possible use to the public we recommend making it free software that everyone can redistribute and change You can
12. const SQLString amp parm11 This type of multiple overload also exists for Query use and Query execute parm0 corresponds to the first parameter etc You may specify up to 12 parameters For example Result res query store Dinner Rolls item item price with the template query provided above would produce select item price from stock where item Dinner Rolls The reason we didn t put the template parameters in numeric order select 0 fieldl 1 field2 from stock where 2 wheref q3 what will become apparent shortly 4 3 Using defaults You can also set the parameters one at a time by means of class Query s public data member def To change the val ues of the def simply use the subscript operator You can refer to the parameters either by number or by name The following two examples have the same effect 23 MySQL User Manual query def 0 Dinner Rolls query def 1 item query def 2 item query def 3 price and query def what Dinner Rolls query def wheref item query def field1 item query def field2 price Once all the parameters are set simply execute as you would have executed the query before you knew about tem plate queries Result res query store 4 4 Combining the two You can also combine the use of setting the parameters at execution time an ing Query store or use
13. func tion you set up a fixed query string with tags inside that indicate where to insert the variable parts If you have mul tiple queries that are structurally similar you simply set up one template query and use that in the various locations of your program A third method for building queries is to use Specialized SQL Structures SSQLS This feature presents your results as a C data structure instead of making you access the data through MySQL intermediary classes It also re duces the amount of embedded SQL code your program needs 2 3 Result Sets The field data in a result set are stored in a special std st ring like class called ColData This class has conver sion operators that let you automatically convert these objects to any of the basic C data types Additionally MySQL defines classes like DateTime which you can initialize from a MySQL DATETIME string These auto matic conversions are protected against bad conversions and can either set a warning flag or throw an exception de pending on how you set the library up As for the result sets as a whole MySQL has a number of different ways of representing them Queries That Do Not Return Data Not all SQL queries return data An example is CREATE TABLE For these types of queries there is a special result type that simply reports the state resulting from the query whether the query was successful how many rows it im pacted if any etc
14. you may also combine or link a work that uses the Library with the Lib rary to produce a work containing portions of the Library and distribute that work under terms of your choice provided that the terms permit modification of the work for the customer s own use and reverse engineering for de bugging such modifications You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License You must supply a copy of this License If the work during execution displays copyright notices you must include the copyright notice for the Library among them as well as a reference directing the user to the copy of this License Also you must do one of these things a Accompany the work with the complete corresponding machine readable source code for the Library including whatever changes were used in the work which must be distributed under Sec tions 1 and 2 above and if the work is an executable linked with the Library with the complete machine readable work that uses the Library as object code and or source code so that the user can modify the Library and then relink to produce a modified executable containing the modified Library It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions b Use a suitable shared library mechanism for l
15. Library and therefore falls outside the scope of this License However linking a work that uses the Library with the Library creates an executable that is a derivative of the Library because it contains portions of the Library rather than a work that uses the library The executable is therefore covered by this License Section 6 states terms for distribution of such executables When a work that uses the Library uses material from a header file that is part of the Library the object code for the work may be a derivative work of the Library even though the source code is not Whether this is true is espe cially significant if the work can be linked without the Library or if the work is itself a library The threshold for this to be true is not precisely defined by law If such an object file uses only numerical parameters data structure layouts and accessors and small macros and small inline functions ten lines or less in length then the use of the object file is unrestricted regardless of whether it is legally a derivative work Executables containing this object code plus portions of the Library will still fall un der Section 6 Otherwise if the work is a derivative of the Library you may distribute the object code for the work under the terms of Section 6 Any executables containing that work also fall under Section 6 whether or not they are linked directly with the Library itself 6 As an exception to the Sections above
16. Or execute without passing the ful supports 1d1 1d2 pric query store Hamburger Buns query store 1 25 price it m mo r query def fi query def fi Result resl Result res2 item Would store the query select item price from stock where item for res1 and select item price from stock where price 1 25 for res2 d setting them via the def object by call 1 number of parameters that the template Hamburger Buns Now you see why we ordered the placeholders in the template above as we did we used positions O and 1 for the ones we want to change frequently and used 2 and 3 for the parameters tha t seldom change One thing to watch out for however is that Query store const char q is also defined for executing the query q Therefore when you call Query store or use or execu te with only one item and that item is a const char you need to explicitly convert it into a SQLString to get the right overload Result res query store SQLString Hamburger Buns 4 5 Error Handling i If for some reason you did not specify all the parameters when executing the query and the remaining parameters do not have their values set via def the query object will throw a SQLQueryNEParms object If this happens you can get an explanation of what happened by checking the value of SQLQueryN query def fieldl1 query def field2 it m
17. along with the Library You may charge a fee for the physical act of transferring a copy and you may at your option offer warranty protec tion in exchange for a fee 2 You may modify your copy or copies of the Library or any portion of it thus forming a work based on the Lib rary and copy and distribute such modifications or work under the terms of Section 1 above provided that you also meet all of these conditions a The modified work must itself be a software library b You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change c You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License d If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility other than as an argument passed when the facility is in voked then you must make a good faith effort to ensure that in the event an application does not supply such function or table the facility still operates and performs whatever part of its purpose remains meaningful For example a function in a library to compute square roots has a purpose that is entirely well defined independent of the application Therefore Subsection 2d requires that any application sup plied function or table used by this function must be optional if the application does not supply it the squ
18. do so by permitting redistribution under these terms or alternatively under the terms of the ordinary General Public License To apply these terms attach the following notices to the library It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty and each file should have at least the copyright line and a pointer to where the full notice is found lt one line to give the library s name and a brief idea of what it does gt 40 MySQL User Manual Copyright lt year gt lt name of author gt This library is free software you can redistribute it and or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation either version 2 1 of the License or at your option any later version This library is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICU LAR PURPOSE See the GNU Lesser General Public License for more details You should have received a copy of the GNU Lesser General Public License along with this lib rary if not write to the Free Software Foundation Inc 59 Temple Place Suite 330 Boston MA 02111 1307 USA Also add information on how to contact you by electronic and paper mail You should also get your employer if you work as a programmer or your school if any to sign a copyright dis cl
19. double weight double price Date sdate 12 MySQL User Manual int main int argc char argv try Establish the connection to the database server Connection con use_exceptions if connect_to_db argc argv con return 1 Create and populate a stock object We could also have used the set member which takes the same parameters as this constructor stock row Hot Dogs 100 1 5 1 75 1998 09 25 Form the query to insert the row into the stock table Query query con query query insert row Show the query about to be executed cout lt lt Query lt lt query preview lt lt endl Execute the query We us xecute because INSERT doesn t return a result set query execute Print the new table print_stock_table query catch BadQuery amp er Handle any connection or query errors cerr lt lt Error lt lt er what lt lt endl return 1 catch BadConversion amp er Handle bad conversions cerr lt lt Error lt lt er what lt lt lt lt endl lt lt retrieved data size lt lt er retrieved lt lt actual data size lt lt er actual_size lt lt endl return 1 catch exception er Catch all for any other standard C exceptions cerr lt lt Error lt lt er what lt lt endl return 1 return 0 That s al
20. example stock table void print_stock_table mysqlpp Query query You must reset the query object when re using it query reset You can write to the query object like you would any ostream query lt lt select from stock Show the query string If you do this you have to do it before you execute or store or use it cout lt lt Query lt lt query preview lt lt endl Execute the query and display the result set mysqlpp Result res query store print_stock_rows res connect_to_do 1 11 1111 1 1 111 ID VIVIAN Establishes a connection to a MySQL database server optionally attaching to database kdb This is basically a command line parser for the examples since the example programs arguments give us the information we need to establish the server connection MySQL User Manual bool connect_to_db int argc char argv mysqlpp Connection amp con const char kdb if arge lt 1 cerr lt lt Bad argument count lt lt argc lt lt lt lt endl return false if argc gt 1 amp amp argv 1 0 cout lt lt usage lt lt argv 0 lt lt host user password port lt lt endl cout lt lt endl lt lt tConnects to database if kdb cout lt lt rut lt lt kdb LL ms else cout lt lt server cout lt lt on localhost using your
21. in version 4 1 Because MySQL does not need to know anything about the flowing through it it doesn t have explicit UTF 8 support C s std string stores UTF 8 data just fine But your program probably does care about the data coming from MySQL The remainder of this chapter covers the choices you have for dealing with UTF 8 en coded Unicode data 6 2 Unicode and Unix Modern Unices support UTF 8 natively Red Hat Linux for instance has had system wide UTF 8 support since ver sion 8 This continues in the commercial and Fedora forks of Red Hat Linux of course On such a Unix the terminal I O code understands UTF 8 encoded data so your program doesn t require any special code to correctly display a UTF 8 string If you aren t sure whether your system supports UTF 8 natively just run the simplel example if the first item has two high ASCII characters in place of the in N rnberger Brats you know it s not handling UTF 8 If your Unix doesn t support UTF 8 natively it likely doesn t support any form of Unicode at all for the historical reasons I gave above Therefore you will have to convert the UTF 8 data to the local 8 bit character set The stand ard Unix function iconv can help here If your system doesn t have the iconv facility there is a free imple mentation available from the GNU Project Another library you might check out is IBM s ICU This is rather heavy weight so if you just need basic conversions ic
22. it And you must show them these terms so they know their rights We protect your rights with a two step method 1 we copyright the library and 2 we offer you this license which gives you legal permission to copy distribute and or modify the library To protect each distributor we want to make it very clear that there is no warranty for the free library Also if the library is modified by someone else and passed on the recipients should know that what they have is not the original version so that the original author s reputation will not be affected by problems that might be introduced by others Finally software patents pose a constant threat to the existence of any free program We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent hold er Therefore we insist that any patent license obtained for a version of the library must be consistent with the full 35 MySQL User Manual freedom of use specified in this license Most GNU software including some libraries is covered by the ordinary GNU General Public License This li cense the GNU Lesser General Public License applies to certain designated libraries and is quite different from the ordinary General Public License We use this license for certain libraries in order to permit linking those libraries in to non free programs When a program is linked with a library whether stati
23. lt endl This will return the first item that begins with Hamburger You can also use it will any STL algorithm that require the values to be less than comparable 5 3 sql_create with Additional Initializers If third parameter for this macro INITPARMS is nonzero the SSQLS will have two additional members functions that make it easier to initialize the structure s data members For example sql_create_5 stock 1 5 string item int num double weight double price mysqlpp Date date will add these functions to the structure relative to that in the previous example struct stock stock const string amp const intg const doubles 27 MySQL User Manual const double amp const mysqlpp Date amp set const string amp const int amp const double amp const double amp const mysqlpp Date amp There is one trick with this because each SSQLS has at least one other constructor besides the one defined by this feature not every logical value for INITPARMS results in working code A simple example is setting KEYS and INITPARMS to the same value you get two identical constructor definitions so the compiler refuses to compile the code If you are getting compiler errors having to do with duplicate definitions try changing this value to zero 5 4 Additional Features of Specialized SQL Structures Up to this point we haven t been using all of the features in the SSQLS structures we ve been ge
24. sample database lt lt con error lt lt endl return 1 else if con select_db kpcSampleDatabase cerr lt lt Failed to select sample database lt lt endl return 1 else created true mysqlpp Query query con query create a new query object try query execute drop table stock catch mysqlpp BadQuery amp ignore any errors try Send the query to create the table and execute it query lt lt create table stock item char 20 not null num bigint lt lt weight double price double sdate date query execute 21 MySQL User Manual Set up the template query to insert the data The parse call tells the query object that this is a template and not a literal query string query lt lt insert into 5 table values 0q Slq 2 3 4q query parse This is setting the parameter named table to stock query def table stock The last parameter table is not specified here Thus the default value for table is used which is stock Also notice that the first row is a UTF 8 encoded Unicode string All you have to do to store Unicode data in recent versions of MySQL is use UTF 8 encoding query execute N rnberger Brats 92 1 5 8 79 2005 03 10 query execute Pickle Relish 87 1 5 1 75 1998 09 04 query execute Hot Mustard 75 95 97 1998 05 25
25. seed IEEE EGEE O ES REES REEE e 23 4 4 Combining the two or erregte o eE dea weegenede eee e ESEE OEE ESEE ge SEE EEEE sents de 24 4 5 Error Handling vs tctsc 3 otek esi inte saa seta ds Sashes sea setcadeeeantayeb votva ss snatsa ses pME ae Sede Rae etme a 24 5 Specialized SQL Structures pensier oiron se ee anA EEEE EES rE EEA EEEE EEE ENERE oea 26 DL SOL Create acs onena E A E O AA ad panded aoa EA EEE AIAS AARS 26 52 sdl Creat with COM dE a E iii 26 5 3 sql_create with Additional Initializers oococcoonccononccononocononoconococononocononoconroronroronanoronacos 27 5 4 Additional Features of Specialized SQL Structures ccocccoccnnconnconnconeconccnncnnncnnncnnncnnncnnnonoss 28 3 3 Harnessing SSQES Internals ea eiii palas rea ad aires 28 216 Alternate Cr ation Methods rrenen nnna os ued hacen AE EA eE a sede nagesteerteas 31 3 7 Expanding SSQES Macro Soent a eta oa ri ees 31 5 8 Extending the SSQLS Mechanism is 5 ccscsc sse0aas s sehessssoencapsessavaesevsedssesseesassoabageeesaonssays 31 6 Using Unicode with MySQL H oo cece cence o retine i pE eeaneeae eens EEOSE EEE EEES E ES eis 33 6 1 A Short History of Unicode s cc3 5 sschedsscsttessberts sts cence ask patrol dades 33 6 2 Unicode and UNIX arnnerioprecici inte nE E EK ETVE EEE E EEEIEE EKSE EEEE IEEE IRETE 33 6 3 Unicode and Win3 2 sensores trenes ani aee eG a eE ERE ss PASE POSE Sa E PESE SER nS EEE 33 6 4 For More Information si i sc6cscsdevcccsvsescsus
26. the ColData link for the details Users of this example should beware that one more check is required in order to run this query safely in some ex treme cases the size of the query might grow larger than MySQL s maximum allowed packet size This check should be added 20 MySQL User Manual 4 Template Queries Another powerful feature of MySQL is being able to set up template queries These are kind of like C s printf facility you give MySQL a string containing the fixed parts of the query and placeholders for the variable parts and you can later substitute in values into those placeholders The following example demonstrates how to use this feature This is the program you ve run a few times now if you ve worked through all the examples resetdb cpp tinclude util h tinclude lt mysql h gt include lt iostream gt using namespace std int main int argc char argv mysqlpp Connection con mysqlpp use_exceptions try if connect_to_db argc argv con return 1 catch exceptiong er cerr lt lt Connection failed lt lt er what lt lt endl return 1 bool created false try con select_db kpcSampleDatabase catch mysqlpp BadQuery amp Couldn t switch to the sample database so assume that it doesn t exist and create it If that doesn t work exit with an error if con create_db kpcSampleDatabase cerr lt lt Failed to create
27. user lt lt endl cout lt lt tname and no password by default lt lt endl lt lt endl return false if kdb kdb kpcSampleDatabase bool success false if argc 1 success con connect kdb else if argc 2 success con connect kdb argv 1 else if argc 3 success con connect kdb argv 1 argv 2 else if argc 4 success con connect kdb argv 1 argv 2 argv 3 else if argc gt 5 success con real_connect kdb argv 1 argv 2 argv 3 atoi argv 4 if success cerr lt lt Database connection failed lt lt endl lt lt endl return success The only simplication done to the code is to remove the Unicode conversion stuff That is covered in a later chapter Using Unicode with MySQL Notes on exceptions Aside from driving the connect_to_db and print_stock_table functions in the utility module the simplel example mainly consists of exception handlers This block of code will become familiar to you in the fol lowing examples because it s identical for all of them A few notes about exceptions 1 Exceptions are the default way for MySQL to signal errors but some MySQL classes Connection MySQL User Manual Query Result ResUse and Row allow you to disable these exceptions The methods vary but some classes constructors accept a flag to disable exceptions some allow you to disable excep
28. uses the SSQLS comparison setup it res find stock Hotdog Buns if it res end cout lt lt endl lt lt Currently lt lt it gt num lt lt hotdog buns in stock lt lt endl i else cout lt lt endl lt lt Sorry no hotdog buns in stock lt lt endl catch BadQuery amp er Handle any connection or query errors cerr lt lt Error lt lt er what lt lt endl return 1 catch BadConversion amp er Handle bad conversions cerr lt lt Error lt lt er what lt lt lt lt endl lt lt retrieved data size lt lt er retrieved lt lt actual data size lt lt er actual_size lt lt endl return 1 catch exception er Catch all for any other standard C exceptions cerr lt lt Error lt lt er what lt lt endl return 1 return 0 3 5 Handling SQL Nulls There is no equivalent of SQL s null in the standard C type system The primary distinction is one of type in SQL null is a column attribute which affects whether that column can hold a SQL null This effectively doubles the number of types in SQL MySQL handles this the same way SQL does it provides the Null template to allow the creation of distinct nullable versions of existing C types For 16 MySQL User Manual each column type MySQL understands the library instantiates this template for the closest C type
29. your current username is assumed If you leave of the password it is assumed that you don t need one And if you leave off the port it will use the standard MySQL port number The user you give resetdb needs to be an account with permission to create databases Once the database is created you can use any account that has full permission to the sample database mysql_cpp_data You may also have to re run resetdb after running some of the other examples as they change the database 3 2 The Basics A simple example The following example demonstrates how to open a connection execute a simple query and display the results This is examples simple 1 cpp tinclude util h include lt mysql h gt include lt iostream gt include lt iomanip gt using namespace std int main int argc char argv Wrap all MySQL interactions in one big try block so any errors are handled gracefully try MySQL User Manual Connect to the sample database mysqlpp Connection con mysqlpp use_exceptions if connect_to_db argc argv con return 1 Create a query object that is bound to con mysqlpp Query query con query Print out the stock table print_stock_table query catch mysqlpp BadQuery amp er handle any connection or query errors that may come up cerr lt lt Error lt lt er what lt lt endl return 1 catch mysqlpp BadConver
30. 0 NULL a Query query con query query lt lt MY_QUERY ResUse res query use Row row strbuf lt lt delete from lt lt MY_TABLE lt lt where lt lt MY_FIELD lt lt in q for UPDATE just replace the above DELETE FROM with UPDATE statement for row res fetch_row itt strbuf lt lt row 0 lt lt if i return 0 string output strbuf str output erase output size 1 1 output query exec output cout lt lt output lt lt endl catch BadQuery amp er Handle any connection or query errors cerr lt lt Error lt lt er what lt lt lt lt con errnum lt lt endl return 1 catch BadConversion amp er Handle bad conversions cerr lt lt Error lt lt er what lt lt X lt lt endl lt lt retrieved data size lt lt er retrieved lt lt actual data size lt lt er actual_size lt lt endl return 1 catch exception er Catch all for any other standard C exceptions cerr lt lt Error lt lt er what lt lt endl return 1 return 0 Notice that the row values used in the IN clause aren t escaped or quoted This is because row elements are ColData types so they have automatic escaping and quoting as appropriate to the type being inserted If you want to disable this feature it s easily done click
31. 8 MySQL User Manual Notice that we used the escape manipulator when building the INSERT query above This is because we re not us ing one of the MySQL types that does automatic escaping and quoting Displaying images in HTML from BLOB column This example is also a very short one considering the function that it performs Although all modern versions of MySQL have a command that dumps data from a BLOB column to a binary file this example shows how to do it in your code instead without requiring an temporary file on disk This is examples cgi_image cpp tinclude lt mysql h gt using namespace std using namespace mysqlpp define MY_DATABASE telcent define MY_TABLE fax define MY_HOST localhost define MY_USER root define MY_PASSWORD define MY_FIELD fax BLOB field define MY_KEY datet PRIMARY KEY int main int argc char argv if argc lt 2 cerr lt lt Usage cgi_image primary_key_value lt lt endl lt lt endl return 1 cout lt lt Content type image jpeg lt lt endl Connection con use_exceptions try con real_connect MY_DATABASE MY_HOST MY_USER MY_PASSWORD 3306 0 60 NULL Query query con query query lt lt SELECT lt lt MY_FIELD lt lt FROM lt lt MY_TABLE lt lt WHERE lt lt MY_KEY lt lt lt lt argv 1 ResUse res query use R
32. Print out a row of data from the stock table in a format compatbile with the header printed out in the previous function void print_stock_row const char item mysqlpp longlong num double weight double price mysqlpp Date date Output first column the item string cout lt lt setw 20 lt lt item lt lt lt lt setw 9 lt lt num lt lt lt lt setw 9 lt lt weight lt lt lt lt setw 9 lt lt price lt lt lt lt date lt lt endl print_stock_rows 1 11 2117 11111111 2811 01 1 III IV III ISS Print out a number of rows from the example stock table void print_stock_rows mysqlpp Result amp res print_stock_header res size Use the Result class s read only random access iterator to walk through the query results mysqlpp Result iterator i for i res begin i res end i Convert the Result iterator into a Row object for easier access mysqlpp Row row i Notice that you can use either the column index or name to retrieve the data Also notice that we do no explicit conversions to match print_stock_row s parameter types Row elements are ColData strings so they auto convert to any standard C type print_stock_row row 0 row 1 row lookup_by_name weight row 3 row 4 print_stock_table 2 11 11 1711711171111 1 11111171111 1111111 Print th ntire contents of the
33. SQL User Manual try Connection con use_exceptions if connect_to_db argc argv con return 1 Get all the rows in the stock table Query query con query query lt lt select from stock vector lt stock gt res query storein res 1f res size gt 0 Build a select query using the data from the first row returned by our previous query query reset query lt lt select from stock where lt lt res 0 equal_list and stock_weight stock_price Display the finished query cout lt lt Custom query n lt lt query preview lt lt endl return 0 catch BadQuery amp er Handle any connection or query errors cerr lt lt Error lt lt er what lt lt endl return 1 catch BadConversion amp er Handle bad conversions cerr lt lt Error lt lt er what lt lt X lt lt endl lt lt retrieved data size lt lt er retrieved lt lt actual data size lt lt er actual_size lt lt endl return 1 catch exception er Catch all for any other standard C exceptions cerr lt lt Error lt lt er what lt lt endl return 1 This example uses the list form of equal_list The arguments stock_weight and stock_price are enum values equal to the position of these columns within the stock table sq1_create_x generates this enum for you automatically The bool
34. able and store the data in a vector of stock SSQLS structures Query query con query query lt lt select from stock vector lt stock gt res query storein res Display the result set print_stock_header res size vector lt stock gt iterator it for it res begin it res end it catch print_stock_row it gt item c_str it gt num it gt weight it gt price it gt sdate BadQuery amp er Handle any connection or query errors cerr lt lt Error lt lt er what lt lt endl return 1 catch BadConversion amp er Handle bad conversions cerr lt lt Error lt lt er what lt lt X lt lt endl lt lt retrieved data size lt lt er retrieved lt lt actual data size lt lt er actual_size lt lt endl return 1 catch exception amp er Catch all for any other standard C exceptions cerr lt lt Error lt lt er what lt lt endl return 1 return 0 As you can see SSQLS is very powerful Adding data SSQLS can also be used to add data to a table This is examples custom2 cpp include include include include include include ella lt mysql h gt lt custom h gt lt iostream gt lt string gt lt vector gt using namespace std using namespace mysqlpp sql_create_5 stock 1 5 string item longlong num
35. aimer for the library if necessary Here is a sample alter the names Yoyodyne Inc hereby disclaims all copyright interest in the library Frob a library for tweaking knobs written by James Random Hacker lt signature of Ty Coon gt 1 April 1990 Ty Coon President of Vice That s all there is to it 41
36. ams and run simple in a console window a k a DOS box The first item should be N rnberger Brats If not see the last paragraph in this section utf8_to_win32_ansi converts ut 8_str from UTF 8 to UCS 2 and from there to the local code page Wait aminnit you shout I thought we were trying to get away from the problem of local code pages The console is one of the few Win32 facilities that doesn t support UCS 2 by default It can be put into UCS 2 mode but that seems like more work than we d like to go to in a portable example program Since the default code page in most versions of Windows includes the ii character used in the sample database this conversion works out fine for our purposes If your program is using the GUI to display text you don t need the second conversion Prove this to yourself by adding the following to ut 8_to_win32_ansi after the MultiByteToWideChar call MessageBox 0 ucs2_buf UCS 2 version of Item MB_OK All of this assumes you re using Windows NT or one of its direct descendants Windows 2000 Windows XP Win dows 2003 Server and someday Longhorn Windows 95 98 ME and Windows CE do not support UCS 2 They still have the W APIs for compatibility but they just smash the data down to 8 bit and call the A version for you 6 4 For More Information The Unicode FAQs page has copious information on this complex topic When it comes to Unix and UTF 8 specific items the UTF 8 and Un
37. applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it too but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case based on the explanations below When we speak of free software we are referring to freedom of use not price Our General Public Licenses are de signed to make sure that you have the freedom to distribute copies of free software and charge for this service if you wish that you receive source code or can get it if you want it that you can change the software and use pieces of it in new free programs and that you are informed that you can do these things To protect your rights we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it For example if you distribute copies of the library whether gratis or for a fee you must give the recipients all the rights that we gave you You must make sure that they too receive or can get the source code If you link other code with the library you must provide complete object files to the recipients so that they can relink them with the lib rary after making changes to the library and recompiling
38. are root function must still compute square roots These requirements apply to the modified work as a whole If identifiable sections of that work are not derived from the Library and can be reasonably considered independent and separate works in themselves then this License and its terms do not apply to those sections when you distribute them as separate works But when you distribute the same sections as part of a whole which is a work based on the Library the distribution of the whole must be on the terms of this License whose permissions for other licensees extend to the entire whole and thus to each and every part regardless of who wrote it Thus it is not the intent of this section to claim rights or contest your rights to work written entirely by you rather the intent is to exercise the right to control the distribution of derivative or collective works based on the Library In addition mere aggregation of another work not based on the Library with the Library or with a work based on the Library on a volume of a storage or distribution medium does not bring the other work under the scope of this License 3 You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library To do this you must alter all the notices that refer to this License so that they refer to the ordin ary GNU General Public License version 2 instead of to this License If a newer version than ve
39. ata members in a different order from those in the MySQL table you can do it like this sql_create_c_order_5 stock 2 5 mysqlpp Date date 5 double price 4 string item 1 int num 2 double weight 3 You can combine the custom names and custom ordering like this sql_create_complete_5 stock 2 5 mysqlpp date date shipment 5 double price price 4 string item item 1 int num quantity 2 double weight weight 3 All three of these macro types have basic variants that work the same way Again basic SSQLSes lack the fea tures necessary for automatic insert update and replace query creation 5 7 Expanding SSQLS Macros If you ever need to see the code that a given SSQLS declaration expands out to use the utility doc ssqls pretty like so ssqls pretty lt myprog cpp less This locates the first SSQLS declaration in that file and uses the C preprocessor to expand that macro You may have to change the script to tell it where your MySQL header files are 5 8 Extending the SSQLS Mechanism The SSQLS headers custom h and custom macros h are automatically generated by the Perl script custom pl Although it is possible to change this script to get additional functionality it s usually better to do that through inher itance A regular user may find it helpful to change the the limit on the maximum number of SSQLS data members al 31 MySQL User Manual lowed I
40. ble to make quite functional programs without tapping but a fraction of its power This section will in troduce you to the most useful fraction This tutorial assumes you know C fairly well in particuler the Standard Template Library STL and exceptions 3 1 Running the Examples All of the examples are complete running programs They may or may not be built for you already depending on how you installed the library If you installed MySQL from the source tarball on a Unixy system the examples should have been built along with the library If not simply go into the examples directory and type make If you installed the library via RPM the examples are in the mysql devel RPM After installing that the examples are in usr src mysql examples To build them go into that directory and type make f Make file simple See the file usr share doc mysql devel README examples for more details If you are on a Windows system the build process for the library should have built the examples as well Where the programs are depends on which compiler you re using There should be a README file in the distribution specif ic to your compiler with further instructions Once you have the examples building you need to initialize the sample database by running the resetdb example The usage of resetdb is as follows resetdb host user password port If you leave off host localhost is assumed If you leave off user
41. cally or using a shared library the combination of the two is legally speaking a combined work a derivative of the original library The ordinary General Public License there fore permits such linking only if the entire combination fits its criteria of freedom The Lesser General Public Li cense permits more lax criteria for linking other code with the library We call this license the Lesser General Public License because it does Less to protect the user s freedom than the ordinary General Public License It also provides other free software developers Less of an advantage over compet ing non free programs These disadvantages are the reason we use the ordinary General Public License for many lib raries However the Lesser license provides advantages in certain special circumstances For example on rare occasions there may be a special need to encourage the widest possible use of a certain library so that it becomes a de facto standard To achieve this non free programs must be allowed to use the library A more frequent case is that a free library does the same job as widely used non free libraries In this case there is little to gain by limiting the free library to free software only so we use the Lesser General Public License In other cases permission to use a particular library in non free programs enables a greater number of people to use a large body of free software For example permission to use the GNU C Library in non free prog
42. contradicts the license restrictions of other proprietary libraries that do not nor mally accompany the operating system Such a contradiction means you cannot use both them and the Library to gether in an executable that you distribute 7 You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities not covered by this License and distribute such a combined library provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted and provided that you do these two things a Accompany the combined library with a copy of the same work based on the Library uncom bined with any other library facilities This must be distributed under the terms of the Sections above b Give prominent notice with the combined library of the fact that part of it is a work based on the Library and explaining where to find the accompanying uncombined form of the same work 8 You may not copy modify sublicense link with or distribute the Library except as expressly provided under this License Any attempt otherwise to copy modify sublicense link with or distribute the Library is void and will automatically terminate your rights under this License However parties who have received copies or rights from you under this License will not have their licenses terminated so long as such parties remain in full complianc
43. d stock containing five member variables along with some constructors and other member functions useful with MySQL One of the generated constructors takes a reference to a mysqlpp Row object allowing you to easily populate a vec tor of stocks like so vector lt stock gt result query storein result That s all there is to it The only requirements are that the table structure be compatible with the SSQLS s member variables and that the fields are in the same order The general format of this set of macros is sql_create_ NAME KEYS INITPARMS TYPE1 ITEM1 TYPE ITEM Where is the number of member variables NAME is the name of the structure you wish to create TYPEx is the type name for a member variable and ITEM is that variable s name The KEYS and INITPARMS arguments can always be zero to keep things simple We will discuss what happens if you use different values in the next few sections 5 2 sql_create with Compare SSQLS structures can also have member functions that allow you to compare one structure to another You simply change the first O in the previous example KEYS to a higher value If this number is N then two structures are considered equal if the first N members of each are equal For example sql_create_5 stock 1 0 string item int num 26 MySQL User Manual double weight double price mysqlpp Date date Here we are sayin
44. dd an explicit geographical distribution limitation excluding those countries so that distribution is permitted only in or among countries not thus excluded In such case this License incorporates the limitation as if written in the body of this License 13 The Free Software Foundation may publish revised and or new versions of the Lesser General Public License from time to time Such new versions will be similar in spirit to the present version but may differ in detail to ad dress new problems or concerns Each version is given a distinguishing version number If the Library specifies a version number of this License which applies to it and any later version you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation If the Library does not specify a license version number you may choose any version ever published by the Free Software Foundation 14 If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incom patible with these write to the author to ask for permission For software which is copyrighted by the Free Software Foundation write to the Free Software Foundation we sometimes make exceptions for this Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally NO WARRANTY 15
45. e 9 You are not required to accept this License since you have not signed it However nothing else grants you per mission to modify or distribute the Library or its derivative works These actions are prohibited by law if you do not accept this License Therefore by modifying or distributing the Library or any work based on the Library you in dicate your acceptance of this License to do so and all its terms and conditions for copying distributing or modify ing the Library or works based on it 10 Each time you redistribute the Library or any work based on the Library the recipient automatically receives a license from the original licensor to copy distribute link with or modify the Library subject to these terms and con ditions You may not impose any further restrictions on the recipients exercise of the rights granted herein You are not responsible for enforcing compliance by third parties with this License 11 If as a consequence of a court judgment or allegation of patent infringement or for any other reason not limited to patent issues conditions are imposed on you whether by court order agreement or otherwise that contradict the conditions of this License they do not excuse you from the conditions of this License If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations then as a con sequence you may not distribute the Library at all For exampl
46. e if a patent license would not permit royalty free re distribution of the Library by all those who receive copies directly or indirectly through you then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library If any portion of this section is held invalid or unenforceable under any particular circumstance the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims this section has the sole purpose of protecting the integrity of the free software distribu tion system which is implemented by public license practices Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system it is up to the author donor to decide if he or she is willing to distribute software through any other system and a licensee can 39 MySQL User Manual not impose that choice This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License 12 If the distribution and or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces the original copyright holder who places the Library under this License may a
47. e ad dresses in addition to the mailing list MySQL User Manual 2 Overview MySQL has developed into a very complex and powerful library with many different ways to accomplish the same task Unfortunately this means that figuring out how to perform a simple task can be frustrating for new users In this section we will provide an overview of the most important user facing components of the library The overall process for using MySQL is similar to that of most other database access APIs 1 Open the connection 2 Form and execute the query 3 Iterate through the result set 4 Goto2 There is however a lot of extra functionality along each step of the way 2 1 The Connection Object A Connection object manages the connection to the MySQL server You need at least one of these objects to do any thing A Connection object can either create Query objects for you or you can execute queries directly through the Connection object The separate Query object is the recommended way as it gives you far more power 2 2 The Query Object A Query object is the recommended way of building and executing queries It is subclassed from std stringstream which means you can write to it like any other C stream to form a query The library in cludes stream manipulators that make it easy to generate syntactically correct SQL You can also set up template queries with this class Template queries work something like the C printf
48. ean argument form of that equal_list call would look like this query lt lt select from stock where lt lt res 0 equal_list and false false true true false It s a little more verbose as you can see And if you want to get really complicated use the vector form vector lt bool gt v 5 false v stock_weight true v stock_price true query lt lt select from stock where lt lt res 0 equal_list and v This form makes the most sense if you are building many other queries and so can re use that vector object 30 MySQL User Manual Many of these methods accept manipulators and custom delimiters The defaults are suitable for building SQL quer ies but if you re using these methods in a different context you may need to override these defaults For instance you could use these methods to dump data to a text file using different delimiters and quoting rules than SQL At this point we ve seen all the major aspects of the SSQLS feature The final sections of this chapter look at some of the peripheral aspects 5 6 Alternate Creation Methods If for some reason you want your SSQLS data members to have different names than used in the MySQL database you can do so like this sql_create_c_names_5 stock 1 5 string item item int num quantity double weight weight double price price mysqlpp Date date shipment If you want your SSQLS to have its d
49. eful These next few examples demonstrate just how powerful C can be allowing you to do a lot of work in few lines of code without losing efficiency Since the code is meant to be re used as is constants that can differ from one case to another have been grouped in order to simplify editing Also all of these examples have full error checking code showing off the power of MySQL s exception handling features Loading binary file in a BLOB column Since MySQL 3 23 BLOB columns have been available but their use is sometimes not straightforward Besides showing how easy it can be with MySQL this example demonstrates several features of MySQL The program requires one command line parameter which is a full path to the binary file This is examples load_file cpp tinclude lt mysql h gt include lt sys stat h gt 17 MySQL User Manual include lt fstream gt include lt errno h gt include lt stdlib using using const const const const const const int main i if name n gt space std namespace mysqlpp char MY_DATABASE telcent char MY_TABLE fax char MY_HOST localhost char MY_USER root char MY_PASSWORD char MY_FIELD fax BLOB field nt argc char argv arge lt 2 cerr lt lt Usage load_file full_file_path lt lt endl lt lt endl return 1 Connection con use_exceptions try
50. es i name lt lt endl cout lt lt endl if res types 0 typeid string this is demonstrating how a mysql_type_info can be compared with a C type_info cout lt lt Field item is of an SQL type which most closely resembles nthe C string type n if res types 1 typeid longlong cout lt lt Field num is of an SQL type which most closely resembles nC long long int type n i lse if res types 1 base_type typeid longlong you have to be careful as if it can be null the actual type is Null lt TYPE gt not TYPE So you should always use the base_type method to get at the underlying type If the type is not null than this base type would be the same as its type cout lt lt Field num base type is of an SQL type which most closely nresembles the C long long int type n catch BadQuery amp er Handle any connection or query errors cerr lt lt Error lt lt er what lt lt endl return 1 catch BadConversion amp er Handle bad conversions cerr lt lt Error lt lt er what lt lt X lt lt endl lt lt retrieved data size lt lt er retrieved lt lt actual data size lt lt er actual_size lt lt endl return 1 catch exception er Catch all for any other standard C exceptions cerr lt lt Error lt lt er what
51. g that the item field is a kind of key field it is always unique between any two stock items so if two stock records have equal item values they are the same stock item That change adds the following members to the SSQLS struct stock stock const std string amp pl set const std string amp pl bool operator const stock sgother const bool operator const stock o0ther const bool operator gt const stock amp other const bool operator lt const stock amp other const bool operator gt const stock o0ther const bool operator lt const stock o0ther const int cmp const stock amp o0ther const int compare const stock amp other const int compare const stock amp x const stock amp y The global compare function compares x to y and returns lt 0 if x lt y 0 if x y and gt 0 if x gt y stock cmp and stock compare are the same thing as compare this other The additional constructor initializes the key fields of the structure and leaves the other member variables undefined This is useful for creating temporary objects to use for comparisons like x lt stock Hotdog Because stock is now less than comparable you can store the query results in an STL associative container std set lt stock gt result query storein result And you can now use it like any other set cout lt lt result lower_bound stock Hamburger gt item lt
52. g this time Sinisa ran the MySQL mailing list and supported its users but made no new releases There were many patches submitted dur ing this period some of which were ignored others which were just put on the MySQL web site for people to try A lot of these patches were mutually incompatible and not all of them gave a fully functional copy of MySQL Most of them centered on GCC compatibility because GCC 3 was created around this same time The continual tightening of the rules starting with GCC 3 0 exposed a lot of places in MySQL that used deprecated and non conforming facilities In early August of 2004 the current maintainer Warren Young got fed up with this situation and took over He re leased 1 7 10 later that month 1 2 If You Have Questions If you want to email someone to ask questions about this library we greatly prefer that you send mail to the MySQL mailing list The mailing list is archived so if you have questions do a search to see if the question has been asked before You may find people s individual email addresses in various files within the MySQL distribution Please do not send mail to them unless you are sending something that is inherently personal Questions that are about MySQL usage may well be ignored if you send them to our personal email accounts Those of us still active in MySQL development monitor the mailing list so you aren t getting any extra coverage by sending messages to thos
53. icode FAQ for Unix Linux is a quicker way to find basic information 34 MySQL User Manual 7 Licensing The primary copyright holders on MySQL are Kevin Atkinson 1998 MySQL AB 1999 through 2001 and Educational Technology Resources Inc 2004 through the date of this writing MySQL is released under the GNU Lesser General Public License LGPL reproduced below This license basically states that you are free to use distribute and modify these works whether for personal or com mercial purposes as long as you grant the same rights to those you distribute the works to whether you changed them or not See the licenses below for full details 7 1 GNU Lesser General Public License Version 2 1 February 1999 Copyright C 1991 1999 Free Software Foundation Inc 59 Temple Place Suite 330 Boston MA 02111 1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document but changing it is not allowed This is the first released version of the Lesser GPL It also counts as the successor of the GNU Library Public Li cense version 2 hence the version number 2 1 Preamble The licenses for most software are designed to take away your freedom to share and change it By contrast the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This license the Lesser General Public License
54. ing like Dinner Rolls 75 0 95 0 97 1998 05 25 item num weight price date item Dinner Rolls num 75 weight 0 95 Value list Field list Equal list price 0 97 date 1998 05 25 That is a value list is a list of data member values within a particular SSQLS instance a field list is a list of the fields columns within that SSQLS and an equal list is a list in the form of an SQL equals clause Just knowing that much it shouldn t surprise you to learn that Query insert is implemented more or less like this this lt lt INSERT INTO lt lt v table lt lt lt lt v field_list lt lt VALUES lt lt v value_list lt lt where v is the SSQLS you re asking the Query object to insert into the database Now let s look at a complete example which uses one of the more complicated forms of equal_list This ex ample builds a query with fewer hard coded strings than the most obvious technique requires which makes it more robust in the face of change Here is examples customS cpp include include include include include include Mata dh lt mysql h gt lt custom h gt lt iostream gt lt iomanip gt lt vector gt using namespace std using namespace mysqlpp sql_create_5 stock int main int 1y 9y string item longlong num double weight double price Date sdate argc char argv 29 My
55. inking with the Library A suitable mechanism is one that 1 uses at run time a copy of the library already present on the user s computer system rather than copying library functions into the executable and 2 will operate properly with a mod ified version of the library if the user installs one as long as the modified version is interface compatible with the version that the work was made with c Accompany the work with a written offer valid for at least three years to give the same user the materials specified in Subsection 6a above for a charge no more than the cost of performing this distribution d If distribution of the work is made by offering access to copy from a designated place offer equivalent access to copy the above specified materials from the same place 38 MySQL User Manual e Verify that the user has already received a copy of these materials or that you have already sent this user a copy For an executable the required form of the work that uses the Library must include any data and utility programs needed for reproducing the executable from it However as a special exception the materials to be distributed need not include anything that is normally distributed in either source or binary form with the major components compiler kernel and so on of the operating system on which the executable runs unless that component itself ac companies the executable It may happen that this requirement
56. l there is to it There is one subtlety MySQL automatically quotes and escapes the data when building SQL queries using SSQLS structures It s efficient too MySQL is smart enough to apply quoting and escaping only for those data types that actually require it Because this example modifies the sample database you may want to run resetdb after running this program Modifying data It almost as easy to modify data with SSQLS This is examples custom3 cpp include util h include lt mysql h gt include lt custom h gt 13 MySQL User Manual include lt iostream gt include lt string gt include lt vector gt using namespace std using namespace mysqlpp sql_create_5 stock int Ey 54 string item longlong num double weight double price Date sdate main int argc char argv try Establish the connection to the database server Connection con use_exceptions if connect_to_db argc argv con return 1 Build a query to retrieve the stock item that has Unicode characters encoded in UTF 8 form Query query con query query lt lt select from stock where item Niirnberger Brats Retrieve the row throwing an exception if it fails Result res query store if res empty throw BadQuery UTF 8 bratwurst item not found in table run resetdb Because there should only be one row in the result set
57. lements uninitialized An example of when this is necessary is when you have a structure containing only two integer elements one of the other ctors defined for SSQLS structures takes two ints so the compiler barfs if you pass 2 for this argument You would need to pass 0 here to get that SSOLS structure to compile string item longlong num double weight double price Date sdate int 15 MySQL User Manual main int argc char argv try Establish the connection to the database server Connection con use_exceptions if connect_to_db argc argv con return 1 Retrieve all rows from the stock table and put them in an STL set Notice that this works just as well as storing them in a vector which we did in customl cpp It works because SSQLS objects are less than comparable Query query con query query lt lt select from stock set lt stock gt res query storein res Display the result set Since it is an STL set and we set up the SSQLS to compare based on the item column the rows will be sorted by item print_stock_header res size set lt stock gt iterator it cout precision 3 for it res begin it res end it print_stock_row it gt item c_str it gt num it gt weight it gt price it gt sdate Use set s find method to look up a stock item by item name This also
58. ncys gosta esa a vee EE E EEE EES 34 TV ACCUSING IO A EE eee Ut dese 35 TI GNU Lesser General Public License preser eean E pE Ep eE Y EPR Ennii 35 MySQL User Manual 1 Introduction MySQL is a powerful C wrapper for MySQL s C API Its purpose is to make working with queries as easy as working with STL containers The latest version of MySQL can be found at the official web site Support for MySQL can be had on the mailing list That page hosts the mailing list archives and tells you how you can subscribe 1 1 A Brief History of MySQL MySQL was created in 1998 by Kevin Atkinson It started out MySQL specific but there were early efforts to try and make it database independent and call it SQL This is where the old library name sqlplus came from This is also why the old versions prefixed some class names with Mysql but not others the others were supposed to be the database independent parts Then in 1999 Sinisa Milivojevic unofficially took over maintenance of the library releasing versions 1 0 and 1 1 All of Kevin s releases were pre 1 0 point releases Kevin gave over maintenance to Sinisa officially with 1 2 and Sinisa went on to maintain the library through 1 7 9 released in mid 2001 Since Sinisa is an employee of MySQL AB it seems to be during this time that the dream of multiple database compatibility died With version 1 7 9 MySQL went into a period of stasis lasting over three years Durin
59. nerating We could have used the sq1_create_basic_ macros instead which would have worked just as well for what we ve seen so far and the generated code would have been smaller Why is it worth ignoring the basic variants of these macros then Consider this query insert s This does exactly what you think it does it inserts s into the database This is possible because a standard SSQLS has functions that the query object can call to get the list of fields and such which it uses to build an insert query query update and query replace also rely on this SSQLS feature A basic SSQLS lacks these func tions Another feature of standard SSQLSes you might find a use for is changing the table name used in queries By de fault the table in the MySQL database is assumed to have the same name as the SSQLS structure type But if this is inconvenient you can globally change the table name used in queries like this stock table MyStockData 5 5 Harnessing SSQLS Internals Continuing the discussion in the previous section there is a further set of methods that the non basic versions of the sq1_create macros define for each SSQLS These methods are mostly for use within the library but some of them are useful enough that you might want to harness them for your own ends Here is some pseudocode showing how the most useful of these methods would be defined for the stock structure used in all the custom cpp ex amples
60. nstead of the French or they invented things like HTML entities amp ccedil in this case to encode these additional characters using only 7 bit ASCII Unicode solves this problem It encodes every character in the world using up to 4 bytes per character The subset covering the most economically valuable cases takes two bytes per character so most Unicode aware programs limit themselves to this set for efficiency Unfortunately Unicode came about 20 years too late for Unix and C Converting the Unix system call interface to Unicode would break all existing programs The ISO lashed a wide character sidecar onto C in 1995 but in common practice C is still tied to 8 bit characters As Unicode began to take off in the early 1990s it became clear that some sort of accommodation with Unicode was needed in legacy systems like Unix and C During the development of the Plan 9 operating system a kind of suc cessor to Unix Ken Thompson invented the UTF 8 encoding Since UTF 8 is a superset of 7 bit ASCII many pro grams that deal in text actually get by okay without any explicit support for UTF 8 The MySQL database server comes out of the Unix C tradition so it only supports 8 bit characters natively UTF 8 data is compatible with C strings so all versions of MySQL could store UTF 8 data but sometimes the database ac tually needs to understand the data When sorting for instance To support this explicit UTF 8 support was added to MySQL
61. o get back some in formation from the MySQL server which execute returns to its caller in a ResNSel object In addition to the ob vious a flag stating whether the query succeeded or not this object also contains things like the number of rows that the query affected If you only need the success status there s Query exec which just returns bool If your query does pull data from the database the simplest option is store This returns a Result object which contains an in memory copy of the result set The nice thing about this is that Result is a sequential container like std vector so you can iterate through it forwards and backwards access elements with subscript notation etc There are also the storein methods which actually put the result set into an STL container of your choice The downside of these methods is that a sufficiently large result set will give your program memory problems For these large result sets you should use the use method instead This returns a ResUse object which is similar to Result but without all of the random access features This is because a use query tells the database server to send the results back one row at a time to be processed linearly It s analogous to a C stream s input iterator as opposed to a random access iterator that a container like vector offers By accepting this limitation you can process arbitrarily large result sets 3 7 Let s Do Something Us
62. onv should suffice 6 3 Unicode and Win32 Each Win32 API function that takes a string actually has two two versions One version supports only 1 byte AN 33 MySQL User Manual SI characters a superset of ASCII so they end in A Win32 also supports the 2 byte subset of Unicode called UCS 2 Some call these wide characters so the other set of functions end in W The MessageBox API for in stance is actually a macro not a real function If you define the UNICODE macro when building your program the MessageBox macro evaluates to MessageBoxW otherwise to MessageBoxaA Since MySQL uses UTF 8 and Win32 uses UCS 2 you must convert data going between the Win32 API and MySQL Since there s no point in trying for portability no other OS I m aware of uses UCS 2 you might as well use native Win32 functions for doing this translation The following code is distilled from utf8_to_win32_ansi Inexamples util cpp void utf8_to_win32_ansi const char utf8_str char ansi_str int ansi_len wchar_t ucs2_buf 100 static const int ub_chars sizeof ucs2_buf sizeof ucs2_buf 0 MultiByteToWideChar CP_UTF8 0 utf8_str 1 ucs2_buf ub_chars CPINFOEX cpi GetCPInfoEx CP_OEMCP 0 amp cpi WideCharToMultiByte cpi CodePage 0 ucs2_buf 1 ansi_str ansi_len 0 0 To see this in action uncomment define USE_WIN32_UCS2 at the top of util cpp build the example progr
63. ow row res fetch_row long unsigned int jj res fetch_lengths cout lt lt Content length lt lt jj lt lt endl lt lt endl fwrite row raw_data 0 1 jj stdout return 0 catch BadQuery amp er cerr lt lt Error lt lt er what lt lt lt lt con errnum lt lt endl return 1 catch exception er cerr lt lt Error lt lt er what lt lt endl return 1 DELETE or UPDATE from SELECT MySQL s SELECT statement has more power to winnow out just the items of interest from the database than do DELETE or UPDATE queries Therefore many people have wanted the ability to execute a SELECT statement that in fact deletes or updates the rows matched rather than returning them This example implements that feature in just a few lines of code It is examples updel cpp tinclude lt mysql h gt 19 MySQL User Manual tinclude lt string gt using namespace std using namespace mysqlpp ld t2 field define MY_DATABASE telcent define MY_TABLE nazivi define MY_HOST localhost define MY_USER root define MY_PASSWORD define MY_FIELD naziv define MY_QUERY SELECT URL from my_table as tl my_table as t2 where tl fi int main Connection con use_exceptions try ostringstream strbuf unsigned int i 0 r con real_connect MY_DATABASE MY_HOST MY_USER MY_PASSWORD 3306 0 6
64. pulator But here we want both quoting and escaping That brings us to the most widely useful manipulator string s Hotdogs Buns Query q conn query q lt lt SELECT FROM stock WHERE item lt lt quote lt lt s The regular quote manipulator both quotes strings and escapes any characters that are special in SQL 3 4 Specialized SQL Structures Retrieving data The next example introduces one of the most powerful features of MySQL Specialized SQL Structures SSQLS This is examples custom1l cpp include util h tinclude lt mysql h gt include lt custom h gt include lt iostream gt include lt iomanip gt include lt vector gt using namespace std using namespace mysqlpp The following is calling a very complex macro which will create struct stock which has the member variables Lf string item Sr Date sdate plus methods to help populate the class from a MySQL row among other things that I ll get to in a later example sql_create_5 stock i Dy explained in the user manual string item longlong num double weight double price Date sdate int main int argc char argv 11 MySQL User Manual try Establish the connection to the database server Connection con use_exceptions if connect_to_db argc argv con return 1 Retrieve th ntire contents of the stock t
65. query execute Hotdog Buns 65 1 1 1 1 1998 04 23 if created cout lt lt Created else cout lt lt Reinitialized cout lt lt sample database successfully lt lt endl catch mysqlpp BadQuery amp er Handle any connection or query errors cerr lt lt Error lt lt er what lt lt endl return 1 catch mysqlpp BadConversion amp er Handle bad conversions cerr lt lt Error lt lt er what lt lt X lt lt endl lt lt retrieved data size lt lt er retrieved lt lt actual data size lt lt er actual_size lt lt endl return 1 catch exceptiong er Catch all for any other standard C exceptions cerr lt lt Error lt lt er what lt lt endl return 1 return 0 The line just before the call to query parse sets the template and the parse call puts it into effect From that point on you can re use this query by calling any of several Query member functions that accept query template parameters In this example we re using Query execute Let s dig into this feature a little deeper 4 1 Setting up template queries To set up a template query you simply insert it into the Query object using numbered placeholders wherever you want to be able to change the query Then you call the parse function to tell the Query object that the query string is a template query and it needs to parse it
66. r any derivative work under copyright law that is to say a work containing the Library or a portion of it either verbatim or with modifications and or translated straightfor wardly into another language Hereinafter translation is included without limitation in the term modification Source code for a work means the preferred form of the work for making modifications to it For a library com plete source code means all the source code for all modules it contains plus any associated interface definition files plus the scripts used to control compilation and installation of the library Activities other than copying distribution and modification are not covered by this License they are outside its scope The act of running a program using the Library is not restricted and output from such a program is covered only if its contents constitute a work based on the Library independent of the use of the Library in a tool for writing it Whether that is true depends on what the Library does and what the program that uses the Library does 36 MySQL User Manual 1 You may copy and distribute verbatim copies of the Library s complete source code as you receive it in any me dium provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty keep intact all the notices that refer to this License and to the absence of any warranty and distribute a copy of this License
67. rams enables many more people to use the whole GNU operating system as well as its variant the GNU Linux operating system Although the Lesser General Public License is Less protective of the users freedom it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library The precise terms and conditions for copying distribution and modification follow Pay close attention to the differ ence between a work based on the library and a work that uses the library The former contains code derived from the library whereas the latter must be combined with the library in order to run GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING DISTRIBUTION AND MODIFICATION 0 This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License also called this License Each licensee is addressed as you A library means a collection of software functions and or data prepared so as to be conveniently linked with ap plication programs which use some of those functions and data to form executables The Library below refers to any such software library or work which has been distributed under these terms A work based on the Library means either the Library o
68. rsion 2 of the or dinary GNU General Public License has appeared then you can specify that version instead if you wish Do not make any other change in these notices Once this change is made in a given copy it is irreversible for that copy so the ordinary GNU General Public Li cense applies to all subsequent copies and derivative works made from that copy This option is useful when you wish to copy part of the code of the Library into a program that is not a library 4 You may copy and distribute the Library or a portion or derivative of it under Section 2 in object code or ex ecutable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corres 37 MySQL User Manual ponding machine readable source code which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange If distribution of object code is made by offering access to copy from a designated place then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code even though third parties are not compelled to copy the source along with the object code 5 A program that contains no derivative of any portion of the Library but is designed to work with the Library by being compiled or linked with it is called a work that uses the Library Such a work in isolation is not a derivat ive work of the
69. sion amp er handle bad conversions cerr lt lt Error lt lt er what lt lt X lt lt endl lt lt retrieved data size lt lt er retrieved lt lt actual data size lt lt er actual_size lt lt endl return 1 catch exception er cerr lt lt Error lt lt er what lt lt endl return 1 return 0 The example programs utility module If anything the code above is too simple because all it really does is drive some functions in examples util cpp Most of the examples link to this module because it contains code that would otherwise have to be repeated in these examples Here s a simplified version of the utility module include util h include lt iostream gt include lt iomanip gt include lt stdlib h gt using namespace std const char kpcSampleDatabase mysql_cpp_data print_stock_header 1 1 11 111 1 1111111 11T1 Display a header suitable for use with print_stock_rows void print_stock_header int rows cout lt lt Records found lt lt rows lt lt endl lt lt endl cout setf ios left cout lt lt setw 21 lt lt Item lt lt setw 10 lt lt Num lt lt setw 10 lt lt Weight lt lt setw 10 lt lt Price lt lt Date lt lt endl lt lt endl MySQL User Manual print_stock_row 1 2 2111 171111 171111111111 11011111111 TTT
70. t endl return 1 return 0 When you run the example you will notice that in the WHERE clause only the item field is checked for This is be cause SSQLS also also less than comparable Don t forget to run resetdb after running the example Less than comparable SSQLS structures can be sorted and stored in STL associative containers as demonstrated in the next example This is custom4 cpp include util h include lt mysql h gt include lt custom h gt include lt iostream gt include lt iomanip gt include lt string gt include lt vector gt using namespace std using namespace mysqlpp sql_create_5 stock 1 This number is used to make a SSOLS less than comparable When comparing two SSQLS structures the first N elements are compared In this instance we are saying that we only want the first element item to be used when comparing two stock structures 5 Each SSQLS structure includes a number of constructors Some of these are fixed in nature but one of these will have this number of arguments one for each of the first N elements in the structure it is an initialization ctor Since N is the same as the number of structure elements in this instance that ctor will be able to fully initialize the structure This behavior is not always wanted however so the macro allows you make the constructor take fewer parameters leaving the remaining e
71. t all changes require a recompile Adding a column to a table is safe for instance as the program will ignore the new column until you update the SSQLS definition The advantage of this method is that your program will require very little embedded SQL code You can simply ex ecute a query and receive your results as C data structures which can be accessed just as you would any other structure The results can be accessed through the Row object or you can ask the library to dump the results into a sequential or set associative STL container for you Consider this vector lt mystruct gt v Query q connection query q lt lt SELECT FROM mytable q storein v for vector lt mystruct gt iterator it v begin it v end v cout lt lt Price lt lt it gt price lt lt endl Isn t that slick 2 4 Exceptions By default the library throws exceptions derived from std exception whenever it encounters an error You can ask the library to set an error flag instead if you like but the exceptions carry more information Not only do they include a string member telling you why the exception was thrown there are several exception types so you can dis tinguish between different error types within a single t ry block MySQL User Manual 3 Tutorial This tutorial is meant to give you a jump start in using MySQL While it is a very complicated and powerful lib rary it s possi
72. t s 25 out of the box A smaller value may speed up compile time or you may require a higher value because you have more complex tables than that Simply change the max_data_members variable at the top of custom pl and say make The limit for Visual C is 31 according to one report There doesn t seem to be a practical limit with GCC 3 3 at least I set the limit to 100 and the only thing that happened is that custom macros h went from 1 3 MB to 18 MB and the build time for examples custom got a lot longer 32 MySQL User Manual 6 Using Unicode with MySQL 6 1 A Short History of Unicode with a focus on relevance to MySQL In the old days computer operating systems only dealt with 8 bit character sets This only gives you 256 possible characters but the modern Western languages have more than that by themselves Add in all the other lanauges of the world plus the various symbols people use and you have a real mess Since no standards body held sway over things like international character encoding in the early days of computing many different character sets were in vented These character sets weren t even standardized between operating systems so heaven help you if you needed to move localized Greek text on a Windows machine to a Russian Macintosh The only way we got any internation al communication done at all was to build standards on the common 7 bit ASCII subset Either people used approx imations like a plain c i
73. tions on a per method call basis and some let you turn them on and off for the object with a method call 2 The state of the object s exception enabling flag is passed down to child objects For example if you create a Connection object with exceptions disabled and then call its query method to get a Query object that object will also have exceptions disabled 3 The exception enabling flag is copied from object to object so if you create a child object and then change the flag in the parent the child object s exception enabling state is not changed 4 ColData will always throw an exception when it is unable to do a requested conversion A bad conversion is defined as one where a not all the characters from the string have yet been processed and b the remaining characters are something other than whitespace zeros 0 or periods This means that an exception is thrown when 1 25 is converted into an int but not when 1 00 is converted into an int In the latter case MySQL knows that it can safely throw away the fractional part 5 An out of bounds subscript when accessing a MySQL container may throw an exception Whether it does or not depends on whether the underlying STL container throws exceptions in this situation so it s compiler de pendent To see how the exceptions work try creating an error Some good things to try would be misspelling a table name or trying to cast a floating point column value to an int
Download Pdf Manuals
Related Search
Related Contents
[型番ー ERK8265WB 16043 SD Instructions Covers2007 m3 14092005-italian.qxd - Hi 取扱説明書 フル HD16 分割ユニット SXC Mode emploi M RER Affi 2-2014.indd Allarme casa radiocomandato 幅広い産業用冷温水ニーズに お応えします。 CT030-2HP Dust Collector User Manual Copyright GZ-HD7E BEDIENUNGSANLEITUNG DL6400/6600 BookITA Copyright © All rights reserved.
Failed to retrieve file