Forums

sqlite3

Hi My code is written with sqlite3

But I get an error using import sqlite3

How do I do this?

What's the error you get when you import sqlite3?

I get yellow triangle on the left side of import sqlite3

If you hover over the yellow triangle, what does it say?

Hi I get sqlite3 is imported but unused

I guess your problem is that your code isn't yet using the sqlite3 module which you've imported?

hi

I use import sqlite3

I have this


def handle_get():
    db = sqlite3.connect('/home/miranoy/mysite/scores.db')
    cur = db.execute('select  FirstName from Scores')
    return render_template("main_page.html", comments=cur.fetchall())

and I get empty results though I know table Scores is not empty

[edited by admin: formatting]

Is the table data in your local machine/have you transferred it to pythonanywhere yet?

Hi Thanks for helping

it is in /home/miranoy/mysite/ as is flask_app.py

maybe cur.fetchall() is wrong

and i also wrote into test.txt the content of scores.db

db = sqlite3.connect('/home/miranoy/mysite/scores.db')

cur = db.execute('select  FirstName from Scores')

with open("/home/miranoy/mysite/test.txt", "w") as f:

    for row in cur:

          f.write(row[0])
          f.write("\n")

in test.txt I got

A C E Eytan A A A Hello

I would try printing out what fetchall returns (do that by printing to stderr or running the file from a console instead of a webapp)

and also seeing if passing that into the template renders correctly. eg. maybe your template expects a list but you pass in a string etc

Hi

I moved to mysql since sqlite3 is not working fotr me

and I dont see i extract in the app part of the rows

"""" As I understand I define the table I select from here

class Comment(db.Model): tablename = "comments" id = db.Column(db.Integer, primary_key=True) content = db.Column(db.String(4096))

But what if I don’t need all the rows from comments but part of them

Select * from comments where content like ‘A%’; """""

From the table definition, it looks like you're using SQLAlchemy. Here's a tutorial on the expression language and how to do ORM queries.

Hi I m using SQLAlchemy because this is what you have in the tutorial Is there another option?

Sure there are many options (just google "python ORM"), or you can avoid using an ORM all together. There are different advantages and disadvantages for any choice like that.

Hey Ok I was reding and found an easy way to connect to mysql at least I think so

con = mdb.connect('miranoy.mysql.pythonanywhere-services.com', 'miranoy', '****', 'miranoy$daily_bl'); cur = con.cursor() cur.execute("SELECT * FROM daily_master")

That can definitely work. There are a lot of good reasons for using an ORM like SQLAlchemy instead, though -- here are the two that I think are most important:

  1. Connection management. If you're doing everything by hand, you need to connect and disconnect from the database. This means that (a) you have to write the code to do that and (b) if you forget to disconnect, then the database will leave a connection open for up to five minutes (on PythonAnywhere, the time may be longer elsewhere) -- because the database limits the number of connections you can have open at any one time, if you make lots of connections and forget to close them (which is surprisingly easy to do) your code will suddenly stop working because you hit the limit.
  2. SQL injection attacks. If you're writing code that accepts user input and then puts it into SQL queries, then you have to be super-careful to make sure that users don't enter input that, when added to your queries, makes a different query. ORMs, used properly, will protect you from that.