pypyodbc help to find tutorials for begining [closed]

巧了我就是萌 提交于 2019-12-14 03:12:47

问题


The question may sound stupid but can someone please provide me with pypyodbc with postgresql....? I have been searching it in internet forever and havenot found anything* literally anything.


回答1:


I can't imagine why you'd want to use pypyodbc; look at using a DB-API driver directly instead, such as psycopg2 or the less-well-maintained but more PyPy-friendly pyPgSQL, or the newer less mature py-postgresql. See the database driver list for more info.

If for some reason you need to do something weird and obscure like using ODBC from PyPy to connect to PostgreSQL...

Sometimes, as programmers, we have to do something scary - go off the tutorial track and think about the problem ourselves ;-)

When facing this terrible challenge there are a few tools that greatly ease the process:

  • The documentation for each component we're using; and
  • Tutorials/guides written for some subset of the involved components, but not all of them.

In this case, that suggests that your key resources are:

  • The pypyodbc code and packages, which contain links to:

  • various pypyodbc documentation on the gcode wiki, introductory examples, a link suggesting it's actually mostly the same as pyodbc, etc.

  • the psqlODBC page, with useful links.

  • The PostgreSQL documentation

  • Microsoft's docs on ODBC

Some of the examples are the sort of thing you want, but connect to different database engines. You will have to adapt them to PostgreSQL and psqlODBC. The documentation on how each component works will help you do that; for example:

  • Learn how to connect to a DSN using an example that refers to MS SQL Server
  • Learn how to create a DSN in psqlODBC from the psqlODBC docs
  • Combine that knowledge to connect to a psqlODBC DSN

Having learned that pypyodbc is very similar to pyodbc in function, this lets you widen your search for examples covering pyodbc, too:

  • PyODBC docs
  • Connecting to PostgreSQL using pyodbc
  • ...

Now, your task is to synthesize these elements, learning relevant parts from different pieces of documentation, so you can put together a working whole from the individual things you have learned.




回答2:


If you're looking for a very basic example to get you started then the following code works for me:

# -*- coding: utf-8 -*-
import pypyodbc
cnxn = pypyodbc.connect(
    'Driver={PostgreSQL ODBC Driver(UNICODE)};' +
    'Server=localhost;' +
    'Port=5432;' +
    'Database=myDBname;' +
    'Uid=postgres;' +
    'Pwd=whatever;')
crsr = cnxn.cursor()
crsr.execute("SELECT id, customer FROM public.table1")
while 1:
    row = crsr.fetchone()
    if not row:
        break
    print row
crsr.close()
cnxn.close()


来源:https://stackoverflow.com/questions/21557879/pypyodbc-help-to-find-tutorials-for-begining

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!