C application libmysqlclient versus ODBC

纵饮孤独 提交于 2019-12-07 22:43:09

问题


I am writing an application that needs to interface with a MySQL database. Should I use libmysql or the ODBC connector ? Are there any pros / cons beside the fact that using ODBC will allow me to change the DB in the future without much code change ?


回答1:


I change my DB all the time, and I use the C connector, which works flawlessly and without too much code change. That has been the better choice for me and, perhaps, for you ( of course only imo).

I can't believe that code changes would be much different between the two interfaces, iff you are going to stick with MySQL.

EDIT: I chose the C connector originally because it was intuitive and natural for me; and because I wasn't too sure that there was a working, hardened ODBC for Linux: my code has to run under both Windows and Linux. Now I see that there is indeed such a thing. That makes ODBC more attractive: maybe I would have chosen differently if I'd known about it.




回答2:


In my opinion, it's a matter of personal preference.

When using libmysql, a good approach would be to first write a set of generic libmysqlclient wrapper functions (layer A), above that a set of functions that implement the database functionality that you're going to use in your program (layer B), and above that your actual program (layer C).

Switching from libmysql to ODBC only requires minor changes in layer B without modifying layer B's API. Hence, if you intend to use your program with MySQL only, I'd suggest that you stick with libmysql until someone provides you with a patch for ODBC.


On the technical side, you have to make a trade-off between database connectivity and portability. Using libmysql enables your code to compile without modifications on a variety of platforms, such as Unix, MacOS and Windows. ODBC libraries are typically written either for Windows or for Unix/MacOS.



来源:https://stackoverflow.com/questions/8679244/c-application-libmysqlclient-versus-odbc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!