0

I am using SQLite3 in Python and everything is working apart from when I am using RANDOM() and LIMIT. I've searched everywhere and I am pretty sure I am using the correct syntax, and I get no error. Every time I run this, no matter what LIMIT I specify (Or whether it's a fixed or with input) it only displays 1 result. Here is the code:

def RandomPlaylist():
    conn = sqlite3.connect("Playlist.db")
    c=conn.cursor()
    RandomPlaylistLimit = input("How many songs do you want in your playlist [1-100]?")
    RandomPlaylistLimitInt = int(RandomPlaylistLimit)
    print(RandomPlaylistLimitInt)
    if RandomPlaylistLimitInt < 101:

        c.execute("SELECT * FROM Songs ORDER BY RANDOM() LIMIT {0}".format(RandomPlaylistLimit))
        for row in c.fetchall():
            print("""
            Song Name: {0}.

            Artist: {1}.

            Album: {2}.

            Length: {3}.

            Year: {4}.

            """.format(row[1], row[2], row[3], row[5], row[6]))
James Smith
  • 95
  • 1
  • 2
  • 7
  • It looks like you've taken the code from [here](https://stackoverflow.com/a/1253576/4799172) and nobody has pointed out issues there. Out of curiosity, what happens if you hard-code the `LIMIT` rather than `format()`? – roganjosh Mar 22 '18 at 21:13
  • 1
    The exact same thing happens when it is hard coded in. I did say that but I am not sure I worded it right :p. – James Smith Mar 22 '18 at 21:21
  • 2
    We need a [mcve]. (Ideally, give us sample code that does a `connect(':memory:'), creates a table, inserts a few rows, executes the select, and returns 1 row when you expected 2.) – abarnert Mar 22 '18 at 21:23
  • 1
    For example, something like [this](https://repl.it/repls/SnappyPrestigiousCamel)—except of course that need to reproduce your problem so we can see what's different. – abarnert Mar 22 '18 at 21:27
  • Did you check how many rows do yo have in the `songs` table? It looks like the code snippet you posted should be able to fetch the desired rows, most likely you only have one row in that table. – hflzh Mar 23 '18 at 03:37
  • I have 99 rows, I can SELECT * and it display all of them, it just seems that LIMIT isn't working. – James Smith Mar 23 '18 at 09:40
  • Does the same thing happen if you use the `sqlite3` command line tool? I just tried it myself, and for me, `LIMIT` does work. – Arndt Jonasson Mar 23 '18 at 12:54
  • It works with the command line, it's just in the way I am doing it I think. – James Smith Mar 23 '18 at 16:28

0 Answers0