安装UnixODBC & PSQLODBC driver for UnixODBC

  1. $ brew install psqlodbc
  2. Updating Homebrew...
  3. ==> Installing dependencies for psqlodbc: postgresql, unixodbc
  4. ==> Installing psqlodbc dependency: postgresql
  5. ==> Downloading https://homebrew.bintray.com/bottles/postgresql-9.6.3.sierra.bottle.ta
  6. ######################################################################## 100.0%
  7. ==> Pouring postgresql-9.6.3.sierra.bottle.tar.gz
  8. ==> Using the sandbox
  9. ==> /usr/local/Cellar/postgresql/9.6.3/bin/initdb /usr/local/var/postgres
  10. ==> Caveats
  11. If builds of PostgreSQL 9 are failing and you have version 8.x installed,
  12. you may need to remove the previous version first. See:
  13. https://github.com/Homebrew/legacy-homebrew/issues/2510
  14. To migrate existing data from a previous major version (pre-9.0) of PostgreSQL, see:
  15. https://www.postgresql.org/docs/9.6/static/upgrading.html
  16. To migrate existing data from a previous minor version (9.0-9.5) of PostgreSQL, see:
  17. https://www.postgresql.org/docs/9.6/static/pgupgrade.html
  18. You will need your previous PostgreSQL installation from brew to perform `pg_upgrade`.
  19. Do not run `brew cleanup postgresql` until you have performed the migration.
  20. To have launchd start postgresql now and restart at login:
  21. brew services start postgresql
  22. Or, if you don't want/need a background service you can just run:
  23. pg_ctl -D /usr/local/var/postgres start
  24. ==> Summary
  25. �� /usr/local/Cellar/postgresql/9.6.3: 3,259 files, 36.6MB
  26. ==> Installing psqlodbc dependency: unixodbc
  27. ==> Downloading https://homebrew.bintray.com/bottles/unixodbc-2.3.4.sierra.bottle.1.ta
  28. ######################################################################## 100.0%
  29. ==> Pouring unixodbc-2.3.4.sierra.bottle.1.tar.gz
  30. �� /usr/local/Cellar/unixodbc/2.3.4: 43 files, 2.0MB
  31. ==> Installing psqlodbc
  32. ==> Downloading https://homebrew.bintray.com/bottles/psqlodbc-09.06.0310.sierra.bottle
  33. ######################################################################## 100.0%
  34. ==> Pouring psqlodbc-09.06.0310.sierra.bottle.tar.gz
  35. �� /usr/local/Cellar/psqlodbc/09.06.0310: 6 files, 796.1KB
  36. $ brew services start postgresql
  37. ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

其中unixODBC的库在

  1. /usr/local/Cellar/unixodbc/2.3.4

postgreSQLODBC库在

  1. /usr/local/Cellar/psqlodbc/09.06.0310

配置UnixODBC

查看PSQL的默认配置

  1. Connection options:
  2. -h, --host=HOSTNAME database server host or socket directory (default: "local socket")
  3. -p, --port=PORT database server port (default: "5432")
  4. -U, --username=USERNAME database user name (default: "He11o_Liu")
  5. -w, --no-password never prompt for password
  6. -W, --password force password prompt (should happen automatically)

配置unix ODBC

odbcinst.ini

  1. [PostgreSQL]
  2. Description=PostgreSQL driver for Linux
  3. Driver=/usr/local/Cellar/psqlodbc/09.06.0310/lib/psqlodbcw.so
  4. Setup=/usr/local/Cellar/psqlodbc/09.06.0310/lib/psqlodbcw.so
  5. UsageCount=1

odbc.ini

  1. [Liu]
  2. Description=Liu
  3. Driver=PostgreSQL
  4. Trace=Yes
  5. TraceFile=sql.log
  6. Database=student
  7. Servername=localhost
  8. UserName=root
  9. Password=
  10. Port=5432
  11. Protocol=6.4
  12. ReadOnly=No
  13. RowVersioning=No
  14. ShowSystemTables=No
  15. ShowOidColumn=No
  16. FakeOidIndex=No

测试UnixODBC链接PSQL

