bottlepy / bottle-sqlite

MIT License
24 stars 13 forks source link

syntax error near unexpected token `SQLitePlugin' #25

Open TRSx80 opened 4 years ago

TRSx80 commented 4 years ago

Hi guys!

Thank you very much for creating and sharing this project. I'm really excited about the possibilities.

I got the basics working, however I ran into a problem trying to connect to an sqlite3 database. I suspect my problem revolves around improper scope, naming, or something like that.

By the way, I really appreciate that you guys wrote the basic tutorial leaving out stuff like app.@route, etc. and introduce that later on. Personally I find the OO stuff just confusing, and unnecessarily complex for a small project.

Anyway, when trying to implement the sqlite plugin, the examples given all revolve around OO type naming. I tried to convert it, even looked at different examples, including the source, but I still could not get it to work. I keep getting variations of the error in the title.

I first tried this way (minimal example):

# app.py
from bottle import install, request, route, run, static_file, template
from bottle_sqlite import SQLitePlugin

install(SQLitePlugin(dbfile='database.sqlite3'))

Then I try this way:

# app.py
from bottle import install, request, route, run, static_file, template
from bottle.ext import sqlite

install(sqlite.Plugin(dbfile='database.sqlite3'))

But when I launch my app.py I just keep getting variation of:

from: too many arguments
from: too many arguments
./app.py: line 4: syntax error near unexpected token `SQLitePlugin'
./app.py: line 4: `install(SQLitePlugin(dbfile='database.sqlite3'))'
TRSx80 commented 4 years ago

I just tried again, this time following exactly (other than my db file name) the example from here:

import bottle

app = bottle.Bottle()
plugin = bottle.ext.sqlite.Plugin(dbfile='database.sqlite3')
app.install(plugin)

Now my app.py starts, but I get nothing. Mouse cursor actually turns to a crosshair(?). If I click or Ctrl-c out of it, I then get this error:

^C./app.py: line 3: syntax error near unexpected token `('
./app.py: line 3: `app = bottle.Bottle()'

I tried then in a practically brand new VM, at least now I got a different error in addition:

./app.py: line 1: import: command not found                  # <- - only seen in VM
./app.py: line 3: syntax error near unexpected token `('
./app.py: line 3: `app = bottle.Bottle()'

Wondering how it could be possible that import itself was not found, a quick Internet search led me to my silly error of not specifying the python3 executable via either shebang or calling app.py explicitly like python3 app.py. So after adding #!/usr/bin/python3 I think I make some progress now. This is on Debian Stable/Testing BTW, so python still points to python2.7. Anyway, moving on now I get this error (on my machine as well as VM):

Traceback (most recent call last):
  File "app.py", line 6, in <module>
    plugin = bottle.ext.sqlite.Plugin(dbfile='database.sqlite3')
AttributeError: module 'bottle.ext' has no attribute 'sqlite'

Then this led on to a whole bunch of other attempts at playing "guess the correct name" on my main machine as well as the VM, all to no avail (thus far)...

I was going to copy and paste some of that but I am running out of time just now, so I will have to check back in later. Any pointers in the meantime would be greatly appreciated, of course.

defnull commented 4 years ago

The bottle.ext pseudo-module is just some magic (not recommended any more) to import bottle_whatever. You still have to import a module before usage, though:

import bottle.ext.sqlite     # actually imports bottle_sqlite

After that, you can use it. The example in the readme seems to be incomplete.

TRSx80 commented 4 years ago

Thanks for the tip!

I ended up getting it working by following the ToDo example, and just straight calling the regular sqlite3 from the standard library like they do in there. That was enough to get me going with the basics. And there are a lot more examples and things to search for when using that more common terminology out on the 'Net.

I am sure I will circle back to the SQLitePlugin at some point when I understand more about the basics of Python and how it works with databases. But right now something I'm sure is useful (somehow) is still in the category of "too much extra layers of complication" for me to grasp at my current low Python wizard level. :smile:

I was however overjoyed to actually be able to connect to a database and get some information out of it over a (local) web interface, something that I had been trying to do (on and off) for literally years if not decades. So to say that I am excited at the possibilities is actually quite an understatement. :smile:

In other words, this project has already brought me more joy than you can possibly know. So I wanted to thank you again for sharing/maintaining it. :beers:

Point of order, I will leave it up to you whether to close or not, and when, seeing as how it sounds like the Docs might need to be updated? Sorry but I still don't know enough to contribute anything useful there. :man_shrugging: One day... but today is not that day...