Environment:
Development Board: Freescale 2.6 armv71, the system is read only , the only path to read and write is/TMP/SD (this is an SD card). The program is placed under/tmp/sd/transfer (instead of the run path below), and the SQL statement is saved as a file on the Development Board, and the statement character set is GBK.
Server: The server is installing SQL Server 2008.
Function: The program can connect to the server database, execute SQL statements and so on.
Considering that the SD card capacity of the Development Board is limited, it is not easy to install the compiler, so decided to cross-compile.
Cross-compilation environment: Ubuntu 12.04 (can also choose other). The transfer.c and transfer.h files to be compiled are placed under/home/arm (instead of the compilation path below)
First install the cross compiler, go to the official website to download the latest ARM-LINUX-GCC.
I am using arm-linux-gcc-4.4.3-20100728.tar.gz, I can use it after downloading (I unzipped to/home/).
For ease of use, add the Opt/toolschain/4.4.3/bin in the extracted directory to the system environment variable.
In order to access SQL SERVER, we need to install FreeTDS. Of course, this freetds compiled dynamic library must also be used in the Development Board, so use the cross compiler to compile
Go to the official website to download the latest freetds-stable.tgz.tgz, unzip after download, and then go to unzip directory
Execute the following command
./configure--prefix=/home/freetds-arm--sysconfdir=/tmp/sd/transfer--with-tdsver=7.1--enable-msdblib-- Disable-libiconv--host=arm-none-linux-gnueabi
Explain the meaning of each parameter.
--prefix=/home/freetds-arm, set FreeTDS installed directory, consider the development Board is read-only, so it is best to manually specify the link library path instead of the default path. Another reason for this is Ubuntu, which is compiled by the arm version.
--sysconfdir=/tmp/sd/transfer, if it is determined that the SQL statement does not use Chinese, this parameter can be used. This parameter sets the path to the FreeTDS configuration file. (The default is the Ext folder under the path specified by prefix). Why do you want to set the parameter folder to/tmp/sd/transfer separately, and then say
--with-tdsver=7.1, set the version of the TDS protocol for SQL SERVER2008. Other versions please modify the version number yourself
--enable-msdblib, the MS (Microsoft) database is allowed to connect because there are other databases that use the TDS protocol.
--disable-libiconv, turn off character set auto-conversion (words that don't turn off will be garbled in Chinese)
--host=arm-none-linux-gnueabi, specify the cross compiler (if you have not previously set the system environment variable, here to manually specify the compiler Path)
And then
Make
Make install
Solve Chinese garbled characters
FreeTDS connection By default is ISO-8859-1 encoding, is not supported in Chinese. In the FreeTDS installation directory of the Ext folder (if you use--sysconfdir, just under its specified path) there is a freetds.conf, you can set the connection character sets. The method is to add a line of client CHARSET=GBK under the [Global] entry (assuming your SQL statement is in GBK format, use the corresponding character set for other formats).
However, the use of abnormal on the board, inexplicable program crashes. Later view FreeTDS source code, found by default support UTF-8.
First, the client CHARSET=GBK is changed to client Charset=utf-8, and then the SQL statement is converted from GBK format to UTF-8 format.
Here we use Libiconv to convert (and other methods, please refer to the network)
To include the header file Iconv.h
void Gbk_to_utf8 (char * src,int Srclen,char * dsc,int Dsclen)
{
iconv_t cd;
cd = Iconv_open ("UTF-8", "GBK");
memset (Dsc,0,dsclen);
Iconv (Cd,&src,&srclen,&dsc,&dsclen);
Iconv_close (CD);
}
Of course, Libiconv has to use cross-compilation.
First download libiconv-1.14.tar.gz, unzip and then go to unzip directory
./configure--prefix=/home/libiconv--host=arm-none-linux-gnueabi
Make
Make install
The parameters do not mean much to explain, refer to above.
Installation completed from the installation directory to find libiconv.so.2.5.1, copy to the compiled directory and renamed to Libiconv.so.2
Locate iconv.h from the installation directory and copy it to the compiled directory
Write the makefile file as follows
cc=/home/opt/friendlyarm/toolschain/4.4.3/bin/arm-linux-gcc
ccd=gcc
flags=-wl,-rpath=/tmp/sd/transfer-i/home/freetds-arm/include//HOME/FREETDS-ARM/LIB/LIBSYBDB.A librt.so Libiconv.so.2
flagsd=-i/home/freetds/include//home/freetds/lib/libsybdb.a-lrt-liconv
TRANSFER:TRANSFER.C Transfer.h
$ (CC)-O transfer-g transfer.c $ (FLAGS)
DEBUG:TRANSFER.C Transfer.h
$ (CCD)-O transferd-g transfer.c $ (FLAGSD)
Clean:
RM-RF *~ Transfer Transferd
Execute make to get transfer, this is to be put on the board with
Execute make debug to get Transferd, this can be used directly on Ubuntu, easy to debug
In order to facilitate the use of GDB debugging ubuntu12.04, add the debug target, FreeTDS and other installation methods refer to above, only need to remove--host=arm-none-linux-gnueabi.
Explain the following
FLAGS
-wl,-rpath=/tmp/sd/transfer indicates that the program executes when it takes precedence to find a dependent library from this path, and if this is not used, it will be reported libiconv.so.2 not found.
-i/home/freetds-arm/include/here Specifies the FreeTDS header file path
/HOME/FREETDS-ARM/LIB/LIBSYBDB.A specifying FREETDS Shared library Path
Librt.so This is a runtime library that needs to be copied from the Development Board, or it can be found under the installation directory of the cross compiler. (Execute command Find/-name "*librt.so*" can be found)
Libiconv.so.2, this is Libiconv's library.
FLAGSD, because the machine is installed under the/usr/lib, so directly using the-LRT connection Librt.so,-liconv connection libiconv.so.2
At last
Put the compiled directory libiconv.so.2, transfer into the/tmp/sd/transfer of the board
Copy Ubuntu/tmp/sd/transfer/freetds.conf to the board/tmp/sd/transfer
So transfer can do it.
Development Board remote Operation SQL Server solution