利用isql测试UnixODBCPSQL的连接

  1. $ isql -v Liu
  2. +---------------------------------------+
  3. | Connected! |
  4. | |
  5. | sql-statement |
  6. | help [tablename] |
  7. | quit |
  8. | |
  9. +---------------------------------------+
  10. SQL> quit

出现的问题

坑人的Mac中的动态库dylib直接链接到目录不能够正常读出。必须手动一个个加上。

书写Makefile如下

  1. ODBCLIB = /usr/local/Cellar/unixodbc/2.3.4/lib/libodbc.2.dylib /usr/local/Cellar/unixodbc/2.3.4/lib/libodbcinst.2.dylib /usr/local/Cellar/unixodbc/2.3.4/lib/libodbccr.2.dylib
  2. ODBCINC = -I/usr/local/Cellar/unixodbc/2.3.4/include/
  3. all:simple
  4. simple:simple.c
  5. gcc -o simple simple.c $(ODBCLIB) $(ODBCINC)
  6. odbc:odbc.c
  7. gcc -o odbc odbc.c $(ODBCLIB) $(ODBCINC)
  8. clean:
  9. -rm odbc
  10. -rm simple

测试与unixODBC连接

  1. #include <stdio.h>
  2. #include <sql.h>
  3. #include <sqlext.h>
  4. int main() {
  5. SQLHENV env;
  6. char driver[256];
  7. char attr[256];
  8. SQLSMALLINT driver_ret;
  9. SQLSMALLINT attr_ret;
  10. SQLUSMALLINT direction;
  11. SQLRETURN ret;
  12. SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
  13. SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
  14. direction = SQL_FETCH_FIRST;
  15. while(SQL_SUCCEEDED(ret = SQLDrivers(env, direction,
  16. (unsigned char *)driver, sizeof(driver), &driver_ret,
  17. (unsigned char *)attr, sizeof(attr), &attr_ret))) {
  18. direction = SQL_FETCH_NEXT;
  19. printf("%s - %s\n", driver, attr);
  20. if (ret == SQL_SUCCESS_WITH_INFO) printf("\tdata truncation\n");
  21. }
  22. }

测试结果如下:

  1. $ ./odbc
  2. PostgreSQL - Description=PostgreSQL driver for Linux

测试ODBC功能

  1. #include <sql.h>
  2. #include <sqltypes.h>
  3. #include <sqlext.h>
  4. #include <stdio.h>
  5. #include <sqlucode.h>
  6. #include <odbcinst.h>
  7. void ODBC_error ( /* Get and print ODBC error messages */
  8. SQLHENV henv, /* ODBC Environment */
  9. SQLHDBC hdbc, /* ODBC Connection Handle */
  10. SQLHSTMT hstmt) /* ODBC SQL Handle */
  11. {
  12. UCHAR sqlstate[10];
  13. UCHAR errmsg[SQL_MAX_MESSAGE_LENGTH];
  14. SDWORD nativeerr;
  15. SWORD actualmsglen;
  16. RETCODE rc = SQL_SUCCESS;
  17. while ( rc != SQL_NO_DATA_FOUND)
  18. {
  19. rc = SQLError(henv, hdbc, hstmt,
  20. sqlstate, &nativeerr, errmsg,
  21. SQL_MAX_MESSAGE_LENGTH - 1, &actualmsglen);
  22. if (rc == SQL_ERROR) {
  23. printf ("SQLError failed!\n");
  24. return;
  25. }
  26. if (rc != SQL_NO_DATA_FOUND) {
  27. printf ("SQLSTATE = %s\n", sqlstate);
  28. printf ("NATIVE ERROR = %d\n", nativeerr);
  29. errmsg[actualmsglen] = '\0';
  30. printf ("MSG = %s\n\n", errmsg);
  31. }
  32. }
  33. if (hdbc != SQL_NULL_HDBC)
  34. {
  35. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  36. }
  37. if (henv != SQL_NULL_HENV)
  38. {
  39. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  40. }
  41. }
  42. int main(void)
  43. {
  44. SQLHENV henv = SQL_NULL_HENV;
  45. SQLHDBC hdbc = SQL_NULL_HDBC;
  46. SQLHSTMT hstmt = SQL_NULL_HSTMT;
  47. RETCODE rc = SQL_SUCCESS;
  48. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  49. if (rc != SQL_ERROR)
  50. {
  51. printf("SQLAllocHandle() OK\n");
  52. rc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3,0);
  53. if (rc != SQL_ERROR)
  54. {
  55. printf("SQLSetEnvAttr() ok\n");
  56. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  57. if ( rc != SQL_ERROR)
  58. {
  59. printf("SQLAllocHandle() ok\n");
  60. rc = SQLSetConnectAttr(hdbc, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF,0);
  61. if (rc != SQL_ERROR)
  62. {
  63. printf("SQLSetConnectAttr() ok\n");
  64. SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
  65. SQLFreeHandle(SQL_HANDLE_ENV, henv);
  66. }
  67. }
  68. }
  69. }
  70. if (rc == SQL_ERROR)
  71. {
  72. ODBC_error (henv, hdbc, hstmt);
  73. }
  74. }

