pyodbc – GettingStarted

pyodbc 使用起來和 mysql.connector 或  mysqldb 有一點差異,相同的地方都是使用 connection 來做 commit(). 差異在

  • sql paramater format, pyodbc  和 sqlite 使用 ?, mysql.connector, mysqldb 使用 %s.
  • sql paramater 輸入型態,pyodbc 是一個一個變數放,sqlite/mysql.connector/mysqldb 是使用 tuple.
  • mysql.connector, mysqldb 取得 rowcount 是使用cursor.rowcount, pyodbc 也是用 rowcount, 但還有一個 arraysize 可以設定。
    arraysize:
    “This read/write attribute specifies the number of rows to fetch at a time with\n” \
    “fetchmany(). It defaults to 1 meaning to fetch a single row at a time.

The insert example in the pyodbc Getting Started document is

cursor.execute("insert into products(id, name) values ('pyodbc', 'awesome library')")
cnxn.commit()

or better using parameters

cursor.execute("insert into products(id, name) values (?, ?)", 'pyodbc', 'awesome library')
cnxn.commit()

As the document says

Note the calls to cnxn.commit(). You must call commit or your changes will be lost! When the connection is closed, any pending changes will be rolled back. This makes error recovery very easy, but you must remember to call commit.


相關文章:

How to use *args and **kwargs in Python
https://stackoverflow.max-everyday.com/2017/09/how-to-use-args-and-kwargs-in-python/

pyodbc source code, 相信你會用的到…:
https://github.com/mkleehammer/pyodbc/tree/master/src

pyodbc – Cursor.wiki
https://code.google.com/archive/p/pyodbc/wikis/Cursor.wiki#rowcount

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *