rvojcik / rtapi

Racktables API

Home Page:https://rtapi.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

example code not working - shows error on print rt.ListObjects()

K1WIZ opened this issue · comments

I tried the example code on the readme and I can't get it to work. Environment is:
Python 3.5.2
rtapi was installed via pip3

My file contains (credentials scrubbed):

import MySQLdb
import rtapi

# Create connection to database
try:
# Create connection to database
   db = MySQLdb.connect(host='rthost',port=3306, passwd='passwd',db='racktables',user='rtuser')
except MySQLdb.Error:
   e = sys.exc_info()[1]
   print("Error %d: %s" % (e.args[0],e.args[1]))
   sys.exit(1)

# Initialize rtapi with database connection
   rt = rtapi.RTObject(db)

# List all objects from database

   print rt.ListObjects()

The error I get is:

python3 ./sqltest.py 
  File "./sqltest.py", line 18
    print rt.ListObjects()
           ^
SyntaxError: invalid syntax

I could really use some help.

seems changing the line
print rt.ListObjects()
to
print(rt.ListObjects())
got rid of the syntax error, but now I get this instead:

Traceback (most recent call last):
  File "sqltest.py", line 19, in <module>
    print(rt.ListObjects())
  File "/usr/local/lib/python3.5/dist-packages/rtapi/__init__.py", line 109, in ListObjects
    return "Found " + str(self.db_query_one(sql)[0]) + " objects in database"
  File "/usr/local/lib/python3.5/dist-packages/rtapi/__init__.py", line 78, in db_query_one
    self.dbresult.execute(sql)
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python3/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 217, in execute
    res = self._query(query)
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 378, in _query
    rowcount = self._do_query(q)
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 341, in _do_query
    db.query(q)
  File "/usr/lib/python3/dist-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.ProgrammingError: (1146, "Table 'racktables.Object' doesn't exist")

I have confirmed that the database credentials are accurate and can see the database being accessed via a tcpdump. Hoping someone can help shed some light on this.

Hi,
I have just tested it and I think it works ok for me. Do you have racktables database initialized and working ?
Meybe there is some difference in our environment.

What version of these modules in python are you using?

  • mysqlclient
  • racktables-api

I'm testing it with docker-compose using following method:

create docker-compose.yaml with following content

version: '3'
services:
  rtdb:
    image: registry.gitlab.com/rvojcik/rtapi/test/rtdb:0.21.1
    ports:
    - 3306:3306
  python:
    image: python:3.5
    entrypoint: [""]
    command: ["sleep", "7200"]

  • Run docker-compose up -d
  • Run docker-compose exec python pip3 install mysqlclient
  • Run docker-compose exec python pip3 install racktables-api
  • Run docker-compose exec python python

In python shell use this

import MySQLdb
import sys

# Create connection to database
try:
    # Create connection to database
    db = MySQLdb.connect(host='rtdb',port=3306, passwd='toor',db='racktables',user='root')
except MySQLdb.Error:
    e = sys.exc_info()[1]
    print("Error %d: %s" % (e.args[0],e.args[1]))
    sys.exit(1)

import rtapi
rt = rtapi.RTObject(db)
print(rt.ListObjects())
commented

README.md example is
print rt.ListObjects()

working example above is
print(rt.ListObjects())