Get the App
SLTechnology News&Howtos  ›  Database  › 

Embedded PowerPC accesses SQL Server database through unixODBC/freetds (3)

Shulou Source: shulou.com Published: 2022-06-01 11:01:11 09月29日 Update

Before reading this article, please read and understand the compilation and deployment of unixODBC/freetds. In addition, some of the data used in the following code comes from the article.

If you need to verify the part of the code, you can revise only part of the content (data source, user name, password, insert statement, etc.).

1. Test the source code

* # include # include SQLHDBC handlestmt; SQLHENV handleenv; SQLHDBC handlehdbc; bool ConnectDB () {long nRetState= 0; nRetState=SQLAllocHandle (SQLHANDLEENV,SQLNULLHANDLE,&handleenv); if ((nRetState! = SQLSUCCESS) & & (nRetState! = SQLSUCCESSWITHINFO)) return false; nRetState=SQLSetEnvAttr (handleenv, SQLATTRODBCVERSION, (void) SQLOVODBC3, 0) If ((nRetState! = SQLSUCCESS) & & (nRetState! = SQLSUCCESSWITHINFO)) return false; nRetState = SQLAllocHandle (SQLHANDLEDBC, handleenv, & handlehdbc); if ((nRetState! = SQLSUCCESS) & & (nRetState! = SQLSUCCESSWITHINFO) return false; SQLSetConnectAttr (handlehdbc, SQLLOGINTIMEOUT, (SQLPOINTER) 5,0) NRetState = SQLConnect (handlehdbc, (SQLCHAR) "odbc", SQLNTS, / / data source (SQLCHAR) "sa", SQLNTS / / user name (SQLCHAR) "root", SQLNTS) / / password if ((nRetState! = SQLSUCCESS) & & (nRetState! = SQLSUCCESSWITHINFO) return false; printf ("DB Connected!\ n"); return true;} bool InsertDB (SQLCHAR szSqlCmd) {long nRetState= 0; nRetState=SQLAllocHandle (SQLHANDLESTMT, handlehdbc, & handlestmt); if ((nRetState! = SQLSUCCESS) & & (nRetState! = SQLSUCCESSWITHINFO) return false NRetState=SQLExecDirect (handlestmt,szSqlCmd,SQLNTS); if ((nRetState! = SQLSUCCESS) & & (nRetState! = SQLSUCCESSWITHINFO) return false; printf ("InsertDB Success\ n"); return true;} bool CloseDB () {SQLFreeHandle (SQLHANDLESTMT,handlestmt); SQLDisconnect (handlehdbc); SQLFreeHandle (SQLHANDLEDBC,handlehdbc); SQLFreeHandle (SQLHANDLEENV, handleenv) Printf ("Excute Finished!\ n"); return true;} int main (int argc,char argv []) {char pSQLCommond = "insert into ReportServer.dbo.userTmp (ID) values (99)"; SQLCHAR pSQLCommondExe = (SQLCHAR) pSQLCommond; if (ConnectDB ()) {InsertDB (pSQLCommondExe); CloseDB ();} return (0);}

*

2, cross compilation

Note: cross-compilation allows links to odbc dynamic libraries.

3, deployment

Copy the compiled file to any location in the powerpc device, use chmod 755 * to add execution permissions to the compiled file, and link the odbc library to / usr/lib.

4, test and verify

The following execution result is received, which indicates that the database operation is successful. Note: it should be executed under the User user.

Tags: Compile data user part code password data source file user name link cross test verify database success location content dynamic copy article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MySQL macOS Shulou Technology Docker