功能测试通过

  1. $ ./simple
  2. SQLAllocHandle() OK
  3. SQLSetEnvAttr() ok
  4. SQLAllocHandle() ok
  5. SQLSetConnectAttr() ok

测试获取全部数据

  1. #include <stdio.h>
  2. #include <sql.h>
  3. #include <sqlext.h>
  4. SQLHENV env;
  5. SQLHDBC dbc;
  6. SQLHSTMT stmt;
  7. void extract_error(
  8. char *fn,
  9. SQLHANDLE handle,
  10. SQLSMALLINT type)
  11. {
  12. SQLINTEGER i = 0;
  13. SQLINTEGER native;
  14. SQLCHAR state[ 7 ];
  15. SQLCHAR text[256];
  16. SQLSMALLINT len;
  17. SQLRETURN ret;
  18. fprintf(stderr,
  19. "\n"
  20. "The driver reported the following diagnostics whilst running "
  21. "%s\n\n",
  22. fn);
  23. do
  24. {
  25. ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
  26. sizeof(text), &len );
  27. if (SQL_SUCCEEDED(ret))
  28. printf("%s:%ld:%ld:%s\n", state, i, native, text);
  29. }
  30. while( ret == SQL_SUCCESS );
  31. }
  32. int main()
  33. {
  34. SQLRETURN ret; /* ODBC API return status */
  35. SQLSMALLINT columns; /* number of columns in result-set */
  36. SQLSMALLINT rows; /* number of columns in result-set */
  37. int row = 0;
  38. int j = 0,i = 0;
  39. SQLINTEGER indicator;
  40. char buf[512];
  41. /* Allocate an environment handle */
  42. SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
  43. /* We want ODBC 3 support */
  44. SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
  45. /* Allocate a connection handle */
  46. SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
  47. /* Connect to the DSN mydsn */
  48. /* You will need to change mydsn to one you have created and tested */
  49. ret = SQLDriverConnect(dbc, NULL, "DSN=Liu;", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);
  50. if (SQL_SUCCEEDED(ret)){ printf("Connected\n");}
  51. /* Allocate a statement handle */
  52. ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
  53. if (ret == -1) { printf("fail\n");}
  54. /* Retrieve a list of tables */
  55. // SQLTables(stmt, NULL, 0, NULL, 0, NULL, 0, "TABLE", SQL_NTS);
  56. ret = SQLExecDirect(stmt, "select * from student", SQL_NTS);
  57. // printf("%d\n",ret);
  58. /* How many columns are there */
  59. SQLNumResultCols(stmt, &columns);
  60. printf("Columns:%d\n",columns);
  61. // /* How many rows are there */
  62. // ret = SQLFetch(stmt);
  63. // ret = SQLRowCount(stmt,&rows);
  64. // printf("%d\n",rows);
  65. /* Loop through the rows in the result-set */
  66. while (SQL_SUCCEEDED(ret = SQLFetch(stmt))){
  67. printf("Row %d\n", row++);
  68. j = 1;
  69. /* Loop through the columns */
  70. while(j <= columns){
  71. i = j;
  72. /* retrieve column data as a string */
  73. ret = SQLGetData(stmt, i, SQL_C_CHAR, buf, sizeof(buf), &indicator);
  74. if (SQL_SUCCEEDED(ret)){
  75. /* Handle null columns */
  76. if (indicator == SQL_NULL_DATA) strcpy(buf, "NULL");
  77. printf("\tColumn %u : %10s len:%d\n",i, buf,indicator);
  78. }
  79. j++;
  80. }
  81. }
  82. }

