sqlalchemy Sqlite not loading table rows - sqlite

I'm using sqlite with sqlalchemy:
when I execute this command:
engine.execute("select * from teams");
<sqlalchemy.engine.base.ResultProxy object at 0x245b190>
Thus, it works as it returns a reference
I've a .py file which contain the class Team:
class Team(Base):
__tablename__ = 'teams'
id = Column(Integer,autoincrement=False, primary_key=True)
name = Column(String(255))
flag = Column(String(255))
url = Column(String(255))
I import Team from classes import
But when I try to use the ORM, i get the following error:
for instance in session.query(Team):
... print instance.id
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/orm/query.py", line 2224, in __iter__
return self._execute_and_instances(context)
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/orm/query.py", line 2239, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/engine/base.py", line 1450, in execute
params)
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/engine/base.py", line 1583, in _execute_clauseelement
compiled_sql, distilled_params
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/engine/base.py", line 1697, in _execute_context
context)
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/engine/base.py", line 1690, in _execute_context
context)
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/engine/default.py", line 331, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (OperationalError) no such table: teams u'SELECT teams.id AS teams_id, teams.name AS teams_name, teams.flag AS teams_flag, teams.url AS teams_url \nFROM teams' ()

It says it can't find the table teams- confirm with SqlLite ( I use firefox plugin) that it exists and that is the name. Also, copy the SQL in the error message in a Sqlite window and see if it runs.

Related

problem in Change Directory in colab for google drive

I want download video from YouTube with youtube_dl with Colab and save it in google drive. I make a directory with the name of video title and save video in that folder. Then I use this code:
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
URL = "https://www.youtube.com/watch?v=QTPP-iaF7BY&t=1955s"
!pip install youtube_dl
import youtube_dl
with youtube_dl.YoutubeDL({"ignoreerrors": True, "quiet": True}) as ydl:
playlist_dict = ydl.extract_info(URL, download=False)
print('\n', playlist_dict['title'], '\n')
import os
new_folder = playlist_dict['title']
path = f"//content//drive//MyDrive//{new_folder}//".replace("'"," ").replace(".","-").replace(":","-")
os.makedirs(path, exist_ok=True)
print('\n', path, '\n')
%cd {path}
But for the URL that I specified in the above code it get this error:
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Transport endpoint is not connected
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Transport endpoint is not connected
The folder you are executing pip from can no longer be found.
ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.
Pillai "Hoeffding's Inequality"
//content//drive//MyDrive//Pillai "Hoeffding's Inequality"//
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-13-dd9eae6c92da>", line 20, in <module>
get_ipython().magic('cd {path}')
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2160, in magic
return self.run_line_magic(magic_name, magic_arg_s)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2081, in run_line_magic
result = fn(*args,**kwargs)
File "<decorator-gen-84>", line 2, in cd
File "/usr/local/lib/python3.7/dist-packages/IPython/core/magic.py", line 188, in <lambda>
call = lambda f, *a, **k: f(*a, **k)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/magics/osm.py", line 288, in cd
oldcwd = py3compat.getcwd()
OSError: [Errno 107] Transport endpoint is not connected
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 1823, in showtraceback
stb = value._render_traceback_()
AttributeError: 'OSError' object has no attribute '_render_traceback_'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/IPython/core/ultratb.py", line 1132, in get_records
return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/ultratb.py", line 313, in wrapped
return f(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/ultratb.py", line 358, in _fixed_getinnerframes
records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
File "/usr/lib/python3.7/inspect.py", line 1502, in getinnerframes
frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
File "/usr/lib/python3.7/inspect.py", line 1460, in getframeinfo
filename = getsourcefile(frame) or getfile(frame)
File "/usr/lib/python3.7/inspect.py", line 696, in getsourcefile
if getattr(getmodule(object, filename), '__loader__', None) is not None:
File "/usr/lib/python3.7/inspect.py", line 725, in getmodule
file = getabsfile(object, _filename)
File "/usr/lib/python3.7/inspect.py", line 709, in getabsfile
return os.path.normcase(os.path.abspath(_filename))
File "/usr/lib/python3.7/posixpath.py", line 383, in abspath
cwd = os.getcwd()
OSError: [Errno 107] Transport endpoint is not connected
with other URL in YouTube I haven't this problem and it downloads and saves correctly in Google Drive.
EDIT
With changing %cd {path} to os.chdir(path) the problem solved. But
I don't understand why %cd {path} work for some and don't work
for others.

sqllite argumentError absolute path

I am trying to create a sqlite database using windows and I have an error "ArgumentError: Invalid SQLite URL: sqlite://search.db" What is the absolute path syntax for creating an SQL lite db.
'sqlite://C:/Users..../search.db'
>>> from flaskapp import db
C:\Users\champ\Python_proj\main_env\lib\site-packages\flask_sqlalchemy\__init__.py:833: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
warnings.warn(FSADeprecationWarning(
>>> db.create_all()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 1039, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 1031, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
return connector.get_engine()
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 556, in get_engine
self._engine = rv = self._sa.create_engine(sa_url, options)
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 972, in create_engine
return sqlalchemy.create_engine(sa_url, **engine_opts)
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\sqlalchemy\engine\__init__.py", line 488, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\sqlalchemy\engine\strategies.py", line 98, in create
(cargs, cparams) = dialect.create_connect_args(u)
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\sqlalchemy\dialects\sqlite\pysqlite.py", line 466, in create_connect_args
raise exc.ArgumentError(
sqlalchemy.exc.ArgumentError: Invalid SQLite URL: sqlite://search.db
Valid SQLite URL forms are:
sqlite:///:memory: (or, sqlite://)
sqlite:///relative/path/to/file.db
sqlite:////absolute/path/to/file.db

Try, Except: sqlite3.OperationalError: no such table:

I'm having some problems with my flask application which I never had before until today and I haven't changed anything so it's quite confusing
So it my app.py
#app.route('/view.html')
def data():
try:
path = 'app/database.db'
con = sqlite3.connect(path)
cur = con.cursor()
con.row_factory = sqlite3.Row
rows = cur.execute("select * from table").fetchall()
return render_template('layouts/default.html',
content=render_template('pages/view.html',
rows=rows))
except sqlite3.OperationalError:
return render_template('layouts/default.html',
content=render_template('pages/view.html'))
The idea is that if there is no table in the database yet, it will catch the error and just return the view.html with no data but for some reason it does not work anymore
Just realised I completely forgot to add the error, my bad
[2020-02-22 23:56:26,209] ERROR in app: Exception on /favicon.ico [GET]
Traceback (most recent call last):
File " -directory- ", line 316, in index
rows = cur.execute("select * from table")
sqlite3.OperationalError: no such table: table
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File " -directory- /app/views.py", line 337, in index
content=render_template('pages/' + path))
File "/opt/anaconda3/lib/python3.7/site-packages/flask/templating.py", line 138, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/opt/anaconda3/lib/python3.7/site-packages/jinja2/environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/opt/anaconda3/lib/python3.7/site-packages/jinja2/environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "/opt/anaconda3/lib/python3.7/site-packages/jinja2/environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "/opt/anaconda3/lib/python3.7/site-packages/jinja2/loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "/opt/anaconda3/lib/python3.7/site-packages/flask/templating.py", line 60, in get_source
return self._get_source_fast(environment, template)
File "/opt/anaconda3/lib/python3.7/site-packages/flask/templating.py", line 89, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: pages/favicon.ico
And just to note, the app runs fine on localhost, it just shows error now when it never did before

"Adapter does not support geometry" exception when declaring geometry field

In my application, an "Adapter does not support geometry" exception is being thrown when attempting to create a field of type, "geometry()". For my test application, I'm using an sqlite DB (production will use postgres):
db = DAL('sqlite://storage.sqlite', pool_size = 1, fake_migrate_all= False)
The DB table in question is declared within a class, inside of a module, and contains a several fields, some of which contain location data:
from gluon.dal import Field, geoPoint, geoLine, geoPolygon
class Info(Base_Model):
def __init__(...):
try:
db.define_table('t_info',
...
Field('f_geolocation', type='geometry()',
label = current.T('Geolocation')),
Field('f_city', type='string',
label = current.T('City')),
...
except Exception as e:
...
Edit:
As per Anthony's suggestion, I've modified the DAL constructor call to the following:
db = DAL('spatialite://storage.sqlite', pool_size = 1)
It produces the following error message:
Traceback (most recent call last):
File "C:\...\web2py\gluon\restricted.py", line 227, in restricted
exec ccode in environment
File "C:/My_Stuff/Programs/web2py/applications/Proj/models/db.py", line 38, in <module>
db = DAL('spatialite://storage.sqlite', pool_size = 1)
File "C:\...\web2py\gluon\packages\dal\pydal\base.py", line 171, in __call__
obj = super(MetaDAL, cls).__call__(*args, **kwargs)
File "C:\...\web2py\gluon\packages\dal\pydal\base.py", line 457, in __init__
raise RuntimeError("Failure to connect, tried %d times:\n%s" % (attempts, tb))
RuntimeError: Failure to connect, tried 5 times:
Traceback (most recent call last):
File "C:\...\web2py\gluon\packages\dal\pydal\base.py", line 435, in __init__
self._adapter = ADAPTERS[self._dbname](**kwargs)
File "C:\...\web2py\gluon\packages\dal\pydal\adapters\base.py", line 53, in __call__
obj = super(AdapterMeta, cls).__call__(*args, **kwargs)
File "C:\...\web2py\gluon\packages\dal\pydal\adapters\sqlite.py", line 169, in __init__
if do_connect: self.reconnect()
File "C:\...\web2py\gluon\packages\dal\pydal\connection.py", line 129, in reconnect
self.after_connection_hook()
File "C:\...\web2py\gluon\packages\dal\pydal\connection.py", line 81, in after_connection_hook
self.after_connection()
File "C:\...\web2py\gluon\packages\dal\pydal\adapters\sqlite.py", line 177, in after_connection
self.execute(r'SELECT load_extension("%s");' % libspatialite)
File "C:\...\web2py\gluon\packages\dal\pydal\adapters\base.py", line 1326, in execute
return self.log_execute(*a, **b)
File "C:\...\web2py\gluon\packages\dal\pydal\adapters\base.py", line 1320, in log_execute
ret = self.cursor.execute(command, *a[1:], **b)
OperationalError: The specified module could not be found.
If you want to use geometry fields with SQLite, you must use the spatialite adapter, which makes use of the SpatialLite extension for SQLite:
db = DAL('spatialite://storage.sqlite', pool_size = 1)
Note, you must have spatialite installed for this to work.

Default value ignored in non-nullable column

I'm trying to create a new boolean, non-nullable column (with default=True) in a table with the following SQL alchemy script:
from sqlalchemy import MetaData, Table, Boolean, Column
def upgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
message_table = Table('message', meta, autoload=True)
col = Column('include_signature', Boolean(), default=True, nullable=False)
col.create(message_table)
This results in a rather puzzling error:
File "src/adhocracy/migration/versions/061_optional_massmessage_signature.py", line 7, in upgrade
col.create(message_table)
File "eggs/sqlalchemy_migrate-0.7.2-py2.6.egg/migrate/changeset/schema.py", line 528, in create
engine._run_visitor(visitorcallable, self, connection, **kwargs)
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 2302, in _run_visitor
conn._run_visitor(visitorcallable, element, **kwargs)
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1972, in _run_visitor
**kwargs).traverse_single(element)
File "eggs/sqlalchemy_migrate-0.7.2-py2.6.egg/migrate/changeset/ansisql.py", line 53, in traverse_single
ret = super(AlterTableVisitor, self).traverse_single(elem)
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/sql/visitors.py", line 106, in traverse_single
return meth(obj, **kw)
File "eggs/sqlalchemy_migrate-0.7.2-py2.6.egg/migrate/changeset/databases/sqlite.py", line 75, in visit_column
super(SQLiteColumnGenerator,self).visit_column(column)
File "eggs/sqlalchemy_migrate-0.7.2-py2.6.egg/migrate/changeset/ansisql.py", line 101, in visit_column
self.execute()
File "eggs/sqlalchemy_migrate-0.7.2-py2.6.egg/migrate/changeset/ansisql.py", line 42, in execute
return self.connection.execute(self.buffer.getvalue())
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1449, in execute
params)
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1628, in _execute_text
statement, parameters
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1698, in _execute_context
context)
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1691, in _execute_context
context)
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/default.py", line 331, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (OperationalError) Cannot add a NOT NULL column
with default value NULL '\nALTER TABLE message ADD include_signature BOOLEAN NOT NULL' ()
As far as I understand, sqlalchemy is missing a default value, but I did include default=True! What is wrong here?
Use server_default="true", like that:
col = Column('include_signature', Boolean(), default=True,
server_default="true", nullable=False)
Where default is used for the class instance and server_default is used for the database column definition.

Resources