Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Accessing sqlite3 db tables and contents

I have sqlite version 3.39.3 installed on my windows computer, but I'm having issues accessing the table that was created when creating the pet.db.

I'm typing in sqlite3.exe pets.db, which then prompts me for "Enter .help for usage hints". However, when I type in .tables there are no tables that are referenced and I therefore cannot access the table data.

Has anyone had this issue before and has a solution?

2 Answers

Steven Parker
Steven Parker
229,732 Points

The version shouldn't matter, so you could reconstruct your DB in a Treehouse workspace, and then you can share a snapshot of your workspace to allow us to replicate your issue.

I'm using VSCode, below are the relevant scripts to the DB construction. (Sorry couldn't get the markdown to properly work with the code)

models.py:

from flask import Flask from flask_sqlalchemy import SQLAlchemy import datetime

app = Flask(name) app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///pets.db" db = SQLAlchemy(app)

class Pet(db.Model): id = db.Column(db.Integer, primary_key=True) created = db.Column("Created", db.DateTime, default=datetime.datetime.now) # this is not datetime.datetime.now() due to the () means it called when the file is run and never called again name = db.Column("Name", db.String()) age = db.Column("Age", db.String()) breed = db.Column("Breed", db.String()) color = db.Column("Color", db.String()) size = db.Column("Size", db.String()) weight = db.Column("Weight", db.String()) url = db.Column("URL", db.String()) url_tag = db.Column("Alt Tag", db.String()) pet_type = db.Column("Pet Type", db.String()) gender = db.Column("Gender", db.String()) spay = db.Column("Spay", db.String()) house_trained = db.Column("House Trained", db.String()) description = db.Column("Description", db.Text())

def __repr__(self):
    return f"""<Pet (Name: {self.name})
            Age: {self.age}
            Breed: {self.breed}
            Color: {self.color}
            Size: {self.size}
            Weight: {self.weight}
            URL: {self.url}
            Tag: {self.url_tag}
            Gender: {self.gender}
            Spay: {self.spay}
            House Trained: {self.house_trained}
            Description: {self.description}
            """

app.py:

from flask import (render_template, url_for, request, redirect) from models import db, Pet, app

@app.route("/") def index(): return render_template("index.html")

@app.route("/add-pet", methods=["GET", "POST"]) def add_pet(): if request.form: print(request.form) print(request.form["name"]) new_pet = Pet(name=request.form["name"], age=request.form["age"], breed=request.form["breed"], color=request.form["color"], size=request.form["size"], weight=request.form["weight"], url=request.form["url"], url_tag=request.form["alt"], pet_type=request.form["pet"], gender=request.form["gender"], spay=request.form["spay"], house_trained=request.form["housetrained"], description=request.form["description"] ) db.session.add(new_pet) db.session.commit() return redirect(url_for("index")) return render_template("addpet.html")

@app.route("/pet") def pet(): return render_template("pet.html")

if name == "main": db.create_all()

app.run(debug=True, port=8000, host="127.0.0.1")

Steven Parker
Steven Parker
229,732 Points

Moving this to a workspace will allow you to share a snapshot, which will make replicating the issue simple for those trying to help (even if they don't have VSCode installed), plus you won't need Markdown.