测试输出如下

  1. $ ./fetch
  2. Connected
  3. Columns:2
  4. Row 0
  5. Column 0 : 0 len:1
  6. Column 0 : stu_0 len:20
  7. Row 1
  8. Column 0 : 1 len:1
  9. Column 0 : stu_1 len:20
  10. Row 2
  11. Column 0 : 2 len:1
  12. Column 0 : stu_2 len:20
  13. Row 3
  14. Column 0 : 3 len:1
  15. Column 0 : stu_3 len:20
  16. Row 4
  17. Column 0 : 4 len:1
  18. Column 0 : stu_4 len:20
  19. Row 5
  20. Column 0 : 5 len:1
  21. Column 0 : stu_5 len:20

测试修改数据

  1. int main()
  2. {
  3. SQLRETURN ret; /* ODBC API return status */
  4. SQLSMALLINT columns; /* number of columns in result-set */
  5. SQLSMALLINT rows; /* number of columns in result-set */
  6. int row = 0;
  7. int j = 0,i = 0;
  8. SQLINTEGER indicator;
  9. char buf[512];
  10. /* Allocate an environment handle */
  11. SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
  12. /* We want ODBC 3 support */
  13. SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
  14. /* Allocate a connection handle */
  15. SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
  16. /* Connect to the DSN mydsn */
  17. /* You will need to change mydsn to one you have created and tested */
  18. ret = SQLDriverConnect(dbc, NULL, "DSN=Liu;", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);
  19. if (SQL_SUCCEEDED(ret)){ printf("Connected\n");}
  20. /* Allocate a statement handle */
  21. ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
  22. if (ret == -1) { printf("fail\n");}
  23. /* Retrieve a list of tables */
  24. ret = SQLExecDirect(stmt, "update student set sname = 'Liu' where sno = 1;", SQL_NTS);
  25. if (ret == -1) { printf("fail\n");}
  26. ret = SQLExecDirect(stmt, "insert into student values(1010,'test');", SQL_NTS);
  27. if (ret == -1) { printf("fail\n");}
  28. ret = SQLExecDirect(stmt, "delete from student where sno = 1010;", SQL_NTS);
  29. if (ret == -1) { printf("fail\n");}
  30. return 0;
  31. }

连接SQLite3

这个地方很坑

下载SQLiteODBC

  1. $ brew info sqliteodbc
  2. sqliteodbc: stable 0.9995 (bottled)
  3. SQLite ODBC driver
  4. http://www.ch-werner.de/sqliteodbc/
  5. /usr/local/Cellar/sqliteodbc/0.9995 (11 files, 356.7KB) *
  6. Poured from bottle on 2017-05-22 at 20:52:10
  7. From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/sqliteodbc.rb
  8. ==> Dependencies
  9. Required: sqlite ✔, unixodbc

配置odbc.iniodbcinst.ini

odbc.ini

  1. [PostgreSQL]
  2. Description=PostgreSQL driver for Linux
  3. Driver=/usr/local/Cellar/psqlodbc/09.06.0310/lib/psqlodbcw.so
  4. Setup=/usr/local/Cellar/psqlodbc/09.06.0310/lib/psqlodbcw.so
  5. UsageCount=1
  6. [SQLlite]
  7. Description=ODBC for SQLite
  8. Driver=/usr/local/Cellar/sqliteodbc/0.9995/lib/libsqlite3odbc.so
  9. Setup=/usr/local/Cellar/sqliteodbc/0.9995/lib/libsqlite3odbc.so
  10. UsageCount=5

