如何使用Dart的ODBC绑定

时间:2014-09-09 17:46:07

标签: dart odbc

我想在我的应用程序中使用DART this one的ODBC绑定,我使用的是ORACLE XE数据库,但是没有得到如何设置它,尽管我在阅读中阅读了基本示例我:(

附上我的ORACLE XE ODB设置的打印屏幕,suceed连接和简单的SQL语句,

欣赏以下内容: 1.锄头在DART中设置我的ODBC, 2.如何执行相同的SQL语句。

我写了以下内容:

void main() {
  // Allocate an environment handle.
 var hEnv = new SqlHandle();   
 sqlAllocHandle(SQL_HANDLE_ENV, null, hEnv);    
 // Set ODBC version to be used.
 var version = new SqlPointer()..value = SQL_OV_ODBC3;    
 sqlSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, version, 0);    
 // Allocate a connection handle.
 var hConn = new SqlHandle();

sqlAllocHandle(SQL_HANDLE_DBC, hEnv, hConn);

// Connect.
sqlConnect(hConn, "XE", SQL_NTS, "SYSTEM", SQL_NTS, "myPasswords", SQL_NTS);  // so, I need to change this data only!

// Allocate a statement handle.
var hStmt = new SqlHandle();

sqlAllocHandle(SQL_HANDLE_STMT, hConn, hStmt);

// Prepare a statement.
sqlPrepare(hStmt, "select * from Vendors", SQL_NTS);   // I need to print the output of this SQL!!

// Execute and fetch some data.
sqlExecute(hStmt);

sqlFetch(hStmt);


// Free statement handle.
sqlFreeHandle(SQL_HANDLE_STMT, hStmt);

// Disconnect.
sqlDisconnect(hConn);

// Free connection handle.
sqlFreeHandle(SQL_HANDLE_DBC, hConn);

// Free environment handle.
sqlFreeHandle(SQL_HANDLE_ENV, hEnv); 

}

感谢

enter image description here

2 个答案:

答案 0 :(得分:1)

感谢酒吧作者Juan。 我的错误是,我使用的是Windows x64,并使用std ODBC定义了我的ODBC,没有注意到,这个发布是32位的,我使用以下方法来定义ODBC:

  

(您的窗口目录)\ SysWOW64 \ odbcad32.exe

enter image description here

以下代码完美无缺

import 'package:odbc/odbc.dart';

void main() {
     var hEnv = new SqlHandle();
     sqlAllocHandle(SQL_HANDLE_ENV, null, hEnv);

     var version = new SqlPointer()..value = SQL_OV_ODBC3;
    sqlSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, version, 0);

    var hConn = new SqlHandle();
    sqlAllocHandle(SQL_HANDLE_DBC, hEnv, hConn);

    sqlConnect(hConn, "<DSN>", SQL_NTS, "<USER>", SQL_NTS, "<PASS>", SQL_NTS);

    var hStmt = new SqlHandle();

    sqlAllocHandle(SQL_HANDLE_STMT, hConn, hStmt);

    sqlPrepare(hStmt, "SELECT * FROM vendors", SQL_NTS);

    var col1 = new SqlLongBuffer();
    var flags1 = new SqlIntBuffer();
    sqlBindCol(hStmt, 1, col1.ctype(), col1.address(), 0, flags1.address());

    var col2 = new SqlLongBuffer();
    var flags2 = new SqlIntBuffer();
    sqlBindCol(hStmt, 2, col2.ctype(), col2.address(), 0, flags2.address());

    var col3 = new SqlStringBuffer(255);
    var flags3 = new SqlIntBuffer();
    sqlBindCol(hStmt, 3, col3.ctype(), col3.address(), col3.length(), flags3.address());

    var col4 = new SqlStringBuffer(255);
    var flags4 = new SqlIntBuffer();
    sqlBindCol(hStmt, 4, col4.ctype(), col4.address(), col4.length(), flags4.address());

    sqlExecute(hStmt);

    while (sqlFetch(hStmt) != SQL_NO_DATA_FOUND) {
          print("${col1.peek()} ${col2.peek()} ${col3.peek()} ${col4.peek()} ");
     }

   sqlFreeHandle(SQL_HANDLE_STMT, hStmt);

   sqlDisconnect(hConn);

   sqlFreeHandle(SQL_HANDLE_DBC, hConn);

   sqlFreeHandle(SQL_HANDLE_ENV, hEnv);
}

创建表:

      CREATE TABLE vendors (
             vendor_id NUMBER,
             vCode NUMBER,
             vName VARCHAR(255),
             vEmail VARCHAR(255),
             PRIMARY KEY (vendor_id));

输入数据:

 INSERT INTO vendors VALUES(1, 111, 'One', 'one@vendors.com')
 INSERT INTO vendors VALUES(2, 222, 'Two', 'two@vendors.com');
 INSERT INTO vendors VALUES(3, 33, 'Three', 'three@vendors.com');

控制台输出:

     Observatory listening on http://127.0.0.1:49433
 1 111 One one@vendors.com 
 2 222 Two two@vendors.com 
 3 33 Three three@vendors.com 

答案 1 :(得分:0)

ODBC包README中的示例代码的代码行显示您必须通过cutom <YOUR DSN><YOUR USER ID><YOUR PASSWORD>

sqlConnect(hConn, "<YOUR DSN>", SQL_NTS, "<YOUR USER ID>", SQL_NTS, "<YOUR PASSWORD>", SQL_NTS);

屏幕截图的<YOUR DSN>XE。我不知道您的用户名和密码:-P