Format: text/x-python
⬇ Download File (2.2 KB)
   1  """db_print.py -- a simple demo for ADO database reads."""
   2  
   3  import sys
   4  
   5  import adodbapi.ado_consts as adc
   6  
   7  cmd_args = ("filename", "table_name")
   8  if "help" in sys.argv:
   9      print("possible settings keywords are:", cmd_args)
  10      sys.exit()
  11  
  12  kw_args = {}  # pick up filename and proxy address from command line (optionally)
  13  for arg in sys.argv:
  14      s = arg.split("=")
  15      if len(s) > 1:
  16          if s[0] in cmd_args:
  17              kw_args[s[0]] = s[1]
  18  
  19  kw_args.setdefault(
  20      "filename", "test.mdb"
  21  )  # assumes server is running from examples folder
  22  kw_args.setdefault("table_name", "Products")  # the name of the demo table
  23  
  24  # the server needs to select the provider based on his Python installation
  25  provider_switch = ["provider", "Microsoft.ACE.OLEDB.12.0", "Microsoft.Jet.OLEDB.4.0"]
  26  
  27  # ------------------------ START HERE -------------------------------------
  28  # create the connection
  29  constr = "Provider=%(provider)s;Data Source=%(filename)s"
  30  import adodbapi as db
  31  
  32  con = db.connect(constr, kw_args, macro_is64bit=provider_switch)
  33  
  34  if kw_args["table_name"] == "?":
  35      print("The tables in your database are:")
  36      for name in con.get_table_names():
  37          print(name)
  38  else:
  39      # make a cursor on the connection
  40      with con.cursor() as c:
  41          # run an SQL statement on the cursor
  42          sql = "select * from %s" % kw_args["table_name"]
  43          print('performing query="%s"' % sql)
  44          c.execute(sql)
  45  
  46          # check the results
  47          print(
  48              'result rowcount shows as= %d. (Note: -1 means "not known")' % (c.rowcount,)
  49          )
  50          print("")
  51          print("result data description is:")
  52          print("            NAME Type         DispSize IntrnlSz Prec Scale Null?")
  53          for d in c.description:
  54              print(
  55                  ("%16s %-12s %8s %8d %4d %5d %s")
  56                  % (d[0], adc.adTypeNames[d[1]], d[2], d[3], d[4], d[5], bool(d[6]))
  57              )
  58          print("")
  59          print("str() of first five records are...")
  60  
  61          # get the results
  62          db = c.fetchmany(5)
  63  
  64          # print them
  65          for rec in db:
  66              print(rec)
  67  
  68          print("")
  69          print("repr() of next row is...")
  70          print(repr(c.fetchone()))
  71          print("")
  72  con.close()
  73