odbcinst.ini

  1. [Liu]
  2. Description=Liu
  3. Driver=PostgreSQL
  4. Trace=Yes
  5. TraceFile=sql.log
  6. Database=student
  7. Servername=localhost
  8. UserName=root
  9. Password=
  10. Port=5432
  11. Protocol=6.4
  12. ReadOnly=No
  13. RowVersioning=No
  14. ShowSystemTables=No
  15. ShowOidColumn=No
  16. FakeOidIndex=No
  17. [Nian]
  18. Description=Nian
  19. Driver=SQLlite
  20. Trace=Yes
  21. Database=student.db
  22. TraceFile=sql.log
  23. Servername=localhost
  24. Port=1433
  25. ReadOnly=No
  26. RowVersioning=No
  27. ShowSystemTables=No
  28. ShowOidColumn=No
  29. FakeOidIndex=No

这里要注意 SQLite的数据库要选择具体文件

SQLite测试连接

  1. #include <stdio.h>
  2. #include <sql.h>
  3. #include <sqlext.h>
  4. int main()
  5. {
  6. SQLHENV env;
  7. SQLHDBC dbc;
  8. SQLHSTMT stmt;
  9. SQLRETURN ret; /* ODBC API return status */
  10. SQLCHAR outstr[1024];
  11. SQLSMALLINT outstrlen;
  12. /* Allocate an environment handle */
  13. SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
  14. /* We want ODBC 3 support */
  15. SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
  16. /* Allocate a connection handle */
  17. SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
  18. /* Connect to the DSN mydsn */
  19. ret = SQLDriverConnect(dbc, NULL, (SQLCHAR*)"DSN=Nian;", SQL_NTS,
  20. outstr, sizeof(outstr), &outstrlen,
  21. SQL_DRIVER_COMPLETE);
  22. if (SQL_SUCCEEDED(ret))
  23. {
  24. printf("Connected\n");
  25. printf("Returned connection string was:\n\t%s\n", outstr);
  26. if (ret == SQL_SUCCESS_WITH_INFO){
  27. printf("Driver reported the following diagnostics\n");
  28. }
  29. SQLDisconnect(dbc); /* disconnect from driver */
  30. }
  31. else
  32. {
  33. fprintf(stderr, "Failed to connect\n");
  34. }
  35. /* free up allocated handles */
  36. SQLFreeHandle(SQL_HANDLE_DBC, dbc);
  37. SQLFreeHandle(SQL_HANDLE_ENV, env);
  38. }

测试fetch数据

  1. #include <stdio.h>
  2. #include <sql.h>
  3. #include <sqlext.h>
  4. SQLHENV env;
  5. SQLHDBC dbc;
  6. SQLHSTMT stmt;
  7. int main()
  8. {
  9. SQLRETURN ret; /* ODBC API return status */
  10. SQLSMALLINT columns; /* number of columns in result-set */
  11. SQLSMALLINT rows; /* number of columns in result-set */
  12. int row = 0;
  13. int j = 0,i = 0;
  14. SQLINTEGER indicator;
  15. char buf[512];
  16. /* Allocate an environment handle */
  17. SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
  18. /* We want ODBC 3 support */
  19. SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
  20. /* Allocate a connection handle */
  21. SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
  22. /* Connect to the DSN mydsn */
  23. /* You will need to change mydsn to one you have created and tested */
  24. ret = SQLDriverConnect(dbc, NULL, "DSN=Nian;", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);
  25. if (SQL_SUCCEEDED(ret)){ printf("Connected\n");}
  26. /* Allocate a statement handle */
  27. ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
  28. if (ret == -1) { printf("fail\n");}
  29. /* Retrieve a list of tables */
  30. SQLTables(stmt, NULL, 0, NULL, 0, NULL, 0, "TABLE", SQL_NTS);
  31. ret = SQLExecDirect(stmt, "select * from student;", SQL_NTS);
  32. printf("return %d\n",ret);
  33. // printf("%d\n",ret);
  34. /* How many columns are there */
  35. SQLNumResultCols(stmt, &columns);
  36. printf("Columns:%d\n",columns);
  37. // /* How many rows are there */
  38. // ret = SQLFetch(stmt);
  39. // ret = SQLRowCount(stmt,&rows);
  40. // printf("%d\n",rows);
  41. /* Loop through the rows in the result-set */
  42. while (SQL_SUCCEEDED(ret = SQLFetch(stmt))){
  43. printf("Row %d\n", row++);
  44. j = 1;
  45. /* Loop through the columns */
  46. while(j <= columns){
  47. i = j;
  48. /* retrieve column data as a string */
  49. ret = SQLGetData(stmt, i, SQL_C_CHAR, buf, sizeof(buf), &indicator);
  50. if (SQL_SUCCEEDED(ret)){
  51. /* Handle null columns */
  52. if (indicator == SQL_NULL_DATA) strcpy(buf, "NULL");
  53. printf("\tColumn %u : %10s len:%d\n",i, buf,indicator);
  54. }
  55. j++;
  56. }
  57. }
  58. }

简单数据库迁移

这里只是最简单的数据库迁移演示,直接用字符串缓冲区进行操作。

  1. #include <stdio.h>
  2. #include <sql.h>
  3. #include <sqlext.h>
  4. char readdata[200][2][30];
  5. int row;
  6. void read_data();
  7. void write_data();
  8. int main(){
  9. read_data();
  10. write_data();
  11. }
  12. void write_data(){
  13. SQLHENV env;
  14. SQLHDBC dbc;
  15. SQLHSTMT stmt;
  16. SQLRETURN ret; /* ODBC API return status */
  17. SQLSMALLINT columns; /* number of columns in result-set */
  18. SQLSMALLINT rows; /* number of columns in result-set */
  19. int j = 0,i = 0;
  20. SQLINTEGER indicator;
  21. char buf[512];
  22. /* Allocate an environment handle */
  23. SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
  24. /* We want ODBC 3 support */
  25. SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
  26. /* Allocate a connection handle */
  27. SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
  28. /* Connect to the DSN mydsn */
  29. /* You will need to change mydsn to one you have created and tested */
  30. ret = SQLDriverConnect(dbc, NULL, "DSN=Nian;", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);
  31. if (SQL_SUCCEEDED(ret)){ printf("Connected\n");}
  32. /* Allocate a statement handle */
  33. ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
  34. if (ret == -1) { printf("fail\n");}
  35. /* Retrieve a list of tables */
  36. printf("row%d\n",row);
  37. for(i = 0; i< row;i++){
  38. printf("Row %d\n",i);
  39. sprintf(buf,"insert into student values(%s,\'%s\');",readdata[i][0],readdata[i][1]);
  40. printf("%s\n",buf);
  41. ret = SQLExecDirect(stmt,buf, SQL_NTS);
  42. if (ret == -1) { printf("fail\n");}
  43. }
  44. }
  45. void read_data(){
  46. SQLHENV env;
  47. SQLHDBC dbc;
  48. SQLHSTMT stmt;
  49. SQLRETURN ret; /* ODBC API return status */
  50. SQLSMALLINT columns; /* number of columns in result-set */
  51. SQLSMALLINT rows; /* number of columns in result-set */
  52. int j = 0,i = 0;
  53. SQLINTEGER indicator;
  54. char buf[512];
  55. row = 0;
  56. /* Allocate an environment handle */
  57. SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
  58. /* We want ODBC 3 support */
  59. SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
  60. /* Allocate a connection handle */
  61. SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
  62. /* Connect to the DSN mydsn */
  63. /* You will need to change mydsn to one you have created and tested */
  64. ret = SQLDriverConnect(dbc, NULL, "DSN=Liu;", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);
  65. if (SQL_SUCCEEDED(ret)){ printf("Connected\n");}
  66. /* Allocate a statement handle */
  67. ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
  68. if (ret == -1) { printf("fail\n");}
  69. /* Retrieve a list of tables */
  70. // SQLTables(stmt, NULL, 0, NULL, 0, NULL, 0, "TABLE", SQL_NTS);
  71. ret = SQLExecDirect(stmt, "select * from student", SQL_NTS);
  72. // printf("%d\n",ret);
  73. /* How many columns are there */
  74. SQLNumResultCols(stmt, &columns);
  75. printf("Columns:%d\n",columns);
  76. // /* How many rows are there */
  77. // ret = SQLFetch(stmt);
  78. // ret = SQLRowCount(stmt,&rows);
  79. // printf("%d\n",rows);
  80. /* Loop through the rows in the result-set */
  81. while (SQL_SUCCEEDED(ret = SQLFetch(stmt))){
  82. printf("Row %d\n", row++);
  83. j = 1;
  84. /* Loop through the columns */
  85. while(j <= columns){
  86. i = j;
  87. /* retrieve column data as a string */
  88. ret = SQLGetData(stmt, i, SQL_C_CHAR, buf, sizeof(buf), &indicator);
  89. if (SQL_SUCCEEDED(ret)){
  90. /* Handle null columns */
  91. if (indicator == SQL_NULL_DATA) strcpy(buf, "NULL");
  92. buf[indicator] = '\0';
  93. strcpy(readdata[row][j-1],buf);
  94. printf("\tColumn %u :%s len:%d\n",i, readdata[row][j-1],indicator);
  95. }
  96. j++;
  97. }
  98. }
  99. }

SQLite

  1. $ sqlite3 student.db
  2. SQLite version 3.16.0 2016-11-04 19:09:39
  3. Enter ".help" for usage hints.
  4. sqlite> select * from student;
  5. 1|Liu
  6. 0|stu_0
  7. 2|stu_2
  8. 3|stu_3
  9. 4|stu_4
  10. 5|stu_5
  11. 6|stu_6
  12. 7|stu_7
  13. 8|stu_8
  14. 9|stu_9
  15. ...

Makefile

  1. ODBCLIB = /usr/local/Cellar/unixodbc/2.3.4/lib/libodbc.2.dylib /usr/local/Cellar/unixodbc/2.3.4/lib/libodbcinst.2.dylib /usr/local/Cellar/unixodbc/2.3.4/lib/libodbccr.2.dylib
  2. ODBCINC = -I/usr/local/Cellar/unixodbc/2.3.4/include/
  3. all:simple odbc connect fetch update
  4. simple:simple.c
  5. gcc -o simple simple.c $(ODBCLIB) $(ODBCINC)
  6. odbc:odbc.c
  7. gcc -o odbc odbc.c $(ODBCLIB) $(ODBCINC)
  8. connect:connect.c
  9. gcc -o connect connect.c $(ODBCLIB) $(ODBCINC)
  10. connectlite:connectlite.c
  11. gcc -o connectlite connectlite.c $(ODBCLIB) $(ODBCINC)
  12. fetch:fetchresult.c
  13. gcc -o fetch fetchresult.c $(ODBCLIB) $(ODBCINC)
  14. fetchlite:fetchresultlite.c
  15. gcc -o fetchlite fetchresultlite.c $(ODBCLIB) $(ODBCINC)
  16. update:updatedata.c
  17. gcc -o update updatedata.c $(ODBCLIB) $(ODBCINC)
  18. trans:transdata.c
  19. gcc -o trans transdata.c $(ODBCLIB) $(ODBCINC)
  20. clean:
  21. -rm odbc
  22. -rm simple
  23. ...

macOS上的ODBC-利用unixODBC连接PostgreSQL与SQLite并进行数据迁移的更多相关文章

  1. python2/3 利用psycopg2 连接postgreSQL数据库。

    psycopg2 是一个通过python连接postgreSQL的库, 不要被它的名称蒙蔽了,你可能发现它的版本是psyconpg2.7.*, 以为它只能在python2上使用,实际上,这只是一个巧合 ...

  2. 利用Kettle进行SQLServer与Oracle之间的数据迁移实践

    Kettle简介 Kettle(网地址为http://kettle.pentaho.org/)是一款国外开源的ETL工具,纯java编写,可以在Windows.Linux.Unix上运行,数据抽取高效 ...

  3. Entity Framework 6连接Postgresql、SQLite、LocalDB的注意事项和配置文件

    Postgresql Postgresql支持Code First的方式自动生成表,不过默认的模式是dbo而不是public,而且还可以自动生成自增主键. <?xml version=" ...

  4. (一)通过JAVA连接SAP (sapjco3.jar在Windows和MacOS上的配置)

    (一)通过JAVA连接SAP调用接口 (sapjco3.jar在Windows和MacOS上的配置) 一.sapjoc3.jar获取 由于sap官网提供的链接需要合作公司提供账号密码,如果商用请索要正 ...

  5. 在MacOS上利用docker构建buildroot

    之前有听说过docker,但是一直没有使用过.最近终于下定决定使用了一下docker,感觉docker用于跨操作系统的软件工具使用还是比较友好的. 适用人群 本文忽略的部分Linux软件包安装的过程, ...

  6. debian C++ OTL库 用 unixodbc 连接 mysql 小记

    这个东东也是折腾了几天,网上很多文章可能已经过时,所以写下不同,以备后用. 参考网址: http://blog.csdn.net/genganpeng/article/details/7402229 ...

  7. iOS macOS的后渗透利用工具:EggShell

    EggShell是一款基于Python编写的iOS和macOS的后渗透利用工具.它有点类似于metasploit,我们可以用它来创建payload建立侦听.此外,在反弹回的session会话也为我们提 ...

  8. 【戾气满满】Ubuntu 18.04使用QT通过FreeTDS+unixODBC连接MSSQL填坑记(含泪亲测可用)

    前言 照例废话几句,想玩下QT,但是学习吧总得想点事情做啊,单纯学习语法用法这些?反正我是学不下去的,脑袋一拍,就先学下怎么连接数据库吧!然而万万没想到,我这是给自己挖了一个深深的坑啊! 学习自然去官 ...

  9. powerdesigner连接postgresql数据库生成pdm及word文档

    1.准备软件: powerdesigner165与postgresql的驱动:psqlodbc_11_01_0000 2.安装并破解完成powerdesigner165 参看链接:https://ww ...

随机推荐

  1. 使用递归算法结合数据库解析成java树形结构

    使用递归算法结合数据库解析成java树形结构 1.准备表结构及对应的表数据a.表结构: create table TB_TREE ( CID NUMBER not null, CNAME VARCHA ...

  2. 数据库分库分表中间件 Sharding-JDBC 源码分析 —— SQL 解析(四)之插入SQL

  3. python---scrapy之MySQL同步存储

    假设我们已经能获取到item里定义的字段的数据,接下来就需要保存item的数据到mysql数据库. pipeline用来存储item中的数据,将爬取到的数据进行二次处理 首先,要做的准备的工作,安装M ...

  4. 音频软件开发中的debug方法和工具

    本文系作者原创.如转载,请注明出处. 谢谢! 音频软件开发同其他软件开发一样,都需要去调试.音频软件调试同其他软件调试方法有相同的地方,也有不同的地方,同时调试时还需要借助一些专门的工具,有了这些方法 ...

  5. 用UE4来做Zego即构的房间列表

    Zego即构是一家做直播的服务商,Zego即构自己的房间列表,本文只是测试功能用,相应代码并没完全测试,请选择性参考. 我们在UE4中来实现一下,我感觉这个过程有点意思,UE4中C++与蓝图和UI的互 ...

  6. 采用SmartQQ 协议可制作聊天机器人

    采用.NET CORE可运行在 Linux . Windows 和 Mac OSX 平台下. SmartQQ可以: 收发文字消息 获取好友.群.讨论组.好友分组和最近会话的列表 SmartQQ不可以: ...

  7. Entity Framework - 基于外键关联的单向一对一关系

    代码的世界,原以为世界关系很简单,确道是关系无处不在.NET世界里ORM框架中EntityFramework作为其中翘楚,大大解放了搬砖工作的重复工作,着实提高了不少生产力,而也碰到过不少问题!比如关 ...

  8. centos上安装配置java WEB环境_java(转)

    趁着十一期间,好好的写写随笔来记录自己所学.所践和所得,不足之处,欢迎各位拍砖~~~ 工具:Xftp 5.Xshell 5 一.安装jdk 1. 使用Xftp 5把jdk-8u65-linux-x64 ...

  9. 解决若要安装 Microsoft Office 2010,需要MSXML 版本 6.10.1129的问题

    单击 开始单击 运行键入 注册表编辑器然后单击 确定. 找到HKEY_CLASSES_ROOT\TypeLib\{F5078F18-C551-11D3-89B9-0000F81FE221}\6.0\0 ...

  10. MySQl数据库常用的DOS命令

    MySQl数据库常用的DOS命令.. 这是第一部分.. 数据库的连接信息:jdbc:mysql://localhost:3306/shxtcom.mysql.jdbc.Driver /*jdbc:sq ...